* Build a synthetic PDF with the given number of pages by copying * pages from sample.pdf. Each page gets unique text to simulate * real-world content variation.
(pageCount: number)
| 80 | * real-world content variation. |
| 81 | */ |
| 82 | async function buildSyntheticPdf(pageCount: number): Promise<Uint8Array> { |
| 83 | const sourceBytes = await loadFixture(mediumPdfPath); |
| 84 | const source = await PDF.load(sourceBytes); |
| 85 | const sourcePageCount = source.getPageCount(); |
| 86 | |
| 87 | // Start by copying the source pages |
| 88 | const pdf = await PDF.load(sourceBytes); |
| 89 | |
| 90 | // Copy pages from source repeatedly until we reach the target count |
| 91 | const pagesNeeded = pageCount - sourcePageCount; |
| 92 | |
| 93 | if (pagesNeeded > 0) { |
| 94 | // Build an array of source page indices to copy in bulk |
| 95 | const indices: number[] = []; |
| 96 | |
| 97 | for (let i = 0; i < pagesNeeded; i++) { |
| 98 | indices.push(i % sourcePageCount); |
| 99 | } |
| 100 | |
| 101 | await pdf.copyPagesFrom(source, indices); |
| 102 | } |
| 103 | |
| 104 | // Add unique text to each page so content varies |
| 105 | for (let i = 0; i < pdf.getPageCount(); i++) { |
| 106 | const page = pdf.getPage(i); |
| 107 | |
| 108 | if (page) { |
| 109 | page.drawText(`Page ${i + 1} of ${pageCount}`, { |
| 110 | x: 50, |
| 111 | y: 20, |
| 112 | font: "Helvetica", |
| 113 | size: 8, |
| 114 | }); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return pdf.save(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get or create a synthetic PDF cached to disk. |
no test coverage detected