A
A
Aljo2022-04-09 09:08:27
PHP
Aljo, 2022-04-09 09:08:27

How to generate beautiful PDF using PHP?

Hello!

The user fills out a questionnaire on the site and after completing the questionnaire, a PDF is generated using the FPDF + html2PDF library + the WriteTag plugin .
The problem is that some unnecessary indents appear, then the font seems to be specified explicitly, but changes in PDF to another. In short, the option is far from presentable. And they suffered a lot with him.

I would like to know who has a lot of experience, which library is better to use? Or maybe there are easier and better options? For example, I was offered to generate a PDF in the front from a page screen using jsPDF

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
ThunderCat, 2022-04-09
@aljo222

TL;DR: If you need an exact match with the html template - only headless chrome, the rest is quite different from the original, due to a number of problems.
I faced exactly the same task a couple of times, there are actually only 2 normal options:
generate on the front through html2pdf, or similar libraries working through canvas,
or on the back through headless chrome.
Naturally, both with their own nuances. In the first case, the downside is that the PDF will essentially be a set of screenshots, with all the consequences, such as different screen sizes and the inability to select, for example, text in the finished PDF.
In the second, you need to put the chrome headless on the server and, accordingly, give it the generated html from the back, which brings its own difficulties and jokes, but 99.9% matches what you send to print from chrome, and debugging and adjusting the html to the format in much easier and more convenient. In addition, the use of styles and media queries for printing opens up incredible flexibility in terms of working with ready-made templates (auto-numbering, margins, headers and footers, etc). Oh, and I forgot the killer feature - it performs zhs, that is, if there are graphics or some elements displayed through zhs - then only chrome, there are simply no other options.
As for other libraries (mpdf, FPDF, wkhtmltopdf) - they all "float" a lot, unlike chrome, many things are simply impossible in them, besides, some have chronic problems with fonts and encodings... Perhaps in the last 3 years since since I tried them something has changed, but I think some of the problems still remain.

A
Anton, 2022-04-09
@sHinE

Another option is wkhtmltox (wkhtmltopdf) and php wrappers for it. In fact, this is a webkit engine that generates pdf. He is given html as an input - accordingly, almost everything from html + css can be used there. There are subtleties, for example, with multi-page tables, but I recommend trying.

A
Alexander Toropov, 2022-04-14
@nefone

Use libraries tailored to work with PDF from PHP:
FPDF

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question