Open emAmazed opened 10 years ago
I have been looking for a good open source program for converting HTML/CSS output to PDF using PHP. I settled with FPDF after a bit of research.
Here is a small program I wrote to generate a PDF landing page.
<?php require('fpdf.php'); include_once "inc/Dao_view.php"; class PDF extends FPDF { // current column var $col = 0; // ordinate of column start var $y; function Header() { // Logo $this->Image("images/apps/logo.JPG",160,8,40); $this->SetFont('Arial','B',6); //Calculate width of title and position $this->SetXY(50,10); //Colors of frame, background and text $this->SetDrawColor(0,80,180); $this->SetFillColor(230,230,0); $this->SetTextColor(220,50,50); //Thickness of frame (1 mm) $this->SetLineWidth(1); //Title $this->Cell(100,6,date("F j, Y"),0,0,'C'); //Line break $this->Ln(10); // save ordinate $this->y=$this->GetY(); } function Footer() { //Position at 1.5 cm from bottom $this->SetY(-10); //Arial italic 8 $this->SetFont('Arial','I',8); //Text color in gray $this->SetTextColor(128); //Page number $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C'); } function LoadData_heading($conn,$townid) { $data = array(); $sql = Dao_heading($townid); if ( $result = mssql_query($sql,$conn) ) { while ( $r = mssql_fetch_assoc($result) ) { $data[] = "Distance"; $data[] = $r["bedford"]; $data[] = $r["boston"]; $data[] = $r["plymouth"]; $data[] = $r["worcester1"]; $data[] = "Heading"; $data[] = $r["bedford_heading"]; $data[] = $r["boston_heading"]; $data[] = $r["plymouth_heading"]; $data[] = $r["worcester1_heading"]; $data[] = "ETA"; $data[] = $r["bedford_eta"]; $data[] = $r["boston_eta"]; $data[] = $r["plymouth_eta"]; $data[] = $r["worcester1_eta"]; } mssql_free_result($result); } return $data; } // town title function LoadData_PageHeading($conn,$townid) { $data= array(); $sql = Dao_get_town_info($townid); if ( $result = mssql_query($sql,$conn) ) { while ( $r = mssql_fetch_assoc($result) ) { $data[] = $r["town"]; $data[] = $r["state"]; $data[] = $r["freqcode"]; $data[] = $r["freq_type"]; $data[] = $r["chan#"]; $data[] = $r["z_chan_name"]; $data[] = $r["z_chan_#"]; $data[] = $r["z_id"]; } mssql_free_result($result); } return $data; } function DrawPage_heading($data) { // Town State $this->SetFillColor(128,0,0); $this->SetTextColor(255); $this->SetDrawColor(255,255,255); $this->SetLineWidth(.3); $this->SetFont('','B',14); $w = array(50,10,30,18,15); for( $i=0; $i<2; $i++ ) { $this->Cell($w[$i],10,$data[$i],'LR',0,'C',true); } // freq head $this->Cell(6,6,''); $this->SetFillColor(112,112,112); $this->SetTextColor(255); $this->SetFont('','B',8); $this->Cell(30,6,"FREQ CODE",'LTRB',0,'C',true); $this->Cell(18,6,"FREQ TYPE",'LTRB',0,'C',true); $this->Cell(15,6,"CHAN#",'LTRB',1,'C',true); $this->Cell(6,6,''); $this->SetX(6); // new line for Freq info $this->SetFillColor(255,255,255); $this->SetTextColor(0); $this->SetDrawColor(0,0,0); $this->SetLineWidth(.3); $this->Cell(70,6,''); $this->SetFont('','',8); for( $i=2; $i<(count($w)); $i++ ) { $this->Cell($w[$i],6,$data[$i],'LTRB',0,'C',true); } $this->Ln(0); $this->SetXY(146,20); // trunk info $this->SetFillColor(128,0,0); $this->SetTextColor(255); $this->SetFont('','B',8); $this->Cell(60,6,"TDFM-658 800 TRUNK",'LTRB',2,'C',true); $this->SetFillColor(112,112,112); $this->SetTextColor(255); $this->SetFont('','B',8); $this->Cell(20,6,"ID",'LTRB',0,'C',true); $this->Cell(20,6,"CHAN#",'LTRB',0,'C',true); $this->Cell(20,6,"ZONE",'LTRB',1,'C',true); $this->SetTextColor(0); $this->SetFont('','',8); $this->SetX(146); $this->Cell(20,6,$data[$i],'LTRB',0,'C',false); $this->Cell(20,6,$data[$i+1],'LTRB',0,'C',false); $this->Cell(20,6,$data[$i+2],'LTRB',0,'C',false); $this->Ln(); } // heading table function DrawTable_heading($header,$data) { //Colors, line width and bold font $this->SetFillColor(255,0,0); $this->SetTextColor(255); $this->SetDrawColor(128,0,0); $this->SetLineWidth(.3); $this->SetFont('','',8); //Header width $w=array(20,10,10,10,12,12,14,14,12,14,10,10,10,10,10); for($i=0;$i<count($header);$i++) { $this->Cell($w[$i],6,$header[$i],1,0,'C',true); } $this->Ln(); //Color and font restoration $this->SetFillColor(224,235,255); $this->SetTextColor(0); $this->SetFont(''); //Data $fill=false; for ( $j=0; $j<count($header); $j++ ) { $this->Cell($w[$j],4,$data[$j],'LR',0,'L',$fill); } $this->Ln(); $fill=true; for ( $k=0; $k<(count($header)); $k++ ) { $this->Cell($w[$k],4,$data[$j+$k],'LR',0,'L',$fill); } $this->Ln(); $fill=false; for ( $q=0; $q<count($header); $q++ ) { $this->Cell($w[$q],4,$data[$j+$k+$q],'LR',0,'L',$fill); } $this->Ln(); $this->Cell(array_sum($w),0,'','T'); // closing table } // get data function LoadData($conn,$townid) { $data = array(); $sql = Dao_info($townid); if ( $result = mssql_query($sql,$conn) ) { while ( $r = mssql_fetch_assoc($result) ) { $data[] = "LZ-".$r["lz#"]; $data[] = $r["latitude"]; $data[] = $r["longitude"]; $land = str_replace("\'", "'", $r["landing_zone"]); $pilot = str_replace("\'", "'", $r["pilot_notes"]); $land = str_replace('\"', '"', $land); $pilot = str_replace('\"', '"', $pilot); $data[] = $land.(((!empty($pilot))?" // CAUTION:".$pilot:"")); $data[] = ($r["airport"])?"Yes":""; } mssql_free_result($result); } return $data; } // draw table function DrawTable($header,$data) { //Colors, line width and bold font $this->SetX(5); $this->SetFillColor(255,0,0); $this->SetTextColor(255); $this->SetDrawColor(128,0,0); $this->SetLineWidth(.3); $this->SetFont('','',6); //Header width $w=array(8,12,12,164,6); for($i=0;$i<count($header);$i++) { $this->Cell($w[$i],4,$header[$i],1,0,'C',true); } $this->Ln(); //Color and font restoration $this->SetX(5); $this->SetFillColor(224,235,255); $this->SetTextColor(0); $this->SetFont(''); //Data $fill = true; for ( $i=0,$j=0; $i<count($data); $i++ ) { $this->MultiCell($w[$j],4,$data[$i],'LR','J',$fill); if ( $j<4 ) { $j++; } else { // background fill if (($i+1)%2) $fill = false; else $fill = true; $j = $j-count($w)+1; $this->Ln(); $this->SetX(5); } } $this->Cell(array_sum($w),0,'','T'); } // get list of agency function LoadData_alist($conn,$townid) { $data = array(); $sql = Dao_agency_list($townid); if ( $result = mssql_query($sql,$conn) ) { while ( $r = mssql_fetch_assoc($result) ) { $data[] = trim($r["agency"]); $data[] = trim($r["id"]); $data[] = trim($r["phone"].$r["phone_ext"]); $data[] = trim(set_pri_freq($conn,$r["rx_freq"],$r["rx_tone_freq"],$r["rx_dig"])); $data[] = trim(set_pri_freq($conn,$r["tx_freq"],$r["tx_tone_freq"],$r["tx_dig"])); $data[] = trim($r["address"]); $data[] = trim($r["town"]); $data[] = trim($r["state"]); $data[] = ($r["fueling_agency_only"])?"Yes":""; } mssql_free_result($result); } return $data; } // get list of primary agencies function LoadData_alist_primary($conn,$townid,$heli) { $data = array(); $sql = Dao_primary_agency($townid,$heli); if ( $result = mssql_query($sql,$conn) ) { while ( $r = mssql_fetch_assoc($result) ) { $data[] = trim($r["agency"]); $data[] = trim($r["id"]); $data[] = trim($r["phone"].$r["phone_ext"]); $data[] = trim(set_pri_freq($conn,$r["rx_freq"],$r["rx_tone_freq"],$r["rx_dig"])); $data[] = trim(set_pri_freq($conn,$r["tx_freq"],$r["tx_tone_freq"],$r["tx_dig"])); $data[] = trim($r["address"]); $data[] = trim($r["town"]); $data[] = trim($r["state"]); $data[] = ($r["fueling_agency_only"])?"Yes":""; } } return $data; } // draw agency list table function DrawTable_alist_head($header) { $this->SetX(5); //Colors, line width and bold font $this->SetFillColor(255,0,0); $this->SetTextColor(255); $this->SetDrawColor(128,0,0); $this->SetLineWidth(.3); $this->SetFont('','',6); //Header width $w=array(50,10,22,22,22,44,20,6,6); for($i=0;$i<count($header);$i++) { $this->Cell($w[$i],4,$header[$i],1,0,'C',true); } $this->Ln(); } function DrawTable_alist_data($data) { //Color and font restoration $this->SetX(5); $this->SetFillColor(224,235,255); $this->SetTextColor(0); $this->SetLineWidth(.3); $this->SetFont(''); $w=array(50,10,22,22,22,44,20,6,6); //Data $fill = false; for ( $i=0,$j=0; $i<count($data); $i++ ) { $this->MultiCell($w[$j],4,$data[$i],'LR','LR',$fill); if ( $j<8 ) { $j++; } else { // background fill if (($i+1)%2) $fill = true; else $fill = false; $j = $j-count($w)+1; $this->Ln(); $this->SetX(5); } } $this->Cell(array_sum($w),0,'','T'); } } $pdf=new PDF(); $title="Landing Page Profile"; $pdf->SetTitle($title); $pdf->SetAuthor('Emilie Y'); // info if ( isset($_GET["t"]) && (!empty($_GET["t"])) && ($_GET["t"]!="undefined") ) { $data = $pdf->LoadData_heading($conn,$_GET["t"]); $data_lz = $pdf->LoadData_lz($lz_conn,$_GET["t"]); $data_alist = $pdf->LoadData_alist($lz_conn,$_GET["t"]); $data_alist_p1 = $pdf->LoadData_alist_primary($lz_conn,$_GET["t"],"first_heli"); $data_alist_p2 = $pdf->LoadData_alist_primary($lz_conn,$_GET["t"],"second_heli"); $data_alist_p3 = $pdf->LoadData_alist_primary($lz_conn,$_GET["t"],"third_heli"); $data_pagehead = $pdf->LoadData_PageHeading($lz_conn,$_GET["t"]); $pdf->SetFont("Arial",'',10); $pdf->AddPage(); // page title contents $pdf->DrawPage_heading($data_pagehead); $pdf->Ln(6); // headings $header = array("","BED","BOS","PYM","WORC"); $pdf->DrawTable_heading($header,$data); $pdf->Ln(6); // info $header_lz = array("LZ#","Latitude","Longitude","Warning","Airport"); $pdf->DrawTable_lz($header_lz,$data_lz); $pdf->Ln(6); // show list of agencies $header_alist = array("Agency","ID","Phone","PRI_RX","PRI_TX","Address","Town","State","Fuel"); $pdf->DrawTable_alist_head($header_alist); $pdf->DrawTable_alist_data($data_alist); $pdf->DrawTable_alist_data($data_alist_p1); $pdf->DrawTable_alist_data($data_alist_p2); $pdf->DrawTable_alist_data($data_alist_p3); $pdf->Ln(6); $pdf->Output(); } ?>
I have been looking for a good open source program for converting HTML/CSS output to PDF using PHP. I settled with FPDF after a bit of research.
Here is a small program I wrote to generate a PDF landing page.