PHP PDF Library: tcPDF vs mPDF

Posted on 1 Dec 2014 by Oleksii Raiu
Recently, I needed to create a simple Drupal-based solution, that would include mailing a PDF printout of a node to the client. My study of the available free PHP PDF libraries has yielded results - Google found about a dozen of them. After some research, these have been narrowed to just 2 - TCPDF and mPDF. After some more research and playing with the two, I ended up with the mPDF as the library of choice. And the reason, plainly - better HTML/CSS support.


TCPDF Library seems to be very well known. I guess, this means some very wide use? It can even be installed via the Ubuntu official repos. It’s weight is up to 30 Megs (28.5 Megs, in my case), which is large, but not too large. One strong benefit of TCPDF is it’s lots of code samples. You can really go and see every operator, every parameter spelled out. Another strong side of TCPDF is that it appears to have a powerful control over the page and content formatting, as well as page margins, page headers and footers, and pagination settings. So, why was TCPDF not chosen in my case? Because I wanted a tool to convert HTML/CSS to a presentable PDF document, rather than fight with pagination, headers, footers, and margins. And fight I did. I did not want page headers and footers in the first place, and I noticed, that it was taking a bit too long trying to do a decent page with ‘normal’ decent looking margins and pagination. When it came together, the default settings of TCPDF appeared to be quite weak, and the more I dug into it, the more I understood, that I would have to spend some time configuring the page on my own. However, the real issue that made me switch to mPDF was that after I had fought my way through, I discovered, that it was failing to delivered the formatting I wanted via HTML/CSS. With all that complex page formatting functionality, it failed where I needed it most. The general syntax of TCPDF is close to that of mPDF:

$html = drupal_render(node_view($node));

// Set document information.
$pdf->SetAuthor('ACWM Server');
$pdf->SetTitle('Program: ' . $node->title);
$pdf->SetSubject('ACWM Program Excerpt');

// Set margins.
$pdf->SetMargins(10, 15); // Left, top
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// Write data.
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output(‘filename.pdf’, 'F');


mPDF is a competitor of TCPDF, that I actually end up going with. It is also a known library, with lots of samples and a whole library of documentation. These are it’s strong sides, that it shared with TCPDF. Oh, and it allows deep page customization, margins, headers, footer, etc. as well - it just does not make you fight the defaults. mPDF weights about 30 Megs sharply, so, it’s only 1.5 Megs heavier than TCPDF. Maybe, these 1.5 Megs account for all that better CSS support? I would like to think so. If you read mPDF documentation closer, however, you will see, that it has some strong functionality to format pages as well - except that you don’t need to know that to write a simple document. Good strong initial presets is what set mPDF apart - it’s “killer feature” that TCPDF lacked. I did not have to disable the header and footer, fight with margins and pagination - because mPDF has good solid defaults about these things. The second “killer feature” of mPDF that “brought me in” was the CSS support. mPDF’s CSS support is not ideal, but it’s insanely more mature than that of TCPDF. The difference in CSS support was so big, that I even removed it from the TCPDF sample. The mPDF code looks something like this:
= new mPDF('utf-8', 'A4');
$css = addStyle();
$html = drupal_render(node_view($node));

$pdf->WriteHTML($css ,1);
$pdf->WriteHTML($html, 2);
$pdf->Output(‘filename.pdf’, 'F');
As you can see, with healthier defaults, mPDF not only allowed me to add CSS (the addStyle() function is a custom function returning the CSS code), but it also took much less effort to do the whole thing.

General notes

I was disappointed at the superficial CSS support the current libraries have. I guess, this is due to the fact, that PDF does not support HTML, and those libraries end up trying to interpret the HTML / CSS to deliver the output as they can. There is actually another option, that I seem to have spotted too late, and that seems like a potential good choice for the task: DOMPDF. From a short glance, I see that it is also limited CSS-wise, but it may actually be stronger than mPDF. Maybe some of the readers of this article can comment. Have had experience with PHP PDF libraries? Know another library that I missed that deserved my attention? Another library saved your day in my place? Feel free to comment and share your perspective!