Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Introducting PDF Package for Fuel.
  • Hi all, I've just finished making a driver-based PDF package for Fuel for anyone who has a system that uses PDF generation. By default it includes two drivers, DOMPDF and TCPDF. See Github for more instructions, but in a nutshell:
    // Load PDF library
    $pdf = \PDF::factory('tcpdf')->init();
    
    // Any parameters you provide in the init function are
    // provided to the construct of the driver
    // eg: $pdf = \PDF::factory('tcpdf')->init('P', 'mm', 'A4');
    
    // Normally to add a page you call addPage();, but this class is smart and adapts
    $pdf->add_page();
    
    $pdf->output();
    

    So, once you've called the factory method to get a driver and the init method to initialise that driver, everything is now based off that driver - use that driver's methods. See Github for more info on adding drivers etc. Why use the one class with drivers? Because that way it's universal - you can use it in different applications. Sometimes in the same application TCPDF might be an overkill and you just want to use DomPDF. Well this makes it easy to switch between. Jelmer Schreuder if you're looking at this could you add this repo to the Fuel Packages github repo so that it can be downloaded via Oil? Anyway guys I'm open to suggestions - tell me what you think.
  • And now I get this error:
    Exception [ Error ]: Call to undefined method Pdf\Pdf::()
  • fueldocsogood wrote on Wednesday 27th of June 2012:
    And now I get this error:
    Exception [ Error ]: Call to undefined method Pdf\Pdf::()
    The entire method is missing in this call...
  • Hey Ben, I checked it out and there's one problem with it I'll wait for you to fix before forking: there's a dependency on another package of yours.
  • That looks freaking awesome, good work Ben :)
  • Hello, Pdf package is great and it works fine with dompdf except that CSS file is not used in my pdf, do you know why ?
    Here is my code
    $view = View::forge('test/view', $data);
    $pdf = Pdf::forge('dompdf')->init();
    $pdf->set_paper('A4');
    $pdf->load_html($view);
    $pdf->render();
    $pdf->stream('test.pdf');
    
    In my view head, I have
    <?php echo Asset::css('pdf.css'); ?>
    
  • @jschreuder woops! Ben and I both missed that. We have our own 'TJS' package we're slowly putting together. So far it has a calendar class & and iCal feed generator, a grid controller system (automatically generates table grids based off a Kohana ORM model object), and a few extra date, image & html helpers. Once it's ready for general consumption we'll be releasing it open-source so we can give something back to the FuelPHP & Kohana community. The grid controller basically takes a Kohana ORM model instance (relationships and all) as it's init parameter. You then call a bunch of 'add_column' methods to define what columns to retrieve. The end result is output of a html table loaded with the correct layout, links, and actions to do sort by, filter by, and mass selection actions. You can even add a 'renderer' property on each column to format the display of that column (e.g. mysql datetime to human) and convert human input on the filter by field for that column to correct format for the db. I'll post some screenshots in the morning when I'm in the office to show you what I mean.
  • Hi Samuel, That's actually a configuration with DOMPDF. It's set not to use external style sheets by default, I think you can override it somehow. The way I do this, which I believe is best, is including the CSS in the view you use. See http://docs.fuelphp.com/classes/asset.html#/method_css for how to do this, but basically in your example, you want:
    // The true param is to echo the Raw CSS. Note, it includes the <style type="text/css"></style> tags
    <?php echo Asset::css('pdf.css', array(), null, true); ?>
    

    This is what I do, it still lets you keep your CSS out of your views, but echoes it in your view when it's used in the PDF.
  • Thanks, it works.
  • @jelmer it's all fixed now.
  • Great work! I forked it into the packages account.
  • Please I get this error: ErrorException [ Error ]: Class 'PDF' not found. After downloading the package from git and placing this code on my controller method // Load PDF library
    $pdf = \PDF::factory('tcpdf')->init(); // Any parameters you provide in the init function are
    // provided to the construct of the driver
    // eg: $pdf = \PDF::factory('tcpdf')->init('P', 'mm', 'A4'); // Normally to add a page you call addPage();, but this class is smart and adapts
    $pdf->add_page(); $pdf->output();
  • About that grid - see this thread.
  • Have you called:
    Package::load('pdf');
    

    Or put the 'pdf' package in the 'always_load' array in APPPATH/config/config.php? Seems like you haven't loaded the package and one of the above methods will do that for you
  • Is it possible to use your Pdf class together view Fuel's View class?
    I need to pass data to be displayed in the View that will then be
    generated into Pdf using your PDF package. I've tried it with no success and wonder if it really is possible?
    Otherwise, I'll have to parse the file and replace the tag with the
    data in the code myself. Rati
  • Still didn't work after doing all that Now I get:
    Fuel\Core\PackageNotFoundException [ Error ]: Package 'pdf' could not be found at 'PKGPATH/pdf/' when i rename the downloaded folder from git (fuel-packages-fuel-pdf-ec568f3) to 'pdf' in the package folder, It displays:
    ErrorException [ Error ]: Class 'PDF' not found any more suggestions
  • Hi Rati, It should work? I have only had experience with DomPDF but this is how I do it: http://d.pr/Q3N4 http://d.pr/46VC I will be using the package in the coming days and I will probably make a few big changes to it, so keep posted and I might have a new, better API for the PDF package.
  • Hi, Thank you very much for you reply. It does work!
    After seeing your example, I realize that I missed the "render()" method.
    A really stupid mistake. :-) Thanks a lot,
    Rati
  • When using your package in production, I get a fuelphp 404 error when I try to instance a Pdf object, any idea why this is happening? It works fine on my local machine (using XAMPP).
  • You need to type it whit small letters 'Pdf->' not ' PDF->'
  • Rather confusingly, there seem to be two versions of this on github:

    The TJS version looks to be more recent - is that the correct version to use?
  • They are different.

    The one in fuel-packages is a fork of the TJS-Technology one, and has a far better interface. I use it on a daily basis.

    The only reason the TJS one "looks" newer is that it had a few commits to update the vendor packages, which is something you can do yourself very easily.
  • Hello Sirs, 

    I have an html table with data of 100 lines and it ends up in generating an empty PDF file. There is no error what so ever.. 
    If i use table with 30 lines it works fine. 
    Is there a limit to the data?

    My code:
    $pdf = \Pdf\Pdf::factory('tcpdf')->init('P', 'mm', 'A4', true, 'UTF-8', false);

    Thanks,
  • Maybe the html conversion has an isue with page overflow?

    I personally don't know, I always generate PDF's by hand, I don't use the HTML conversion.
  • Hello Sir,

    I am using this setting for html.
    $pdf->writeHTML($invoice_pdf_html, true, false, true, false, '');

    Much appreciated,
  • Hello
    I have a problem with the "pdf-package". I tested locally and everything worked like a charm.
    Unfortunately while testing on our hosting, an error occured.

    As soon as I initialize the following code (below), i get a 500 internal error.

    <?php<br />// Create an instance of the PDF class
    $pdf = \Pdf\Pdf::forge('dompdf')->init();

    Additionally i took a look into my logfiles: [Mon Jun 16 15:59:00 2014]
    [error] [client xxx.xxx.xxx.xx] Premature end of script headers: php.fcgi, referer: http://xxx

    Are u aware of this problem? Help is highly appreciated. Many thanks!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion