Converting PDF Files using PHP - PowerPoint PPT Presentation

About This Presentation
Title:

Converting PDF Files using PHP

Description:

Converting PDF Files using PHP By Idris Winarno idris_at_eepis-its.edu Tools FPDF PDFlib HTML_ToPDF FPDF Install your apache2 and PHP5 # apt-get install apache2 php5 ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 15
Provided by: IdrisW
Category:
Tags: pdf | php | converting | files | hello | using

less

Transcript and Presenter's Notes

Title: Converting PDF Files using PHP


1
Converting PDF Files using PHP
  • By
  • Idris Winarno
  • idris_at_eepis-its.edu

2
Tools
  • FPDF
  • PDFlib
  • HTML_ToPDF

3
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

4
Testing FPDF (1)
  • lt?php
  • require('fpdf.php')
  • pdfnew FPDF('P','mm','A4')
  • pdf-gtAddPage()
  • pdf-gtSetFont('Arial','B',16)
  • pdf-gtCell(40,10,'Hello World!')
  • pdf-gtOutput()
  • ?gt

5
Testing FPDF (2)
//Page footer function Footer() //Position
at 1.5 cm from bottom this-gtSetY(-15)
//Arial italic 8 this-gtSetFont('Arial','I',8)
//Page number this-gtCell(0,10,'Page
'.this-gtPageNo().'/nb',0,0,'C') //Instanc
iation of inherited class pdfnew
PDF() pdf-gtAliasNbPages() pdf-gtAddPage() pdf
-gtSetFont('Times','',12) for(i1ilt40i)
pdf-gtCell(0,10,'Printing line number
'.i,0,1) pdf-gtOutput() ?gt
  • lt?php
  • require('fpdf.php')
  • class PDF extends FPDF
  • //Page header
  • function Header()
  • //Logo
  • this-gtImage('logo_pb.png',10,8,33)
  • //Arial bold 15
  • this-gtSetFont('Arial','B',15)
  • //Move to the right
  • this-gtCell(80)
  • //Title
  • this-gtCell(30,10,'Title',1,0,'C')
  • //Line break
  • this-gtLn(20)

6
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 "extensionpdf.so" gtgt /etc/php4/conf.d/pdf_li
    b.ini
  • echo "extensionpdf.so" gtgt /etc/php5/conf.d/pdf_li
    b.ini
  • Checking yout config
  • apache2ctl configtest
  • Syntax OK
  • Restart your apache
  • /etc/init.d/apache2 restart

7
Alternative install of PDFLib
  • cd /usr/src/
  • wget http//www.pdflib.com/binaries/PDFlib/704/P
    DFlib-Lite-7.0.4p4.tar.gz
  • Or
  • wget http//lecturer.eepis-its.edu/idris/files/
    aplikasi_web/PDFlib-Lite-7.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

8
PDFLib
  • phpinfo()
  • PDF Support gt enabled
  • PDFlib GmbH Version gt 4.0.1
  • Revision gt Revision 1.8 Pdflib 4.0.1 -
    installation ok

9
PDFLib Example
  • lt?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_begin_page_ext(p, 595, 842, "")
  • font PDF_load_font(p, "Helvetica-Bold",
    "winansi", "")
  • 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)
  • header("Content-type application/pdf")
  • header("Content-Length len")
  • header("Content-Disposition inline
    filenamehello.pdf")
  • print buf
  • PDF_delete(p)
  • ?gt

10
HTML_ToPDF
  • Download source
  • cd /usr/src
  • wget http//www.rustyparts.com/scripts/HTML_ToPDF
    -3.5.tar.gz
  • or
  • wget http//lecturer.eepis-its.edu/idris/files/ap
    likasi_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

11
Creating HTML
  • This HTML code will be converted into PDF
  • Make your own HTML code

12
HTML_ToPDF Test
  • lt?php
  • / Id example1.php 2426 2006-11-18 195926Z
    jrust /
  • /
  • The simplest example. We convert an HTML file
    into a PDF file.
  • We also add a few custom headers/footers to
    the PDF.
  • /
  • ?gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtTesting HTML_ToPDFlt/titlegt
  • lt/headgt
  • ltbodygt
  • Creating the PDF from local HTML file.... Note
    that we customize the headers and footers!ltbr /gt
  • lt?php
  • // Require the class
  • require_once dirname(__FILE__) .
    '/../HTML_ToPDF.php'
  • // Full path to the file to be converted
  • htmlFile dirname(__FILE__) . '/test.html'
  • // 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
  • _at_unlink(pdfFile)
  • // Instnatiate the class with our variables
  • pdf new HTML_ToPDF(htmlFile, defaultDomain,
    pdfFile)
  • // Set headers/footers
  • pdf-gtsetHeader('color', 'blue')
  • pdf-gtsetFooter('left', 'Generated by
    HTML_ToPDF')
  • pdf-gtsetFooter('right', 'D')
  • result pdf-gtconvert()
  • // Check if the result was an error
  • if (is_a(result, 'HTML_ToPDFException'))
  • die(result-gtgetMessage())
  • else
  • echo "PDF file created successfully
    result"
  • Please note that your folder permission is fully
    open

13
Its 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)

14
THANK YOU
Write a Comment
User Comments (0)
About PowerShow.com