Dynamic PDF generation
Category:Programming
Aug 15, 2009
I decided to set the daunting task of dynamic PDF generation for myself today. I want to be able to create invoices from the back-end of my website, have them PDF'd and emailed directly to my clients.
Invoices can be created from either website maintenance projects, purchases entered via the back-end or website design purchases entered via the front-end. I have already set up the functionality that logs the invoice in the database, assigns the products to it and creates the invoice via Smarty template. I was originally going to send the HTML invoice via email but thought it would be nicer to send it as an attachment and I could also save the PDF on the server for future reference.
I started researching it this morning. I came across a few different classes out there, like FPDF and PDFLIB but it's wasn't quite what I was looking for. You have to recreate the PDF from bottom up with those. What I wanted to to feed in the HTML and have the class create the PDF. Lo-andbehold, I found it, dompdf.
dompdf was easy to install on my Windows Vista machine, I downloaded it, unzipped it. I also had to download and execute the ttf2pt1 utility. I changed the path to the utility in the config file, ran a test script and Voila! it worked.
I then went back into the Invoice module where the invoices are created. I needed to capture the HTML from the Smarty template and feed it into the dompdf class then save it to a folder. I wanted the file names in the following fashion CLIENT_invoice_DATE.pdf. I also needed to check if this filename already existed and add a _1 after date, so that I didn't end up overwriting a file if two invoices where created on the same date for the same client. This was easy enough to do, I just put it into a while loop checking for the filename with is_file and using an incremental count variable to the _NUMBER.
I wrote out the code, tested it and got a load of errors. <code>Notice: Undefined variable: idProject in...</code> I was sure I hadn't changed the error_reporting in my php.ini file, checked it and it was still 'E_ALL & ~E_NOTICE' I eventually tracked down the problem to the config file in dompdf. It was setting the error_reporting to E_STRICT. I commented it out, and it worked.... yee haa.
A few small clean ups and I'm done for the day.
Invoices can be created from either website maintenance projects, purchases entered via the back-end or website design purchases entered via the front-end. I have already set up the functionality that logs the invoice in the database, assigns the products to it and creates the invoice via Smarty template. I was originally going to send the HTML invoice via email but thought it would be nicer to send it as an attachment and I could also save the PDF on the server for future reference.
I started researching it this morning. I came across a few different classes out there, like FPDF and PDFLIB but it's wasn't quite what I was looking for. You have to recreate the PDF from bottom up with those. What I wanted to to feed in the HTML and have the class create the PDF. Lo-andbehold, I found it, dompdf.
dompdf was easy to install on my Windows Vista machine, I downloaded it, unzipped it. I also had to download and execute the ttf2pt1 utility. I changed the path to the utility in the config file, ran a test script and Voila! it worked.
I then went back into the Invoice module where the invoices are created. I needed to capture the HTML from the Smarty template and feed it into the dompdf class then save it to a folder. I wanted the file names in the following fashion CLIENT_invoice_DATE.pdf. I also needed to check if this filename already existed and add a _1 after date, so that I didn't end up overwriting a file if two invoices where created on the same date for the same client. This was easy enough to do, I just put it into a while loop checking for the filename with is_file and using an incremental count variable to the _NUMBER.
I wrote out the code, tested it and got a load of errors. <code>Notice: Undefined variable: idProject in...</code> I was sure I hadn't changed the error_reporting in my php.ini file, checked it and it was still 'E_ALL & ~E_NOTICE' I eventually tracked down the problem to the config file in dompdf. It was setting the error_reporting to E_STRICT. I commented it out, and it worked.... yee haa.
A few small clean ups and I'm done for the day.



’)