spiritix / php-chrome-html2pdf

A PHP library for converting HTML to PDF using Google Chrome
MIT License
112 stars 29 forks source link

Converting HTML to PDF that contains PDF.js #41

Closed coxfrederic closed 2 years ago

coxfrederic commented 2 years ago

Hi,

I agree ... this solution will sound weird but believe me ... I need it for a project I'm working on so please bear with me :-)

I have a HTML page that contains some specific HTML tags and CSS to create a label, multiple labels on the page also. Works fine and I can convert them to PDF perfectly using this wonderful library.

However, now my use case is to also include external labels (which are PDF) and need to visually be in the same layout (think about 4 A6 labels on one A4 page) as the "HTML" internal labels.

The solution I came up with first was to embed the external PDF urls in an object tag or iframe, but that didn't work. So then I included PDF.js to render the external PDF's, and when the entire page was rendered I want to convert it to PDF.

This works in "html" mode in my browser, but when I convert using this library the PDF.js does not load the PDF document.

Any idea why? I'm clueless.

I am able to access the pdfjsLib but the promise for loading the document does not work.

I also tried setting waitFor to 15000ms but even then it does not work.

Any pointers?

Thanks

spiritix commented 2 years ago

Hi @coxfrederic, Unfortunately, puppeteer doesn't have a PDF renderer included (https://github.com/puppeteer/puppeteer/issues/2313). I would convert your HTML/CSS to PDF first using this library. Then I would convert the PDF labels to images and place them on the original PDF using some scripts like here. Hope this helps.

coxfrederic commented 2 years ago

Hi @spiritix Thanks for your quick reply.

I brings me to an idea, which for my use case might be simpler. To convert the external labels from PDF to image using Imagick for example, and then they can be rendered in HTMLtoPDF solution.

Thanks a lot !!