On a phone, you can scroll through a receipt image that's 3,500 pixels tall. At Repceipt's print scale, that's nearly 44 cm of paper. Shrinking it to fit on a single Letter sheet makes the prices harder to read. You can see how small receipt type gets in Repceipt's receipt typography study.
The home-print export puts that receipt on two sheets. It starts with the PNG rendered by the server and works out how many rows of pixels fit on each page at the intended size.
Convert pixels to PDF points
Repceipt uses eight image pixels per millimeter for printing. A 576-pixel-wide receipt therefore occupies 72 mm on paper. The pxToPt helper divides by eight to get millimeters, divides by 25.4 to get inches, and multiplies by 72 to get PDF points. A PDF point is 1/72 of an inch at the default scale. Adobe's PDF coordinate guide.
A Letter page is 792 points high. Half-inch margins at the top and bottom leave 720 points for the image. Divide that by the size of one pixel in points and round down. That gives 2,032 whole rows on the first page. The remaining 1,468 go on the second.
Crop the image and place each slice
The slicePngSource helper copies each full-width slice to a temporary canvas with the same pixel dimensions as the crop. The export embeds each slice as a PNG using pdf-lib.
PDF coordinates start at the bottom of the page and increase upward. To position a slice under the top margin, subtract the margin and the slice height from the page height. Using the current slice height puts the last piece at the top of its page, even when that piece is much shorter than the first. Half the leftover width gives the horizontal offset for centering.
const margin = 36; const printableHeight = paper.height - margin * 2; const rowsPerPage = Math.floor(printableHeight / pxToPt(1)); const width = pxToPt(source.width); for (let top = 0; top < source.height; top += rowsPerPage) { const rows = Math.min(rowsPerPage, source.height - top); const png = await slicePngSource(source, top, rows); const image = await pdf.embedPng(new Uint8Array(await png.arrayBuffer())); const height = pxToPt(rows); const page = pdf.addPage([paper.width, paper.height]); page.drawImage(image, { x: (paper.width - width) / 2, y: paper.height - margin - height, width, height, }); } Because this export cuts at a fixed row count, a page break can land in the middle of a line of text.
A good test uses two images. Make one 2,032 pixels tall and the other 2,033. The first should fit on a single Letter page. The second should have a one-pixel-tall slice just below the top margin of page two. That extra row checks both the page boundary and the placement of a short final slice.
