Mass creation of template-based pdfs in PHP using pdftk

I am doing bulk template-based pdf generation and I quickly ran into big performance issues. My current scenario is as follows:

  • Get data to fill out from db
  • create fdf based on one line of data and pdf form
  • write the .fdf file to disk
  • merge a PDF file with fdf using pdftk (fill_form using the flatten command)
  • continue iterating through the lines until all .pdf are created
  • all generated files are combined together at the end and one PDF file is provided to the client

I use passthru to give raw output to the client (saves a time record file), but this is only a slight performance improvement. The total run time is about 50 seconds for 200 records, and I would like to go down to at least 10 seconds.

An ideal script will work with all these pdf files in memory and not write any of them to split the file, but then the output will be impossible, since I cannot transfer such data to an external tool like pdftk. Another idea was to generate one large .fdf file with all these lines, but it looks like it is forbidden.

Did I miss something very trivial here?

I am grateful for any advice.

PS. I know that I can use some good library, for example pdflib, but now I am considering only open licensed libraries.

EDIT:

I guess that the syntax for creating the .fdf file with multiple pages, using the same pdf file as the template, spent several hours and could not find good documentation.

+7
source share
3 answers

After you have been facing the same problem for a long time (you wanted to create my pdf files based on LaTeX), I finally decided to switch to another rough but effective technique:

I create my pdf files in two stages: first I generate html with a template engine, for example twig or smart guy. second i use mpdf to generate pdf files from it. I tried many other html2pdf frameworks and ended up using mpdf, it is very mature and has been developed for a long time (frequent updates, rich functionality). advantage of using this method: you can use css to develop your documents (mpdf fully supports css), which happens along with the advantages of css (http://www.csszengarden.com) and it is very easy to generate dynamic tables.

Mpdf parses html tables and looks for theader, tfooter element and places it on each page if your tables are larger than one page size. You also have the option of defining page header and footer elements with dynamic objects such as page nr, etc.

I know using this bypass seems to be a workaround, but to be honest, not a single latex, not a single PDF engine will be as strong and simple as html!

+1
source

Try another less complex library like fpdf ( http://www.fpdf.org/ )

I find it nice and easy.

Always find small libraries and do what you need.

The larger the library, the more resources it consumes.

0
source

This will not help your problem with multiple pages, but I notice that pdftk accepts a character - to mean "read from standard input".

You can send .fdf to the pdftk process via stdin to avoid having to write them to disk.

0
source

All Articles