fastify-xpdf
extractPages
Extract specific pages from a PDF into a new document using pdf-lib. Pages are zero-indexed.
extractPages
Extract specific pages from a PDF into a new document using pdf-lib. Pages are zero-indexed (page 1 = index 0).
Signature
fastify.xPDF.extractPages(
pdfBuffer: Buffer,
pageIndices: number[],
options?: ExtractOptions,
): Promise<MergeResult>
interface ExtractOptions {
filename?: string
saveToStorage?: boolean
folder?: string
}
// Returns same shape as mergePDFs
interface MergeResult {
buffer: Buffer
filename: string
size: number
pageCount: number // number of pages extracted
storageKey?: string
url?: string
}
Params
| Name | Type | Required | Description |
|---|---|---|---|
pdfBuffer | Buffer | Yes | Source PDF buffer |
pageIndices | number[] | Yes | Zero-based page indices to extract, in desired output order |
options.filename | string | No | Output filename |
options.saveToStorage | boolean | No | Upload result to xStorage |
options.folder | string | No | Storage folder override |
Returns
MergeResult — includes pageCount (number of pages in the extracted document).
Throws
| Error | When |
|---|---|
[xPDF] Invalid PDF buffer | pdfBuffer doesn't start with %PDF |
[xPDF] pageIndices must be a non-empty array... | pageIndices is empty, null, or not an array |
[xPDF] Each page index must be a non-negative integer | Any index is not a non-negative integer |
[xPDF] Page index N out of range... | Any index ≥ document page count |
Examples
Extract the first and last pages of a contract
fastify.get("/contracts/:id/summary", async (request, reply) => {
const pdfBuffer = await fetchContractBuffer(request.params.id);
const count = await fastify.xPDF.getPageCount(pdfBuffer);
// Extract page 1 (index 0) and the last page
const { buffer } = await fastify.xPDF.extractPages(
pdfBuffer,
[0, count - 1],
{ filename: `contract-${request.params.id}-summary.pdf` },
);
return reply
.header("Content-Type", "application/pdf")
.header("Content-Disposition", `attachment; filename="summary.pdf"`)
.send(buffer);
});
Reorder pages — build a custom page order
// Original: pages 1-5 (indices 0-4)
// Desired output order: page 3, page 1, page 5 (indices 2, 0, 4)
const { buffer, pageCount } = await fastify.xPDF.extractPages(
sourcePdf,
[2, 0, 4],
);
console.log(pageCount); // 3
See Also
- getPageCount / getMetadata — Get page count before extracting
- mergePDFs — Merge extracted sections into a final document
AI Context
package: "@xenterprises/fastify-xpdf"
method: fastify.xPDF.extractPages(pdfBuffer, pageIndices, options?)
use-when: Extract specific pages from a PDF into a new document using pdf-lib (0-indexed)
params: pdfBuffer (Buffer), pageIndices (number[]), options ({ saveToStorage })
returns: { buffer, size, pageCount }
fastify-xpdf
Fastify 5 plugin for PDF generation and manipulation — HTML/Markdown/URL to PDF via Puppeteer, form filling, merging, page extraction, and metadata via pdf-lib, with optional S3 storage.
fillForm
Fill PDF form fields (text, checkbox, radio, dropdown) using pdf-lib and optionally flatten the result.
