MCPcopy Create free account
hub / github.com/LibPDF-js/core / buildSyntheticPdf

Function buildSyntheticPdf

benchmarks/fixtures.ts:82–119  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

80 * real-world content variation.
81 */
82async 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.

Callers 1

getOrCreateSyntheticFunction · 0.85

Calls 8

pushMethod · 0.80
copyPagesFromMethod · 0.80
drawTextMethod · 0.80
saveMethod · 0.80
loadFixtureFunction · 0.70
getPageCountMethod · 0.65
loadMethod · 0.45
getPageMethod · 0.45

Tested by

no test coverage detected