r/PHPhelp • u/Fair-Average-2126 • Feb 08 '23
Solved pdf generation
I have requirements to generate pdf with more than 30 pages dynamically based on the user query. The pdf not contains much images only text in the form table. Planning to wkhtmltopdf library for this. Just wanted to know it's affect the execution time Or memory. Anyone help me to understand this.
6
u/kAlvaro Feb 08 '23
Running a headless browser to render HTML is a resource intensive task. If you only need to generate simple documents, you're better off using a tool that generates PDF directly. In the old days we used FPDF and its successors (TCPDF was the most popular). Both seem to have recent releases. There's also mPDF , that seems to be another child of FPDF.
7
u/ZippyTheWonderSnail Feb 08 '23
I'm currently using mPDF for large reports including tables, graphs, and complex formatting.
It does a good job of translating HTML documents into PDFs. There are a number of CSS gotchas, image requirements, and font limitations, but the learning curve is fairly small.
When it comes to server load, mPDF here is my experience:
It renders HTML with little overhead. It is picky, though. Any invalid HTML and it errors out.
Keep the CSS simple. Use element ID's and global classes for formatting. Complex CSS structures will spin that server load way up.
In the same vein, all images should be linked using
<img src>format. Rendering inline SVGs is a disaster you don't want to be a part of.In my experience, a 100 page PDF based on an HTML page with tables and images can be rendered out in less than 5 seconds.
1
u/kAlvaro Feb 09 '23
Looks great. And it's probably even faster if you can manage to skip HTML altogether.
2
2
u/BinBashBuddy Feb 08 '23
I use tecnickcom/tcpdf, and we generate pdfs every day all day long from purchase orders to invoices to technical documents (which do include many images). The current version is at a dead end while they get a major update in place (noted on github) but works great for us.
2
u/codeWithSusan Feb 08 '23
If you find it's taking a lot of time to execute you could outsource the job to a queue and notify the user via email when their download is ready.
Info about queues: https://geekflare.com/queuing-systems-for-backend-developers/
1
u/eurosat7 Feb 08 '23
If we need something simple and fast and tame on resources we go with wkhtml2pdf. (the qt patched version where we have have a separate header and footer on each page)
If we want something fancy and complex we run a headless webdriver with firefox or chromium. That bling is expensive though.
1
u/DiskoPete Feb 08 '23
We have created this service which also can convert html to pdf. https://www.websnap.app
Here you can find the package: https://packagist.org/packages/websnap/php
6
u/ardicli2000 Feb 08 '23
I use dompdf and it works pretty fine.