[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

Converting html to pdf

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,
  • 3
  • 2
1 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

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.
QuarfelburgAuthor Commented:
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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:
http://www.fpdf.org    --> click the "manual" link on the left

QuarfelburgAuthor Commented:
Exactly what I was looking for, thx again!
www.onlinepdfprinter.com 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now