/** * Where the company logo lands in the invoice header. * * The logo box is always drawn at the full 240x80pt reserved area (any logo * larger than that gets clamped to it), so the image itself is positioned * *inside* that box by objectFit/objectPosition. With the default centering, * a near-square logo scaled down to fit 80pt of height ends up indented by * half the leftover width, which reads as "the logo is not aligned with the * left margin" while a wide banner logo looks fine. The template therefore * anchors the image top-left, so every aspect ratio starts at the margin. * * This test renders the real PDF and reads the image placement matrix out of * the content stream, so it fails if the anchoring regresses. */ import { inflateSync } from 'node:zlib' import { describe, expect, it } from 'vitest' import React from 'react' import { renderToBuffer } from '@react-pdf/renderer' import { InvoicePDF } from '@/lib/invoices/pdf-template' import { makeCompanySettings, makeCustomer, makeInvoice } from '@/tests/helpers' import type { InvoiceItem } from '@/types' // The page uses a 40pt left margin; a left-anchored logo starts exactly there. const PAGE_MARGIN_PT = 40 async function makeLogoDataUrl(width: number, height: number): Promise { const { default: sharp } = await import('sharp') const png = await sharp({ create: { width, height, channels: 3, background: { r: 20, g: 80, b: 160 } }, }) .png() .toBuffer() return `data:image/png;base64,${png.toString('base64')}` } /** * Pull the placement of the first drawn image out of a rendered PDF. * * pdfkit emits ` 0 0 <-h> cm` followed by `/