Php2pdf

Post on 19-May-2015

1.406 views 0 download

Tags:

description

Fox Valley Computing Professionals Presentation Series

Transcript of Php2pdf

PHP to pdf files

May 7,2009

By Eric Michalseneric@michalsengroup.com

WHAT?

PHP to pdf files, dynamically cool stuff!

Why in the world would you want to do that??

pfd's are cool, fun, secure, scary, pain in the butt...

Cody says “Hmmm, I'm intrigued. Tell me more.”

Tools To Use:

TCPDFwww.tcpdf.org

TCPDF project was started in 2002 and now it is freely used all over the world by millions of people.

TCPDF is a Free Libre Open Source Software (FLOSS).

- no external libraries are required for the basic functions;- supports all ISO page formats;- supports custom page formats, margins and units of measure;- supports UTF-8 Unicode and Right-To-Left languages;- supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;- supports document encryption;- includes methods to publish some (x)HTML code;- includes graphic (geometric) and transformation methods;- includes Javascript and forms support;

blah blah blah...

Copy/Paste from their website...

Cody says “ROCK ON!

This is going to be easy!”

RTFM

Follow the installation instructions.

Seriously. If I can do it, so can you!

Most of the examples in this presentation are bastardized from the tcpdf.org site.

<?$dir_file = "files/".$file;require_once('config/lang/eng.php');require_once('tcpdf.php');class MYPDF extends TCPDF {public function LoadData($file) {

$lines=file($file);$data=array();foreach($lines as $line)

{$data[]=explode(';',chop($line));

}return $data;

}public function ColoredTable($header,$data)

{$this->SetFillColor(255,0,0);$this->SetTextColor(255);$this->SetDrawColor(128,0,0);$this->SetLineWidth(.3);$this->SetFont('','B');$w=array(40,167,30,30);

for($i=0;$i<count($header);$i++)$this->Cell($w[$i],7,$header[$i],1,0,'C',1);$this->Ln();$this->SetFillColor(224,235,255);$this->SetTextColor(0);$this->SetFont('');$fill=0;

foreach($data as $row) {

$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill); $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill); $this->Cell($w[2],6,$row[2],'LR',0,'R',$fill); $this->Cell($w[3],6,$row[3],'LR',0,'R',$fill);

$this->Ln(); $fill=!$fill;}

$this->Cell(array_sum($w),0,'','T'); } }

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor("Eric Michalsen");$pdf->SetTitle("TrendEyes www.trendeyes.com");$pdf->SetSubject("PDF Report");$pdf->SetKeywords("PDF Report");$pdf->SetHeaderData(PDF_HEADER_LOGO,

PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf->setLanguageArray($l); $pdf->AliasNbPages();$pdf->AddPage();$pdf->SetFont("helvetica", "", 8);$header=array('Metric','Detail','Description','Alternate');$data=$pdf->LoadData($dir_file);$pdf->ColoredTable($header,$data);$pdf->Output("report.pdf"); ?>

“Hey, I thought you said this would be easy!”

Step 1 :: Hello World<?PHP/////////////////////////////// Include the TCPDF files////////////////////////////require_once('config/lang/eng.php');require_once('tcpdf.php');

// create new PDF document$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('Eric Michalsen');$pdf->SetTitle('Example 1');$pdf->SetSubject('Western Suburb PHP Meetup');$pdf->SetKeywords('PHP, PDF, example'); $pdf->setPrintHeader(false);$pdf->setPrintFooter(false);$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf->setLanguageArray($l); $pdf->SetFont('times', 'BI', 20);$pdf->AddPage();$pdf->Cell(0, 10, 'Hello World', 1, 1, 'C');$pdf->Output('example.pdf', 'I'); ?>

<?PHPrequire_once('config/lang/eng.php');require_once('tcpdf.php');$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('Eric Michalsen');$pdf->SetTitle('example 2');$pdf->SetSubject('Image');$pdf->SetKeywords('TCPDF, PDF, example');$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); $pdf->setLanguageArray($l); $pdf->AddPage();$pdf->setJPEGQuality(75);$pdf->Image('images/lightning.jpg', 50, 50, 100, 150, '', 'http://www.foxvalleycp.com', '', true, 150);$pdf->Output('2.pdf', 'I');?>

<?phprequire_once('config/lang/eng.php');require_once('tcpdf.php');$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('Eric Michalsen');$pdf->SetTitle('example 3');$pdf->SetSubject('hmmmm...pie');$pdf->SetKeywords('TCPDF, PDF, example');$pdf->setPrintHeader(false);$pdf->setPrintFooter(false);$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);$pdf->setLanguageArray($l);$pdf->SetFont('helvetica', '', 14);$pdf->AddPage();$xc = 70; // from left$yc = 70; // from top$r = 50; // radius$pdf->SetFillColor(120, 120, 255); // blue$pdf->PieSector($xc, $yc, $r, 0, 90); //x,y,r,starting,ending degrees$pdf->SetFillColor(120, 255, 120); // green$pdf->PieSector($xc, $yc, $r, 90, 100);$pdf->SetFillColor(255, 120, 120); // red$pdf->PieSector($xc, $yc, $r, 130, 270);$pdf->Output('3.pdf', 'I');?>

$xc = 70; $yc = 70; $r = 50;

// text annotation$pdf->Annotation(100, 10, 20, 10, "First Line\nSecond Line", array('Subtype'=>'Text', 'Name' => 'Comment', 'T' => '90% Blue', 'Subj' => 'example', 'C' => array(255, 255, 0)));

$pdf->SetFillColor(120, 120, 255); // blue$pdf->PieSector($xc, $yc, $r, 0, 90); //x,y,r,starting,ending degrees

hmmm....pie

...but what does it mean? We Need Annotation Baby!

Mouse Over Fancy Stuff

$pdf->SetFillColor(120, 120, 255); // blue$pdf->PieSector($xc, $yc, $r, 0, 90); //x,y,r,starting,ending degrees$pdf->SetFillColor(120, 255, 120); // green$pdf->PieSector($xc, $yc, $r, 90, 100);$pdf->SetFillColor(255, 120, 120); // red$pdf->PieSector($xc, $yc, $r, 130, 270);

$pdf->Text(100, 20, '90% is Blue');$pdf->Text(130, 75, '10% is Green');$pdf->Text(20, 125, '82% is Red');

...or just plain text...

end

Eric Michalseneric@michalsengroup.com

Fox Valley Computing Professionalswww.foxvalleycp.com

Twitter #FVCP