APD Toronto
asked on
Creating PDFs with PHP
Hi Experts,
Can anyone tell me a good and easy way to create PDFs through PHP?
For the majority, I'll be needing to create PDFs for reports based on a database query. The reports would consist of columns, rows, and row groupings.
I've came across http://www.fpdf.org/, but just reading their site, it says that it can be slower than PDFlib (I'm not sure what that is), and it doesn't provide HTML to PDF support. I'm not afraid of writing longer code, but does any one have any experience with fpdf, and knows if it is truly slow with more complex and lengthy reports? What about PDFlib or others?
Thank you,.
Can anyone tell me a good and easy way to create PDFs through PHP?
For the majority, I'll be needing to create PDFs for reports based on a database query. The reports would consist of columns, rows, and row groupings.
I've came across http://www.fpdf.org/, but just reading their site, it says that it can be slower than PDFlib (I'm not sure what that is), and it doesn't provide HTML to PDF support. I'm not afraid of writing longer code, but does any one have any experience with fpdf, and knows if it is truly slow with more complex and lengthy reports? What about PDFlib or others?
Thank you,.
ASKER
Have you used TCPDF, is it fast?
Yes, I've used both. It's pretty much the same level of performance. Unless you're doing something extremely complex, or unless you're on really slow hardware, you probably won't be able to tell a difference between the various libraries.
ASKER
Also (sorry), do you know if this will work under IIS? I'm running PHP 5.6.5 under IIS7.5
Yes, it should. There are several examples in the TCPDF documentation that you should be able to run quickly to see what kind of things you can generate.
Here is my experimental example using TCPDF. There are many parts of it that I do not understand (and that's why I usually choose FPDF instead).
<?php // demo/tcpdf_image_position.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');
// SYNTHESIZE THE PDF FILE NAME
$pdf_file_link = 'storage/tcpdf_example.pdf';
$pdf_file_name = getcwd() . DIRECTORY_SEPARATOR . $pdf_file_link;
// THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// EXTEND THE TCPDF OBJECT
class PDF extends TCPDF
{
// NULLIFY AUTOMATIC HEADER AND FOOTER
public function Header() {}
public function Footer() {}
}
echo "<br />Starting PDF creation" . PHP_EOL;
// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0,0,0,TRUE);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);
// DO NOT DO THIS
// $pdf->setFillColor(255,255,224);
// CELL BORDERS, IF NEEDED
$bdr = '1TRBL';
// IF NO CELL BORDER IS NEEDED
$bdr = 0;
// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);
// ADD AN IMAGE
// http://www.tcpdf.org/doc/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352
$img = 'images/ray_padded.png';
// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SCALE - EXPERIMENT WITH IT
$pdf->setImageScale(1.53);
// GET THE IMAGE, I HOPE
$pdf->Image
( $img
, 0 // $x
, 0 // $y
, 0 // WIDTH
, 0 // HEIGHT
, 'PNG' // TYPE
, '#' // LINK URL
, 'T' // SET POINTER TOP LEFT
, FALSE // NO RESIZING
, 300 // DPI
, 'L' // PALIGN
, FALSE // ISMASK
, FALSE // IMGMASK
, 0 // BORDER
, FALSE // FIT TO BOX
, FALSE // HIDDEN
, FALSE // FIT ON PAGE
)
;
// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24 // $x,
, 24 // $y,
, 'HELLO WORLD'
, FALSE // $fstroke = false,
, FALSE // $fclip = false,
, TRUE // $ffill = true,
, $bdr // $border = whatever,
, 2 // $ln = 0, 2=PUT CURSOR BELOW
, '' // $align = '', DEFAULT LEFT
, FALSE // $fill = false, CELL BACKGROUND
, '' // $link = '', NOT A LINK
, 0 // $stretch = 0, NO TEXT STRETCH
, FALSE // $ignore_min_height = false,
, 'A' // $calign = 'T', A=FONT TOP INSIDE CELL
, 'T' // $valign = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE // $rtloff = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;
// WRITE THE PDF FILE TO THE SERVER
echo "<br />Writing PDF\n";
$pdf->Output($pdf_file_name, 'F');
// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo '<br /><a target="my_PDF" href="' . $pdf_file_link . '"><strong>Print the PDF</strong></a>';
You could use ZendPdf for this. ZendPdf is part of the Zend Framework, which can be installed using the Microsoft Web Platform Installer.
Ray, your example looks fine at first glance. What parts don't you understand? To me, the syntax for FPDF and TCPDF is nearly identical...
ASKER
I downloaded TCPDF, installed it into my C:\inetpub\wwwroot\tcpdf, wwwroot is of course IIS's root folder, and all examples in .../tcpdf/examples work.
However, if I copy .../wwwroot/tcpdf/examples /example_0 01.php into .../www/myproject/testpdf. php and change line 28 from
require_once('tcpdf_includ e.php');
to
require_once('../tcpdf/exa mples/tcpd f_include. php');
I still get
Fatal error: Class 'TCPDF' not found in C:\inetpub\wwwroot\test\te stPDF.php on line 31
My goal is to keep TCPDF into my root, so I can access it by multiple of projects.
Thank you
However, if I copy .../wwwroot/tcpdf/examples
require_once('tcpdf_includ
to
require_once('../tcpdf/exa
I still get
Fatal error: Class 'TCPDF' not found in C:\inetpub\wwwroot\test\te
My goal is to keep TCPDF into my root, so I can access it by multiple of projects.
Thank you
@gr8gonzo: I agree - they are very similar. Probably just my prejudice and allocation of test time, but I found FPDF eaier for my work. Some things, for example, the setImageScale() method, I just never figured out!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Ray - I never had to screw with setImageScale beyond setting it to 1.53 like most people seem to do. To me, it was all about the dpi, since the PDF is measured in units like millimeters, so setImageScale was defining how many pixels-to-millimeters, but that was just an assumption I made a while ago. I didn't really mess with it because the 1.53 setting always seemed to produce accurate results. I just extended the base class with all my defaults and then used that extended class as my go-to template for new PDFs.
ASKER
Worked!
I should also mention that FPDF isn't really under active development anymore (at least not last time I checked). There's another project, TCPDF, which is pretty much the next generation of FPDF and is in more active development.