Friday, January 16, 2015

Create PDF with fixed header and footer in PHP or JavaScript

Solution 1:

TCPDF - one reason is Unicode support in tcpdf, which fpdf utterly lacks. tcpdf is also still actively maintained. and, it's also got a decent OO architecture. word to the wise: fpdf is basically an old thing that got superseded by tcpdf.

Solution 2:

MPDF is a PHP class which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF, with a number of enhancements.

I found mpdf better than tcpdf in terms of html rendering. It can parse css styles much better and create pdf that look very similar to the original html.

mpdf even supports css things like border-radius and gradient etc.

I am surprised to see why mpdf is so less talked about when it comes to html to pdf.

Check out the examples here

I found it useful for designing invoices, receipts and simple prints etc. However the website itself says that pdfs generated from mpdf tend to be larger in size.

Solution 3:

DOMPdf is the easiest way to make a pdf quickly. Like Mike said, feed it html and it outputs a pdf. Under the hood, it has the option to use either r&ospdf or pdflib as the rendering engine.

Solution 4:

html2ps is the best if you need the css and 3c compatibily.

Solution 5:

wkhtmltopdf - A great alternative for PDF generation is wkhtmltopdf. It has a smooth integration and the result is awesome. You will have to install the binary into your server, though. If you're on a VPS or dedicated, this will not be a problem.

There's a PHP lib for using it, you can find it here:

Also, there's a nice solution for the large table problem you have. Maybe it can help you with another lib too, take a look here:

Solution 6:

Print to the built-in "Microsoft XPS Document Writer" (the *.xps format) and use Apache FOP to covert it to PDF. Not a PHP-native solution, not very efficient either, but it works well even if you need to generate PDF with very complex layouts.

Solution 7:

The Zend Framework's Zend_Pdf is really good too. It's on par with pdflib in terms of control of output and complexity and is more portable because its a pure php solution. That said, its slower and uses more memory than pdflib. Pecl modules are always more efficient than a php solution.

Solution 8:

Phantomjs now supports pdf output. The problem with various libraries (PDFlib, FPDF etc.) is the rather long learning curve. PhantomJS uses html5+css3+svg+canvas, which have a long history and large user space and php people know html+css usually very well, so the learning curve with PhantomJS is short.

Solution 9:

If you include commercial, I've heard good things about Prince.

Solution 10:

PECL pdflib - Creating PDF on the fly with the PDFlib library

Solution 11:

PDF.js - A general-purpose, web standards-based platform for parsing and rendering PDFs.

Solution 12:

PHPExcel - Project providing a set of classes for the PHP programming language, which allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML, ... This project is built around Microsoft's OpenXML standard and PHP.


No comments: