pdf-lib
pdf-lib copied to clipboard
Page embed not working properly
What were you trying to do?
I need to embed printing in documents. To do this, I create a separate document with a print page in order to later embed it in many pages. In the end, I only have a frame, no text, and the text is very important to me
How did you attempt to do it?
I do this through a separate page, because I need to rotate the embed page with a print, which also needs to be rotated in different documents. I use a separate page as a separate layer.
What actually happened?
no text appears
What did you expect to happen?
appearance of a full page
How can we reproduce the issue?
const { PDFDocument, StandardFonts, PDFFont, degrees, rgb, PDFNumber, PDFPage } = require('pdf-lib'); const fs = require("fs"); const fontkit = require("@pdf-lib/fontkit");
const rbgWrapper = (red,green,blue) => rgb(red/(255/100)/100, green/(255/100)/100, blue/(255/100)/100);
// CONSTANTS const STAMP_COLOR = [85,0,171];
// Method for create PDF with stamp const createStampDoc = async () => { const newPDF = await PDFDocument.create(); const width = 250;
// Creating page for stamp
const page = newPDF.addPage([width, 100]);
const drawStamp = (page, x, y) => {
let curY = y;
curY+=10;
page.moveTo(x, curY);
page.drawText( "TEXT ONE", {
size: 9,
color: rbgWrapper(...STAMP_COLOR)
});
curY+=10;
page.moveTo(x, curY);
page.drawText( "TEXT TWO", {
size: 9,
color: rbgWrapper(...STAMP_COLOR)
});
// Border of stamp
const height = curY-y+10;
page.drawRectangle({
x: x,
y: y,
width: width,
height: height,
borderColor: rbgWrapper(...STAMP_COLOR),
borderWidth: 1,
opacity:0.3,
color: rbgWrapper(255,255,255)
});
page.setHeight(height);
return {width, height};
};
drawStamp(page, 0, 0);
return newPDF;
};
async function main() { // Creating new main PDF file const mainPDF = await PDFDocument.create(); mainPDF.addPage([350,400]); mainPDF.addPage([350,400]);
const stampPDF = await createStampDoc();
// embedding newPDF to mainPDF (for use drawPage)
const embeddedPage = await mainPDF.embedPage(stampPDF.getPage(0));
// In each page embedding stamp page
const pages = mainPDF.getPages();
for (const page of pages) {
await page.drawPage(embeddedPage, {x: 100, y: 100, rotate: degrees(0), opacity: 1});
}
// TODO: it working!! but embedding not working!
// Adding newPDF to mainPDF (as separated page)
const [copiedStampPage] = await mainPDF.copyPages(stampPDF, [0]); // Embed 0 page from stampDoc in pdfDoc
mainPDF.addPage(copiedStampPage);
const binaryFileWithStamp = await mainPDF.save();
fs.writeFileSync("stamp.pdf", Buffer.from(binaryFileWithStamp) );
// pdfDoc.saveAsBase64
// fs.writeFileSync("new23.pdf", pdfBytes, {encoding: "utf-8"});
}
main();
Version
^1.17.1
What environment are you running pdf-lib in?
Node
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
No response