Skip to content

Commit

Permalink
Break pages up when long enough on tally report (#5290)
Browse files Browse the repository at this point in the history
* break pages up when long enough on tally report

* add test for max length print

* address pr comments
  • Loading branch information
carolinemodic authored Aug 22, 2024
1 parent 42d5279 commit 4640b66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 15 additions & 0 deletions libs/printing/src/render.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ test('page can be longer than letter when using LetterRoll', async () => {
});
});

test('page can not be longer than 100 inches when using LetterRoll', async () => {
const outputPath = tmpNameSync();
const pdfData = await renderToPdf({
document: <ManyHeadings count={500} />,
outputPath,
paperDimensions: PAPER_DIMENSIONS.LetterRoll,
});

const pdf = await parsePdf(pdfData);
expect(pdf.numPages).toEqual(3);
const { height, width } = (await pdf.getPage(1)).getViewport({ scale: 1 });
expect(width * PDF_SCALING).toEqual(1700); // letter
expect(height * PDF_SCALING).toEqual(20000); // maximum length
});

test('bmd 150 page is 13.25"', async () => {
const outputPath = tmpNameSync();
const pdfData = await renderToPdf({
Expand Down
18 changes: 12 additions & 6 deletions libs/printing/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const PAPER_DIMENSIONS = {
// their own, followed by a mostly blank line. This causes stripes in the printed page.
Bmd150: { width: 7.975, height: 13.25 },
Letter: { width: 8.5, height: 11 },
LetterRoll: { width: 8.5, height: Infinity },
LetterRoll: { width: 8.5, height: 100 }, // If we make the height infinite the canvas conversion to an image can seg fault. Break into pages beyond 100 inches.
} satisfies Record<string, PaperDimensions>;

export interface MarginDimensions {
Expand Down Expand Up @@ -112,16 +112,13 @@ export async function renderToPdf(
// be in the PDF, which allows us to determine the necessary height to fit
// the page to the content. viewport height here is irrelevant, but we have to
// set something.
const viewportHeight =
// Viewport height can't be infinity
height === Infinity ? PAPER_DIMENSIONS.Letter.height : height;
await page.setViewportSize({
// Noninteger values are not supported
width: Math.floor(
(width - horizontalMargin) * PLAYWRIGHT_PIXELS_PER_INCH
),
height: Math.floor(
(viewportHeight - verticalMargin) * PLAYWRIGHT_PIXELS_PER_INCH
(height - verticalMargin) * PLAYWRIGHT_PIXELS_PER_INCH
),
});

Expand Down Expand Up @@ -175,15 +172,24 @@ export async function renderToPdf(
waitUntil: 'load',
});

const isLetterRoll = height === PAPER_DIMENSIONS.LetterRoll.height;

const contentHeight =
(await getContentHeight(page)) / PLAYWRIGHT_PIXELS_PER_INCH +
verticalMargin;

buffers.push(
await page.pdf({
path: outputPath,
width: inchesToText(width),
height: inchesToText(
height === Infinity ? Math.max(viewportHeight, contentHeight) : height
/* if printing on a roll remove any unneeded height but never be smaller then a standard page */
isLetterRoll
? Math.min(
Math.max(contentHeight, PAPER_DIMENSIONS.Letter.height),
height
)
: height
),
margin: {
top: inchesToText(marginDimensions.top),
Expand Down

0 comments on commit 4640b66

Please sign in to comment.