Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDF.php is ~20 times smaller filesize than TCPDF.php? Why?

I know there have been a lot of posts about these two but figured I'd address a glaring question I have. A designer of ours recently sent me a few files with TCPDF already tied in because a friend of him said it was "better".

In the past we have used FPDF for everything PDF generation in PHP but right off the bat I noticed an enormous glaring difference:

Filesize of fpdf.php: 46KB

filesize of tcpdf.php: 996 KB

note: the file sizes above are of the actual php file, not the PDF's generated.

I don't really have too much patience to sit down and look at all of the differences between the two but it doesn't seem it is worth the switch really for the huge file difference. Most on SO seem to really like TCPDF but what gives?

Main Question

Why the difference in size and should I be worried for my server having to load a 1MB file hundreds or thousands of times a day versus a 50KB file that does nearly the same thing? I am NOT saying my PDF file larger here folks. The filesize of the PHP script itself is the 1MB to 40Kb.

like image 398
JM4 Avatar asked Sep 15 '11 19:09

JM4


People also ask

What is the difference between FPDF and Tcpdf?

TCPDF is based on FPDF and adds a few more methods, as you observed. I just checked my code and the methods I'm using in TCPDF that aren't available in FDPF are SetCellPadding , SetAlpha , WriteHTML and WriteHTMLCell .

What is FPDF used for?

FPDF is a PHP class which allows generating PDF files with PHP code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files.

What is FPDF library?

What is FPDF? FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.


2 Answers

I avoid TCPDF because of its unfriendly license (you must leave link + logo intact in generated PDF documents). (Note: it seems the license has changed and is now standard LGPLv3: http://www.tcpdf.org/license.php)

That said, the usual cause for larger file size is embedded fonts. You can specify fonts in several different ways:

  • specify them and do not embed them (smallest size, however, text might not display correctly)
  • embed them fully (FPDF already supports this)
  • embed just the parts of characters that are used

The first option produces smallest files - I guess this is what you use with FPDF. Note that your PDF might display differently on different systems.

The second option produces largest files. Since the font is there it is (in theory - I have no experience with this) possible to edit file and add text in the same font.

The third option is the one that should be used in most cases, however, it is the most difficult to implement in libraries and core FPDF does not support it (TFPDF however does). It only embeds glyphs that are used so it produces cross-platform PDFs which are quite small.

The third option was not supported with TCPDF a few years ago (however, this might have changed by now). As I mentioned, it is also not supported in core FPDF - however, it is supported in MPDF and TFPDF (which I have successfully used in many projects).

On a side note, another reason for me not using TCPDF was unfriendly and unhelping attitude of mr. Asani (developer) in contrast with FPDF / MPDF / TFPDF community (Oliver, Ian,...) help on FPDF forum. It took 2 weeks of correspondence on forum before he admitted that TCPDF does not support partial font embedding. However, it is the license that is a real deal-breaker to me.

So, to answer your question: you could make TCPDF produce smaller files by not embedding fonts. However the license should be the main reason for switching from it. :)

like image 73
johndodo Avatar answered Oct 13 '22 18:10

johndodo


i took an instant to compare both sources.

fpdf has almost no comments.

tcpdf has a few more methods but also has full blocks of phpDoc-like comments with explanations of every parameter and usage and examples in html format before every method and property. i'd say that's the main reason for the big file size.

like image 22
Einacio Avatar answered Oct 13 '22 17:10

Einacio