Hack 87 Use PHP to Create PDF

 < Day Day Up > 

figs/moderate.gif figs/hack87.gif

Generate PDF from within your PHP script .

A number of libraries enable you to create PDF using PHP. The standard PHP documentation includes a PDF Functions section that describes the popular PDFlib module (http://www. pdflib .com). However, this PDF extension is not free software. Typically, you must purchase a license and then recompile PHP to take advantage of these functions.

Consider some of these free alternatives. They are native PHP, so they are easy to install; just include one in your script.

6.15.1 R&OS PDF-PHP

With the R&OS PDF-PHP library (http://www.ros.co.nz/pdf/), you can add text, bitmaps, and drawings to new PDF pages. Formatting includes running headers and footers, multicolumn layout, and tables. PDF features include page labels, links, and encryption. Programming features include callbacks and transactions.

 <?php // hello world with R&OS, from readme.pdf include ('class.ezpdf.php'); $pdf =& new Cezpdf( ); $pdf->selectFont('./fonts/Helvetica.afm'); $pdf->ezText('Hello World!', 50); $pdf->ezStream( ); ?> 

6.15.2 FPDF

FPDF (http://www.fpdf.org) enables you to add text, bitmaps, lines, and rectangles to new PDF pages. Formatting includes running headers and footers, multicolumn layout, and tables. PDF features include metadata and links. The home page provides an active user forum and user - contributed scripts. The following PHP script produces the PDF document shown in Figure 6-17.

Figure 6-17. Dynamically generated PDF from FPDF
figs/pdfh_0617.gif

 <?php // hello world with FPDF, adapted from the tutorial for IndigoPerl users define('FPDF_FONTPATH','C:\indigoperl\apache\htdocs\pdf_hacks\fpdf\font\'); require('C:\indigoperl\apache\htdocs\pdf_hacks\fpdf\fpdf.php'); $pdf=new FPDF( ); $pdf->AddPage( ); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output( ); ?> 

6.15.3 pdf4php

pdf4php (http://www.gnuvox.com/pdf4php/) provides basic operations for creating PDFs. Add text, JPEG bitmaps, lines, and rectangles to new PDF pages. PDF features include compression. The PHP library file size is smaller, so runtime parsing goes faster.

 <?php // hello world with pdf4php, adapted from the home page include('pdf4php.php'); $pdf=new PDFClass( ); $pdf->startPage(8.5 * 72, 11 * 72); $pdf->SetFont(48, 'Helvetica'); $pdf->SetStrokeColor(1,0,0); $pdf->DrawTextAt(4.25*72, 45, "Hello World!", ALIGN_CENTER); $pdf->endPage( ); $pdf->end( ); ?> 

6.15.4 phppdflib

phppdflib (http://www.potentialtech.com/ppl.php) provides basic operations for creating PDFs. You can add text, bitmaps, lines, rectangles, and circles to new PDF pages. Special features include templates.

 <?php // hello world with phppdflib, adapted from example.php require('phppdflib.class.php'); $pdf=new pdffile; $pdf->set_default('margin', 0); $firstpage=$pdf->new_page("letter"); $pdf->draw_text(10, 100, "Hello World!", $firstpage); $temp=$pdf->generate( ); header("Content-type: application/pdf"); header("Content-length: '.strlen($temp)); echo $temp; ?> 

 < Day Day Up > 


PDF Hacks.
PDF Hacks: 100 Industrial-Strength Tips & Tools
ISBN: 0596006551
EAN: 2147483647
Year: N/A
Pages: 158
Authors: Sid Steward

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net