Converting PDF Files using PHP By Idris Winarno idris@eepis-its
14 Slides112.50 KB
Converting PDF Files using PHP By Idris Winarno [email protected]
Tools FPDF PDFlib HTML ToPDF
FPDF Install your apache2 and PHP5 – # apt-get install apache2 php5 Install fpdf – # apt-get install fpdf Intergrate your fpdf into your php – ln -s /usr/share/fpdf /var/www/fpdf
Testing FPDF (1) ?php require('fpdf.php'); pdf new FPDF('P','mm','A4'); pdf- AddPage(); pdf- SetFont('Arial','B',16); pdf- Cell(40,10,'Hello World!'); pdf- Output(); ?
Testing FPDF (2) ?php require('fpdf.php'); class PDF extends FPDF { //Page header function Header() { //Logo this- Image('logo pb.png',10,8,33); //Arial bold 15 this- SetFont('Arial','B',15); //Move to the right this- Cell(80); //Title this- Cell(30,10,'Title',1,0,'C'); //Line break this- Ln(20); } //Page footer function Footer() { //Position at 1.5 cm from bottom this- SetY(-15); //Arial italic 8 this- SetFont('Arial','I',8); //Page number this- Cell(0,10,'Page '. this- PageNo().'/{nb}',0,0,'C'); } } //Instanciation of inherited class pdf new PDF(); pdf- AliasNbPages(); pdf- AddPage(); pdf- SetFont('Times','',12); for( i 1; i 40; i ) pdf- Cell(0,10,'Printing line number '. i,0,1); pdf- Output(); ?
PDFLib Installing PDFLib – apt-get install libapache2-mod-php4 libapache2-mod-php4 – apt-get install php4-dev php5-dev php4-pear php5-pear – pear install pdflib --with-pdflib /usr/local Adding new extension into php.ini – echo "extension pdf.so" /etc/php4/conf.d/pdf lib.ini – echo "extension pdf.so" /etc/php5/conf.d/pdf lib.ini Checking yout config – apache2ctl configtest Syntax OK Restart your apache – # /etc/init.d/apache2 restart
Alternative install of PDFLib # cd /usr/src/ # wget http://www.pdflib.com/binaries/PDFlib/704/PDFlib-Lite-7.0.4p4.tar.gz Or # wget http://lecturer.eepis-its.edu/ idris/files/aplikasi web/PDFlib-Lite7.0.4p4.tar.gz # tar xvf PDFlib-Lite-7.0.4p4.tar.gz # cd PDFlib-Lite-7.0.4p4 # ./configure # make # make install # pecl install pdflib # install dir /usr/local Adding pdflib.so into your php.ini # /etc/init.d/apache2 restart
PDFLib phpinfo() PDF Support enabled PDFlib GmbH Version 4.0.1 Revision Revision: 1.8 Pdflib 4.0.1 installation ok
PDFLib Example ?php p PDF new(); /* open new PDF file; insert a file name to create the PDF on disk */ if (PDF begin document( p, "", "") 0) { die("Error: " . PDF get errmsg( p)); } PDF set info( p, "Creator", "hello.php"); PDF set info( p, "Author", "Rainer Schaaf"); PDF set info( p, "Title", "Hello world (PHP)!"); PDF setfont( p, font, 24.0); PDF set text pos( p, 50, 700); PDF show( p, "Hello world!"); PDF continue text( p, "(says PHP)"); PDF end page ext( p, ""); PDF end document( p, ""); buf PDF get buffer( p); len strlen( buf); PDF begin page ext( p, 595, 842, ""); header("Content-type: application/pdf"); header("Content-Length: len"); header("Content-Disposition: inline; filename hello.pdf"); print buf; font PDF load font( p, "Helvetica-Bold", "winansi", ""); PDF delete( p); ?
HTML ToPDF Download source – # cd /usr/src – #wget http://www.rustyparts.com/scripts/HTML ToPDF3.5.tar.gz or – wget http://lecturer.eepis-its.edu/ idris/files/aplikasi web/HTML ToPDF-3.5.tar.gz Make symbolic link into /var/www – # ln -s /usr/src/HTML ToPDF-3.5 /var/www/html2pdf Installaing html2ps & ps2pdf – # apt-get install html2ps – # apt-get install texlive-latex-recommended
Creating HTML This HTML code will be converted into PDF Make your own HTML code
HTML ToPDF Test ?php /** Id: example1.php 2426 2006-11-18 19:59:26Z jrust */ /** * The simplest example. We convert an HTML file into a PDF file. * We also add a few custom headers/footers to the PDF. */ ? html head title Testing HTML ToPDF /title /head body Creating the PDF from local HTML file. Note that we customize the headers and footers! br / ?php // Require the class require once dirname( FILE ) . '/./HTML ToPDF.php'; // Full path to the file to be converted htmlFile dirname( FILE ) . '/test.html'; // The default domain for images that use a relative path // (you'll need to change the paths in the test.html page // to an image on your server) defaultDomain 'www.rustyparts.com'; // Full path to the PDF we are creating // Full path to the PDF we are creating pdfFile dirname( FILE ) . '/timecard.pdf'; // Remove old one, just to make sure we are making it afresh @unlink( pdfFile); // Instnatiate the class with our variables pdf & new HTML ToPDF( htmlFile, defaultDomain, pdfFile); // Set headers/footers pdf- setHeader('color', 'blue'); pdf- setFooter('left', 'Generated by HTML ToPDF'); pdf- setFooter('right', ' D'); result pdf- convert(); // Check if the result was an error if (is a( result, 'HTML ToPDFException')) { die( result- getMessage()); } else { echo "PDF file created successfully: result"; echo ' br / Click a href "' . basename( result) . '" here /a to view the PDF file.'; } ? /body /html Please note that your folder permission is fully open
It’s your turn How to make a PDF file converted into database and shown by PHP files Please adapt into Final Project files (pdf files) to capture the abstract page (id and en)
THANK YOU