Php pdf creation



  • Hi. I currently have the task of maintaining a mamoth php program, which involves pdf generation(using fpdf). The first time i saw the pdf generation code I looked like the guy pictured top-left. And there's LOTS of code like this, the entire(long) document is created from scratch in php.

     $pdf->AddPage();
     $pdf->HeadingFont();
     $pdf->SetLeftMargin(15);
     $pdf->SetY(30);
     $pdf->AnswerFont();
     $pdf->Cell(0,5, '');
     $pdf->Ln();

     $today_date = date("d M Y");
     $pdf->Cell(0,5, $today_date);
     $pdf->Ln();
     $pdf->Heading2Font();
     $pdf->Cell(0,5, "Text", 0, 0, 'R');
     $pdf->Ln();
     $pdf->Cell(0,5, "More Text", 0, 0, 'R');
     $pdf->AnswerFont();
     $pdf->Ln();
     $pdf->Ln();
     $pdf->Ln();

    Seriously, there has GOT to be a better way. Is there a better library? Can I perhaps use some sort of template and just insert in the bits that change? As you can see I'm new to this php/pdf thing.



  • That looks suspiciously like someone took a PostScript document and ran it through a translator.



  • the number of comments and loops suggests otherwise.



  • I used fpdf years ago.  It was pretty much the only thing available for generating pdf's at the time.  It definitely is a PITA. 

    One thing you could look at is latex.  It might be a better option for generating pdfs.
     



  • @DaBookshah said:

    Hi. I currently have the task of maintaining a mamoth php program, which involves pdf generation(using fpdf). The first time i saw the pdf generation code I looked like the guy pictured top-left. And there's LOTS of code like this, the entire(long) document is created from scratch in php.

     $pdf->AddPage();
     $pdf->HeadingFont();
     $pdf->SetLeftMargin(15);
     $pdf->SetY(30);
     $pdf->AnswerFont();
     $pdf->Cell(0,5, '');
     $pdf->Ln();

     $today_date = date("d M Y");
     $pdf->Cell(0,5, $today_date);
     $pdf->Ln();
     $pdf->Heading2Font();
     $pdf->Cell(0,5, "Text", 0, 0, 'R');
     $pdf->Ln();
     $pdf->Cell(0,5, "More Text", 0, 0, 'R');
     $pdf->AnswerFont();
     $pdf->Ln();
     $pdf->Ln();
     $pdf->Ln();

    Seriously, there has GOT to be a better way. Is there a better library? Can I perhaps use some sort of template and just insert in the bits that change? As you can see I'm new to this php/pdf thing.

    yeah, try PDFlib . also you could try using '\n' in the original text in order to remove the Ln() calls (note this is only in theory, as I could not be bothered installing the fpdf library).



  • There are some introductory PDFLIB examples here:

    http://bmw.ws.utk.edu/ut/php/

    scroll down to "Creating PDF Files On-the-Fly"

    Also the guys at PDFLIB were somewhat responsive. They had at one time a two tiered licensing setup. Free for researchers and universities doing non-admininstrative work.

    But I think even the full blown license was very reasonable.

    Also Jim Thome did a tutorial a few years ago and is helpful.

    http://devzone.zend.com/node/view/id/131

    Thanks


    J



  • No, there isn't much you can do to alleviate the drudge code for placing text by coordinates, selecting fonts and whatever.  (You get the same kind of stuff if you try to generate an XLS file from a script too - there's lots of place-by-coordinate operations that you ignore when using a mouse and a GUI.)

    I once had to create gift certificates in PHP.  I obviously didn't want to create all of the pretty backgrounds and stuff in the script, just write in the name and voucher number on a prepared template.  I think the free-download package I used was FPDI.  This allows you to use any page from any PDF as a background image for your output PDF.  It was reasonably efficient too; it didn't inflate the file size by converting it to a bitmap or anything silly.


Log in to reply