pdf-lib
pdf-lib copied to clipboard
drawPage of embedded pages produces non-functional hyperlinks
What were you trying to do?
I want to put two landscape orientated one-page PDFs of size A5 on one portrait orientated PDF of size A4.
(In our real world application we click through H5P course presentations and make PDF snapshots of them with playwright, which then are put into a resulting PDF in the way that is described in this bug report in simplified manner.)
How did you attempt to do it?
I create an empty PDFDocument object for the result and an empty Array to hold the embedded pages. Then I read both test files (test1.pdf + test2.pdf containing clickable links) into two PDFDocument objects and put them with .getpage(0) and embedPage into the resulting PDFDocument object. In the final step I add an empty page to the resulting PDFDocument with addPage and draw both embedded pages onto this added page with drawPage. Then the resulting PDFDocument is saved into the file named result.pdf.
(async () => {
const {PDFDocument, PageSizes} = require('pdf-lib');
const fs = require('fs');
const pdfDoc = await PDFDocument.create();
const embeddedPages = [];
let uint8Array = fs.readFileSync('./test1.pdf');
const pdfPageDoc1 = await PDFDocument.load(uint8Array);
embeddedPages.push(await pdfDoc.embedPage(pdfPageDoc1.getPage(0)));
uint8Array = fs.readFileSync('./test2.pdf');
const pdfPageDoc2 = await PDFDocument.load(uint8Array);
embeddedPages.push(await pdfDoc.embedPage(pdfPageDoc2.getPage(0)));
const newPage = pdfDoc.addPage(PageSizes.A4);
newPage.drawPage(embeddedPages[0], {x: 0, y: embeddedPages[0].height - 80});
newPage.drawPage(embeddedPages[1], {x: 0, y: 0});
fs.writeFileSync('result.pdf', await pdfDoc.save());
})();
What actually happened?
The two A5 PDFs were placed onto the resulting A4 PDF and it looked like expected. Only the hyperlinks in the result were not clickable.
What did you expect to happen?
I expected the hyperlinks to be clickable.
How can we reproduce the issue?
Unzip pdf_lib_test.zip
$ cd pdf_lib_test/
$ npm install
$ node app.js
Open the newly written file result.pdf
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
This issue was already reported in #606, #798 and #1155 ,but all these reports seem to lack of reproducable code and/or pdf examples. I hope I did it now the correct way.
Maybe it is the same root cause as in this pdf library: https://github.com/pymupdf/PyMuPDF/issues/2988