|
|
PGTS Humble BlogThread: Tips/Tricks For Programming etc |
|
![]() |
Gerry Patterson. The world's most humble blogger |
Edited and endorsed by PGTS, Home of the world's most humble blogger | |
| |
Converting HTML to PDF. |
|
Chronogical Blog Entries:
|
|
| |
Date: Sat, 21 Jan 2012 20:47:18 +1100PDF has become a de-facto standard for transporting invoices, statements and other common documents which used to be transported by ordinary mail. But getting a convenient open source solution solution for producing PDF files can be difficult |
I have discovered that the easiest way to create PDF documents is to create the initial document as HTML. There are, of course, a large number of packages and programs to create HTML, and I have developed numerous scripts which generate HTML. Having created the HTML I then convert it to PDF.
You need the packages that will help with the conversion. These are the html2ps utility which converts HTML to postscript and the ps2pdf utility from the ghostscript package.
In Ubuntu, you can get them with this command:
sudo apt-get install gs html2ps
First create your HTML. for example, you might have a perl CGI script which reads a tab-delimited file and creates an invoice. Let's suppose your invoice is called /data/html/0000999.html. Assuming that your html will display ok (i.e. paths etc have been setup correctly), You can easily create your PDF file with the following commands:
html2ps /data/html/0000999.html > /data/html/0000999.ps ps2pdf /data/html/0000999.ps
This will create the file /data/html/0000999.pdf in the output folder.
You can add page numbers with the -n option.
Note: The script html2ps is written in perl.