Anyone here got a viewfile that pulls information from the database and outputs the HTML to PDF?? If so, could you post your code?
I have a invoicepdf.php file which was parsed for the CI template but it didnt work so I added PHP code directly in the invoicepdf.php and getting the same error…
Parse error: syntax error, unexpected $end in /home/beemcom/public_html/system/application/views/invoices/invoicepdf.php on line 143
Im not sure of any other way around it, I have a table invoices and a table invoiceitems so the invoice will call the items for the related invoice before displaying it…
I’m not quite as sold on MVC as some of the folks around here. I know it works great most of the time, but I can’t think of any way MVC would make your task easier.
I’ll be watching to see if someone can give an example of something that would be “easier” or at least not a lot more difficult.
What I would probably do is have a model do a join on the invoice and invoice items table, producing the data, then have the controller call another model (or helper) that converts the data into an HTML string, complete with headings and table tags with css, then sends that as a lump to the view.
But separating those two models is probably not “easier” at all. Safer, perhaps. More likely to produce reusable code, perhaps.
<?php /* * * This file is essentially a stripped down version of /views/invoices/view.php * Any changes you make to that formatting, you may consider adding to this. * */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>BambooInvoice: <?= $pageTitle;?> </title> <style type="text/css"> /* * Invoice view styles notes * * This file NEEDS a locally located stylesheet to generate the appropriate formatting for * transformation into a PDF. If you alter this file (and you are encouraged to do so) just * keep in mind that all of your formatting must be located here. You might also find that * there is limited or no support for a specific CSS style you want (ie: floating) and you'll * need to work around with old-school tables. Sorry for that... ;) * */
Great stuff here Derek, you saved me a few hours of work. The only thing I find disappointing is that DOMPDF handles UTF-8 characters so badly. I think this is a shortcoming of PHP, not DOMPDF, though.
function pdf() { $this->load->plugin('to_pdf'); // page info here, db calls, etc. $html = $this->load->view('controller/viewfile', $data, true); pdf_create($html, 'filename'); }
How and where do I call this function?
What’s the 3rd argument in the load->view for?
I assume that ‘controller’, ‘viewfile’ and ‘filename’ can be anything, yes?
$data, is required? According to the errors I had, I think so.
Does anybody have a simple, working, example of a controller and a view for this?
I tried a simple test:
class Dompdftest extends Controller {
function Dompdftest(){ parent::Controller(); } function pdf(){ $this->load->plugin('to_pdf'); $data['title'] = 'The title of the page'; $data['content'] = 'Some content...'; $html = $this->load->view('dompdftest_view', $data, true); pdf_create($html, 'my_filename',true); }
BambooInvoice uses this successfully, you can download the code and play if you want.
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 1572864 bytes)
You are running out of PHP memory. You’ll need to either boost it with an htaccess file, or ask your host to increase it for you. Ask them for 32M at least.
Thanks Derek! The .htaccess tip solved my problem.
Actually, I’m quite impressed by dompdf. Just the tool I need to create PDF’s on the fly that look acceptable. Nice!