Tag Archives: pdf

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

How to Install Foxit PDF Reader 1.1 in Ubuntu Karmic Koala CodeUnit 12 MAY 2010

Foxit Reader 1.1 is an incredible little PDF viewer that features some powerful tools while remaining pretty damn quick. It’s free for non-commercial usage and features most of the viewing tools you’ve come to expect from the big daddy of PDF readers, namely Adobe Acrobat. Among these tools included in Foxit is the standard zoom functionality, full document navigation, bookmarks, thumbnails, text selection, snapshot image grabbing as well as the old faithful, full screen document viewing.

The download .deb package weighs in at a handy 3.6 MB and when running, has a claimed memory consumption that remains less than 15 MB. It launches instantly when called and can load a PDF document in under 3 seconds.

And installing it in Ubuntu is an absolute breeze.

First, download the .deb package of Foxit’s official download page at http://www.foxitsoftware.com/pdf/desklinux/download.html. Once it is downloaded to the appropriate folder on your system, install it using the dpkg Debian package manager. To do this run a terminal in administrator mode and call:

sudo dpkg -i FoxitReader_1.1.0_i386.deb

And that’s it really. Foxit will be installed on your system and you’ll see the menu shortcut being added under the Office tab on the Applications main menu. It is also added as a right-click context menu selection for PDF files. Couldn’t be simpler and works like an absolute charm! :)

(Oh, and finally, for the future when you might find something even better, you can uninstall it in the usual way using: dpkg -r FoxitReader)

Related Link: http://www.foxitsoftware.com/pdf/desklinux/index.html

How to Convert a Webpage to a PDF CodeUnit 15 FEB 2010

Saving a web page to a PDF file for later offline viewing has just become a whole lot simpler thanks to the excellent pdfmyurl.com web service that aims to convert any URL you feed it into a downloadable PDF file. The application is particularly easy to use, looks good and returns some excellent results, making it the perfect tool for a beginner web user.

However, its magic doesn’t quite just end there.

Equipped with a host of settable options like layout, page size, dpi and zoom factor for example, pdfmyurl is geared towards allowing other developers to use the service either by making calls to it via an href anchor with the associated GET variables set, or even on their own servers if for instance they wanted to make use of the brilliant wgets functionality in order to rip a website down into PDF format.

For example:

wget -O opentracker.pdf "pdfmyurl.com?url=www.opentracker.net&-O=Landscape&--header-left=hello"

would result in the system generating a pdf file entitled opentracker.pdf in a landscape orientation and with a custom header text.

Particularly interesting for this system is the way it manages to keep menus and links active in the generated PDF file, meaning that should the page you saved as a PDF reference another article, you could still access that referenced article at a later stage by clicking on the link contained in the PDF file.

In other words, this whole damn system is simply put, pretty nifty! ;)

Related Link: http://www.pdfmyurl.com