Tag Archives: fpdf

FPDF: How to use a Degree Symbol in a Generated PDF Tips, Tricks and Tutorials 29 SEP 2017

I’ve been using the FPDF PDF generator library for years now as the de facto method for my PHP projects to produce PDF reports. However, one minor annoyance is that the generated PDF files often falter when it comes to the inclusion of certain special characters – like the degree symbol (°) as an example.  (Basically, something like °C becomes °C in the final document)

The reason for this happening is that Arial, the default used/included font, is of type ISO-8859-1 while the degree symbol is UTF-8 encoded. So in order for us to include special symbols or characters from other languages, we need to either try and convert them into our font compatible ISO-8859-1 format, or perhaps switch to using a different TrueType or Type1 font (which then would contain the desired character set).

Now while UTF-8 support is available via a modified class, the easiest way to fix the degree symbol issue without having doing any real work is to simply make use of the PHP utf8_decode function, which convert UTF-8 encoded strings to their ISO-8859-1 equivalents.

In other words outputting utf8_decode(“°C”) to your PDF should result in the expected °C

Related Link: FPDF PDF Generator Library

FPDF: Failing to Insert a Google Chart into a PDF CodeUnit 13 OCT 2010

fpdf-logoMy tried and trusted method for inserting graphs into PDFs is to use the nifty PHP FPDF library and insert an image into it via a Google Chart URL.

In other words, your code would look something like this:

$charturl = "http://chart.apis.google.com/chart?cht=lc&chco=" . implode(',', $colors) . "&chs=930x310&chd=t:" . implode('|', $graphdata) . "&chxt=x,y&chxl=0:|" . implode('|', $monthsheading) . "|1:|10|20|30|40|50|60|70|80|90|100";
$pdf->Image($charturl, null, null, 180, 60, 'PNG');

99% of the time though, my PDF fails, gives me an error about not being able to load the image and leaves me in tears.

Because each and every time, I can without fail go look at the PHP error log and will be greeted with this:

failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

And then I remember: URLENCODE the damn graph string before feeding it into the Image function!

One day I’ll remember to do this from the get go, promise! :)

Related Link: http://php.net/manual/en/function.urlencode.php

FPDF PHP PDF Generator: How to Wrap Long Lines of Text in Tables CodeUnit 13 AUG 2010

I have mentioned the useful FPDF PHP PDF generating library before, but today I’m quickly going to point out how you can solve the problem of inserting extra long lines of text into a table and forcing the table to automatically wrap or linebreak all of your text without simply chopping it off at the end when it reaches the right hand border of the cell.

The solution to this is pretty simple and it involves dropping the use of the Cell() function and instead incorporating the MultiCell() call.

The MultiCell basically allows for the printing of text with line breaks. These line breaks can be automatic, in other words when the text hits the right border of the cell, or explicit, i.e. via the n control character.

The function cleverly automatically lays down as many cells as possible, one below the other and all without any input or direction from you. The cell block can then be frames and even have its background painted if you want.

Parameters are pretty self-explanatory. You can set the width of the cells, the height of the cells, the text to print, whether or not you want a border, how you want the text aligned and whether or not you want the background filled.

Putting this in code, you would get a call that looks something like this:

$pdf->MultiCell(275,7,’Insert your long string here’,1,’L’,false);


Related Link: http://www.fpdf.org/en/doc/multicell.htm