Tuesday, June 22, 2010

Install html2ps/pdf library to create PDF files using php

SkyHi @ Tuesday, June 22, 2010

Generating PDF or postscript files on the fly with php is quite easy using a library called html2ps/pdf. Following are the steps which I took to install it on a fedora 7 server running php 5.x

As per the documentation, html2ps/pdf requires either Ghostscript or PDFLIB libraries for fast pdf generation. You can also use the inbuilt FPdf routines but that’s much slower. PDFLIB is non-free library which may requires you to have a license to use, therefore, I opted for GhostScript here.

Step 1. Check whether ghostscript is already installed or not:

# which gs

/usr/bin/which: no gs in (/usr/kerberos/sbin:/usr/kerberos/bin: //sbin://bin: /usr/local/sbin: /usr/local/bin:/sbin: /bin:/usr/sbin:/usr/bin:/root/bin)

as you can see above, gs binary is not installed. if its available in your system then jump to step 3 else proceed to its installation in step 2.

Step 2. Download, compile and install ghostscript library:

# cd /usr/src

# wget http://mirror.cs.wisc.edu/pub/mirrors/ghost/GPL/gs863/ghostscript-8.63.tar.gz

# tar xzf ghostscript-8.63.tar.gz

# cd ghostscript-8.63

# ./configure

# make

# make install

you can verify installation by issuing:

# which gs


Step 3. download and unzip the html2ps/pdf library:

# cd /usr/src

# wget http://www.tufat.com/files_lgpl/script_19.zip

# unzip script_19.zip

# cd html2ps_v2042/

you will find a public_html directory here having php files and related directories. They needs to be transferred to some other locations where Apache can access them. let’s store in /var/www/html/html2ps directory. (I assume /var/www/html is your DocumentRoot here).

# mkdir /var/www/html/html2ps

# mv public_html/* /var/www/html/html2ps

# cd /var/www/html/

change ownership to apache:

# chown apache:apache html2ps/ -R

Step 4. Update html2ps/pdf library config file to find out ghostscript binary for pdf creation.

# cd /var/www/html/html2ps

# vim config.inc.php

//update line where path of Ghostscript executable required, in my case it’s in line 27.


save and exit.

Library is installed, you should be able to use it using http://localhost/html2ps/index.php URL now.