Converting html to pdf

Posted on 2009-02-23
Last Modified: 2012-05-06
I'm trying to create a script that will convert my html to PDF file using PHP. It has to be done dynamically because some thing on the pdf will be based on the user's input.

I've tried using FPDF and HTML2FPDF but the problem with them is that they can't support css and absolute positioning in particular.

I'm wondering if there is a solution to this out there or perhaps someone has done something like this in the past. I'm attaching the desired PDF file with this question.

Thanks in advance for any help,
Question by:Quarfelburg
    LVL 31

    Accepted Solution

    Instead of trying to convert from html to pdf, you can use an existing pdf and a template and them add data to it.

    You've already tried out FPDF, so you should be familiar with it's interface. You can use simple calls to SetXY(), SetFont() and Cell() to put text into various places in the document.

    What you don't know, is that you can use FPDI to load an existing PDF, and use it as a template for FPDF to write onto.

    The way you use FPDI, is you create a class which inherits the base fpdi class and creates the pdf from a template. Then, you create an instance of that class in your calling page to actually do the pdf generation. I've attached an example that I use for generating invoices.

    -------- FPDI SUBCLASS --------
    class FPDF_Invoice2 extends FPDI
    	var $invoiceid;
         * Constructor
         * See FPDF-Manual
        function FPDF_Invoice() {
        function addInvoice($colname_Invoice)    {
    		$pagecount = $this->setSourceFile('pdf/invoicetemplate2.pdf');   
    		$tplidx = $this->importPage(1, '/BleedBox');   
    		$this->addPage('P', '', 'letter');   
    		$this->useTemplate($tplidx, -5, 0);   
    		//Output text onto the page
    		$this->SetXY(162.5, 34); $this->Cell(100,4,	'1 of 1'													,0,0,'L');  		// page
    ------ USAGE --------
    $pdf =& new FPDF_Invoice2();   
    $pdf->Output($filename, "F");  

    Open in new window

    LVL 31

    Expert Comment

    I tried going the html->pdf route.... it sucked. No matter which engine you use - even the expensive pay engines - they never get it quite right.

    Use an existing pdf as a template and overlay user generated text ontop of it. It's the only surefire way.

    Author Comment

    Hi Frosty555,

    Thanks for a quick and prompt reply. That FPDI class certainly saved me couple hours of work!

    Do you happen to know a way of changing color/style of the Cell in FPDF? As you can see in that example that I've attached, I'd like to draw a grid with light gray dashed borders.
    LVL 31

    Expert Comment

    The easiest thing to do is first make the PDF from scratch using some other piece of software, and then only use FPDF to lay text down ontop of it.

    But if you really want to draw colored boxes using FPDF for some reason, I think the SetDrawColor() and SetFillColor() functions are what you're looking for.

    Check out the FDPF reference:    --> click the "manual" link on the left


    Author Closing Comment

    Exactly what I was looking for, thx again!

    Expert Comment

    by:JLT is a service that you can use to populate Word templates with dynamic data and get a PDF stream back to your web app (to save to disk or download or whatever). You can sign up for free too.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now