Просмотр исходного кода

Keep full stops readable in every PDF, whatever the server printed first (#647)

The fonts draw a colon from the full stop's outline, and the font library
keeps one glyph object per face for as long as the server runs, remembering
the character it was first asked for. Writing a colon into a PDF fetched the
full stop's outline with no character, so when the first document a server
rendered had a colon and no full stop in some face, every later PDF drew its
full stops correctly and could not be read back: text extraction, search and
copying gave "james mitchell@gmail com" and "$500 00".

Each face is now asked for all its characters before the first render, which
settles what every glyph stands for. Every server render waits for that, and
a test fails if a new one does not.
Bernt Christian Egeland 2 дней назад
Родитель
Сommit
9f01e7151e

+ 81 - 0
src/__tests__/features/pdf-fonts/full-stop-after-colon.test.ts

@@ -0,0 +1,81 @@
+/**
+ * @vitest-environment node
+ *
+ * A PDF's text has to read back as what it shows: it is searched, copied and
+ * read aloud. The fonts draw a colon from the full stop's outline, and a
+ * server whose first document had a colon and no full stop used to lose the
+ * full stop's character for good: every later sheet showed "$500.00" and read
+ * back "$500 00". These render real documents, in that order.
+ */
+import { readFileSync, readdirSync, statSync } from 'node:fs'
+import { join } from 'node:path'
+import { createElement } from 'react'
+import { Document, Page, Text, renderToBuffer } from '@react-pdf/renderer'
+import { extractText } from 'unpdf'
+import { describe, expect, it } from 'vitest'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
+
+const FAMILIES = [
+  'Roboto',
+  'Noto Serif',
+  'Noto Sans Mono',
+  'Open Sans',
+  'Lato',
+  'Montserrat',
+  'PT Sans',
+]
+
+const sheet = (fontFamily: string, lines: string[]) =>
+  createElement(
+    Document,
+    null,
+    createElement(
+      Page,
+      { size: 'A4', style: { fontFamily, fontSize: 9 } },
+      lines.map((line) => createElement(Text, { key: line }, line)),
+      createElement(Text, { style: { fontFamily: `${fontFamily}-Bold` } }, lines.join(' '))
+    )
+  )
+
+async function read(fontFamily: string, lines: string[]): Promise<string> {
+  const buffer = await renderToBuffer(sheet(fontFamily, lines))
+  const { text } = await extractText(new Uint8Array(buffer), { mergePages: true })
+  return String(text).replace(/\s+/g, ' ')
+}
+
+describe('a full stop on a sheet printed after one that had only colons', () => {
+  it.each(FAMILIES)('reads back as a full stop in %s', async (family) => {
+    await pdfFontsReady()
+    // The first sheet of the day: colons, an ellipsis, a semicolon, no full stop.
+    await read(family, ['Engine Hours: 1,250 hrs', 'Registration: NB-4521-E; wait…'])
+
+    const text = await read(family, ['james.mitchell@gmail.com', '$500.00', 'Invoice No. 7'])
+
+    expect(text).toContain('james.mitchell@gmail.com')
+    expect(text).toContain('$500.00')
+    expect(text).toContain('Invoice No. 7')
+    // Regular and bold both.
+    expect(text.match(/\$500\.00/g)).toHaveLength(2)
+  })
+})
+
+function sourceFiles(dir: string): string[] {
+  return readdirSync(dir).flatMap((name) => {
+    const path = join(dir, name)
+    if (name === '__tests__' || name === 'generated' || name === 'node_modules') return []
+    if (statSync(path).isDirectory()) return sourceFiles(path)
+    return /\.tsx?$/.test(name) ? [path] : []
+  })
+}
+
+describe('every document the server renders', () => {
+  it('waits for the fonts first', () => {
+    const offenders = sourceFiles('src').filter((path) => {
+      const source = readFileSync(path, 'utf8')
+      const renders = source.match(/renderTo(Buffer|Stream)\(/g)?.length ?? 0
+      const waits = source.match(/await pdfFontsReady\(\)/g)?.length ?? 0
+      return renders > waits
+    })
+    expect(offenders).toEqual([])
+  })
+})

+ 3 - 1
src/__tests__/features/quotes/quote-pdf-marine-labels.test.ts

@@ -33,7 +33,9 @@ vi.mock('@/lib/torqvoice-branding', () => ({ getTorqvoiceLogoDataUri: vi.fn(asyn
 vi.mock('@/features/custom-fields/Lib/getCustomFieldsForPrint', () => ({
   getCustomFieldsForPrint: vi.fn(async () => []),
 }))
-vi.mock('@/features/vehicles/Components/invoice-pdf/fonts', () => ({}))
+vi.mock('@/features/vehicles/Components/invoice-pdf/fonts', () => ({
+  pdfFontsReady: async () => undefined,
+}))
 vi.mock('@/features/quotes/Components/QuotePDF', () => ({ QuotePDF: () => null }))
 vi.mock('@react-pdf/renderer', () => ({
   renderToBuffer: vi.fn(async (element: { props: Record<string, unknown> }) => {

+ 3 - 1
src/__tests__/features/reports/report-schedule-timezone.test.ts

@@ -18,7 +18,9 @@ vi.mock('@/lib/email', () => ({
 vi.mock('@react-pdf/renderer', () => ({
   renderToBuffer: vi.fn(async () => Buffer.from('pdf')),
 }))
-vi.mock('@/features/vehicles/Components/invoice-pdf/fonts', () => ({}))
+vi.mock('@/features/vehicles/Components/invoice-pdf/fonts', () => ({
+  pdfFontsReady: async () => undefined,
+}))
 vi.mock('@/features/reports/Components/ReportPDF', () => ({ ReportPDF: () => null }))
 // A plain function, so a resetAllMocks in a beforeEach cannot blank it.
 vi.mock('@/lib/workshop-timezone', () => ({

+ 2 - 1
src/app/api/protected/inspections/[id]/pdf/route.ts

@@ -1,6 +1,6 @@
 import { NextResponse } from 'next/server'
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import { resolveDocumentLocale } from '@/i18n/locale-from-request'
 import { getAuthContext } from '@/lib/get-auth-context'
 import { db } from '@/lib/db'
@@ -181,6 +181,7 @@ export async function GET(_request: Request, { params }: { params: Promise<{ id:
       attachedDocuments: documents.map((document) => document.fileName),
       // eslint-disable-next-line @typescript-eslint/no-explicit-any
     }) as any
+    await pdfFontsReady()
     const buffer = await renderToBuffer(element)
     // The signed forms follow the certificate as pages of their own.
     const body = await appendCertificateDocuments(buffer, documents)

+ 2 - 1
src/app/api/protected/tire-hotel/[id]/labels/route.ts

@@ -1,7 +1,7 @@
 import { NextResponse } from 'next/server'
 import React from 'react'
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import { cookies } from 'next/headers'
 import { createTranslator } from 'next-intl'
 import { getAuthContext } from '@/lib/get-auth-context'
@@ -140,6 +140,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ id:
       seasonLabel,
       // Cast because react-pdf's element typing does not describe this.
     }) as any
+    await pdfFontsReady()
     const buffer = await renderToBuffer(element)
 
     const fileName = `tire-labels-${set.reference ?? set.id}.pdf`

+ 2 - 1
src/app/api/protected/vehicles/[id]/service-history-pdf/route.ts

@@ -1,7 +1,7 @@
 import { resolveCurrencyCode, resolveCurrencyFormat } from '@/lib/currencies'
 import { NextResponse } from 'next/server'
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import { resolveDocumentLocale } from '@/i18n/locale-from-request'
 import { getAuthContext } from '@/lib/get-auth-context'
 import { db } from '@/lib/db'
@@ -180,6 +180,7 @@ export async function GET(_request: Request, { params }: { params: Promise<{ id:
       labels,
       // eslint-disable-next-line @typescript-eslint/no-explicit-any
     }) as any
+    await pdfFontsReady()
     const pdfBuffer = await renderToBuffer(element)
 
     const vehicleName = `${vehicleNameOf(vehicle)}`

+ 2 - 1
src/features/inspections/Pdf/buildCertificatePdfBuffer.ts

@@ -3,7 +3,7 @@ import 'server-only'
 import { readFile } from 'fs/promises'
 import React from 'react'
 import { type DocumentProps, renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import { db } from '@/lib/db'
 import { getFeatures } from '@/lib/features'
 import { resolveUploadPath } from '@/lib/resolve-upload-path'
@@ -176,6 +176,7 @@ export async function buildCertificatePdfBuffer({
     bodyType: inspection.vehicle.bodyType,
     conditionMapLabels,
   }) as unknown as React.ReactElement<DocumentProps>
+  await pdfFontsReady()
   const rendered = await renderToBuffer(element)
   const body = await appendCertificateDocuments(Buffer.from(rendered), documents)
   const vehicleName = vehicleNameOf(inspection.vehicle, '-')

+ 2 - 1
src/features/inspections/Pdf/customerCertificatePdf.ts

@@ -1,6 +1,6 @@
 import 'server-only'
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import React from 'react'
 import { readFile } from 'fs/promises'
 import { db } from '@/lib/db'
@@ -194,6 +194,7 @@ export async function buildCustomerCertificatePdf({
     attachedDocuments: documents.map((document) => document.fileName),
     // eslint-disable-next-line @typescript-eslint/no-explicit-any
   }) as any
+  await pdfFontsReady()
   const buffer = await renderToBuffer(element)
   // The signed forms follow the certificate as pages of their own.
   const body = await appendCertificateDocuments(buffer, documents)

+ 2 - 1
src/features/invoices/Pdf/buildInvoicePdfBuffer.ts

@@ -21,7 +21,7 @@
  */
 
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import React from 'react'
 import { InvoicePDF } from '@/features/vehicles/Components/InvoicePDF'
 import { getFeatures } from '@/lib/features'
@@ -96,6 +96,7 @@ export async function renderInvoicePdf(
     ...attachments,
     // eslint-disable-next-line @typescript-eslint/no-explicit-any
   }) as any
+  await pdfFontsReady()
   return renderToBuffer(element)
 }
 

+ 2 - 1
src/features/invoices/Pdf/buildWorkOrderPdfBuffer.ts

@@ -1,7 +1,7 @@
 import 'server-only'
 
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import React from 'react'
 import { getFeatures } from '@/lib/features'
 import { getTorqvoiceLogoDataUri } from '@/lib/torqvoice-branding'
@@ -57,6 +57,7 @@ export async function buildWorkOrderPdfBuffer(
     locale,
     // eslint-disable-next-line @typescript-eslint/no-explicit-any
   }) as any
+  await pdfFontsReady()
   const buffer = await renderToBuffer(element)
   return { buffer, filename: `${workOrderNumberOf(assembly.record)}-work-order.pdf` }
 }

+ 2 - 1
src/features/quotes/Pdf/buildQuotePdfBuffer.ts

@@ -18,7 +18,7 @@
 import { resolveCurrencyCode, resolveCurrencyFormat } from '@/lib/currencies'
 import { readFile } from 'node:fs/promises'
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import { PDFDocument } from 'pdf-lib'
 import React from 'react'
 import { getCustomFieldsForPrint } from '@/features/custom-fields/Lib/getCustomFieldsForPrint'
@@ -210,6 +210,7 @@ export async function buildQuotePdfBuffer(
     // eslint-disable-next-line @typescript-eslint/no-explicit-any
   }) as any
 
+  await pdfFontsReady()
   const rendered = await renderToBuffer(element)
   return {
     buffer: pdfAttachments.length > 0 ? await appendPdfs(rendered, pdfAttachments) : rendered,

+ 43 - 0
src/features/vehicles/Components/invoice-pdf/fonts.ts

@@ -38,3 +38,46 @@ for (const { family, regular, bold } of FAMILIES) {
 
 // Disable word hyphenation so Cyrillic text is not broken mid-word
 Font.registerHyphenationCallback((word) => [word])
+
+/** A registered face as react-pdf holds it: the parsed font, once it has been loaded. */
+interface LoadedFace {
+  data?: { characterSet?: number[]; glyphForCodePoint?: (codePoint: number) => unknown } | null
+}
+
+let primed: Promise<void> | null = null
+
+/**
+ * Resolves once every face knows which character each of its glyphs stands
+ * for. Await it before every render.
+ *
+ * The font library keeps one glyph object per face for as long as the server
+ * runs, and a glyph remembers the character it was first asked for. A colon
+ * in these fonts is drawn from the full stop's outline, and writing a colon
+ * into a PDF fetches that outline with no character at all. So when the first
+ * document a server rendered had a colon and no full stop in some face, the
+ * full stop had no character from then on: every later PDF drew it correctly
+ * and could not be read back, searched or copied ("james mitchell@gmail com",
+ * "$500 00"). Asking each face for all its characters first settles what
+ * every glyph stands for before any document can get there.
+ */
+export function pdfFontsReady(): Promise<void> {
+  primed ??= (async () => {
+    const faces = FAMILIES.flatMap(({ family }) => [
+      { fontFamily: family, fontWeight: 400 },
+      { fontFamily: family, fontWeight: 700 },
+      { fontFamily: `${family}-Bold` },
+    ])
+    for (const face of faces) {
+      try {
+        await Font.load(face)
+        const font = (Font.getFont(face) as LoadedFace | undefined)?.data
+        if (!font?.characterSet || !font.glyphForCodePoint) continue
+        for (const codePoint of font.characterSet) font.glyphForCodePoint(codePoint)
+      } catch (error) {
+        // The document still prints; only its text layer may read wrongly.
+        console.error(`[pdf] could not prepare the font ${face.fontFamily}`, error)
+      }
+    }
+  })()
+  return primed
+}

+ 2 - 1
src/lib/cron/report-schedules.ts

@@ -2,7 +2,7 @@ import { resolveCurrencyCode, resolveCurrencyFormat } from '@/lib/currencies'
 import { CronJob } from 'cron'
 import { db } from '@/lib/db'
 import { renderToBuffer } from '@react-pdf/renderer'
-import '@/features/vehicles/Components/invoice-pdf/fonts'
+import { pdfFontsReady } from '@/features/vehicles/Components/invoice-pdf/fonts'
 import React from 'react'
 import { sendOrgMail, getOrgFromAddress } from '@/lib/email'
 import { ReportPDF } from '@/features/reports/Components/ReportPDF'
@@ -752,6 +752,7 @@ export async function processOneSchedule(
   })
 
   // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  await pdfFontsReady()
   const pdfBuffer = await renderToBuffer(element as any)
 
   // Resolve recipient emails