pdf-lib
pdf-lib copied to clipboard
Copy two different pages and add them in a document.
Hello,
I am currently trying to copy two different pages (newPage1 & newPage2) from two different documents (pdfDoc2 & pdfDoc3) and then add them to a document (pdfDoc1) using the .addPage() method. However only the first page (newPage1) is added twice and the third one (newPage2) never.
const {PDFDocument} = PDFLib;
const urlPage1Bytes = await fetch(urlPage1).then((res) => res.arrayBuffer());
let pdfDoc1 = await PDFDocument.load(urlPage1Bytes);
const urlPage2Bytes = await fetch(urlPage2).then((res) => res.arrayBuffer());
let pdfDoc2 = await PDFDocument.load(urlPage2Bytes);
const urlPage3Bytes = await fetch(urlPage3).then((res) => res.arrayBuffer());
let pdfDoc3 = await PDFDocument.load(urlPage3Bytes);
let [newPage1] = await pdfDoc1.copyPages(pdfDoc2, [0]);
let [newPage2] = await pdfDoc1.copyPages(pdfDoc3, [0]);
pdfDoc1.addPage(pdfDoc2)
pdfDoc1.addPage(pdfDoc3)
Does anyone have any idea of the reason for this behavior?
Thank you very much 😃
Hello @antoine-numerized! At first glance your code looks fine. Can you share some sample PDFs I can use to reproduce the issue?
Hello @Hopding,
Yes, you can find the main file and the add-on file here 👇. pdfDoc1 -> document-1-3.pdf pdfDoc2/3 -> document-tab-1.pdf
What I do more precisely in my application is that my pdfDoc2 and pdfDoc3 are the same basic files (document-tab-1.pdf) but I modify them in different ways by adding different texts in the tables by accessing the form (.getForm) of each document, then the elements of this document (.getTextField) then I modify (.setText).
And here it is pdfDoc2 which is doubled because it is added first (.addPage). I think that this has nothing to do with the modification of text in the file because when I reverse the order of addition it is pdfDoc3 which is doubled.
Thank you very much 😃
I am facing the same problem. Apparently, something not are be awaiting, because if I do a few things before write on the pc, it's works...