-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge PDF page number error #5
Comments
The description language in PDF format is very similar to HTML, with the only drawback being its lack of semantics,It describes the content of PDF pages through objects, such as the following example:
So many PDF parsing libraries cannot extract page numbers, and I cannot modify page numbers when merging PDFs. I have been thinking for a long time without a solution. However, there is an imperfect solution, which is to turn off page numbers when generating PDF, but leave room for page numbers and add them yourself. Here is an example: import { readFileSync, writeFileSync } from "node:fs";
import { PDFDocument, StandardFonts, rgb } from "pdf-lib";
const existingPdfBytes = readFileSync("./vitepress.dev.pdf");
const pdfDoc = await PDFDocument.load(existingPdfBytes);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const pages = pdfDoc.getPages();
const totalPages = pages.length;
for (let i = 0; i < totalPages; i++) {
const page = pages[i];
const { width } = page.getSize();
const text = `${i + 1} / ${totalPages}`;
const fontSize = 9;
const textX = width - 50;
const textY = fontSize;
page.drawText(text, {
x: textX,
y: textY + 5,
size: fontSize,
font: helveticaFont,
color: rgb(127 / 256, 127 / 256, 127 / 256),
});
}
const pdfBytes = await pdfDoc.save();
writeFileSync("pagination.pdf", pdfBytes); It's not perfect, but it's good. |
I know that Cairo at least supports page labels. Perhaps |
Question:
Page number is not merge, Is there any way to combine the page numbers or customize the page numbers, Thank you very much
Code:
The text was updated successfully, but these errors were encountered: