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

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

Converting html to pdf

Hello,
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,
Hayden
single-sheet-planner-final.pdf
0
Quarfelburg
Asked:
Quarfelburg
  • 3
  • 2
1 Solution
 
Frosty555Commented:
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.

http://www.setasign.de/products/pdf-php-solutions/fpdi/

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 --------
 
<?PHP
 
require_once('lib/fpdf/fpdi.php'); 
require_once('Connections/KCSPortal.php');
 
class FPDF_Invoice2 extends FPDI
{
	var $invoiceid;
 
    /**
     * Constructor
     * See FPDF-Manual
     */
    function FPDF_Invoice() {
        parent::FPDI('P','mm','letter');
    }	
 
    function addInvoice($colname_Invoice)    {
 
		$pagecount = $this->setSourceFile('pdf/invoicetemplate2.pdf');   
		$tplidx = $this->importPage(1, '/BleedBox');   
		$this->addPage('P', '', 'letter');   
		$this->useTemplate($tplidx, -5, 0);   
		
		$this->SetAutoPageBreak(false);
		
		//Output text onto the page
		$this->SetFont('helvetica','',8);
		$this->SetXY(162.5, 34); $this->Cell(100,4,	'1 of 1'													,0,0,'L');  		// page
 
 
    }
 
 
 
 
 
 
------ USAGE --------
 
 
<?PHP
 
require('lib/fpdf_invoice2.php');
$pdf =& new FPDF_Invoice2();   
$pdf->addInvoice("5523");
$pdf->Output($filename, "F");  
 
 
?>

Open in new window

0
 
Frosty555Commented:
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.
0
 
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.
0
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.

 
Frosty555Commented:
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

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

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