TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
PDF Generator for Kohana
  • Hi guys,

    I thought I'd share this with ye. I was handed some PHP classes for creating PDF docs this morning and was charged with integrating them into our Kohana system. I don't see any point in other ppl having to repeat work so click the link above to download. Simply unzip it into the vendors folder of your kohana system and place the template PDF doc somewhere relative to your index.php file.

    It works fine for us but I have not tested all its features or how its use might affect performance so please dont just drop it into a production environment without checking it out first.

    anyway enjoy,
    benny :)
  • Hi. Thanks for sharing this. Can you give example on how to use this under Kohana?
  • hi,

    There some sample code in the zip but check below for a quick eg.


    public function pdf()
    {
    // find the PDF PHP files.
    require Kohana::find_file('vendor/pdf/fPDF', 'fpdf');
    require Kohana::find_file('vendor/pdf/fPDI', 'fpdi');

    // create an instance
    $pdf =& new FPDI();

    // add a page to the doc
    $pdf->AddPage();

    // load template
    $pdf->setSourceFile("assets/template.pdf"); // relative to index.php

    $template = $pdf->importPage(1);
    // use the imported page and place it at point 20,30 with a width of 170 mm
    $pdf->useTemplate($template, 20, 30, 170);

    // now write some text above the imported page
    $pdf->SetFont('Arial');
    $pdf->SetTextColor(0,0,0);
    $pdf->SetXY(90, 160);
    $pdf->Rotate(90);

    $pdf->Write(0, "This is a test");

    $pdf->Output('doc.pdf', 'I');
    }
  • Uhm, so it's just a redownload of fPDF? >_<

  • yip afraid so zombor. as i said i was only given the files and told to work them into Kohana, sorry if ppl were expecting more than that, but hey if u werent then all is good :)
  • Hi,
    I've got a problem while trying to use the above example. it prints the almost proper PDF, however the template is not used. The path is definitely valid (relative to index.php), I also tried the absolute path (DOCROOT."res/template.pdf") but it doesn't work either....
    any ideas?
  • hey migajek,

    any chance you could show us your code please? what is the output, is it just a blank pdf?
  • the code is exactly the one you wrote, except that the path is "res/template.pdf" (so it points the file correctly).
    the output is "This is a test" written in the middle of the page, but unfortunately the page "background" is blank (I suppose it should be content of a pdf template, right? )
  • hi migajek,

    i doubled checked the sample code above in case of a typo but its all correct. You are right in thinking that the generated PDF should show the content of the template as the "background". I too encountered the problem you described but it turned out to be a simple incorrect path supplied for the template, can you double check yours just in case? The only thing that I have that might be any different to your setup is that I'm not using URL rewriting.
  • Hi, the path is definitely 100% correct... and no matter I use url rewriting or not, the thing doesnt work... btw even with the url rewriting on, the "res" subdirectory is never rewritten...
  • im stumped migajek sorry. just as a test I suggest you place the template file in the same location as your index.php, check the file permissions on the template and change the call to $pdf->setSourceFile('template.pdf');

    if anyone else reading this thread has any ideas please do add a comment.
  • I'm currently having the exact same issue.

    Odd thing is - if I try the above example in a new clean project - it works fine. When I try and use it in my current project, the template is 'blank' as mentioned above.

    The code is 100% correct, the template is 100% correct, it doesn't have to do anything with htaccess since that does not change anything ... what on earth could this be?
  • Ok, I have tracked this one down...but I have NO IDEA AT ALL why it is doing this.

    My project has a 'locale.php' file in it's config folder. The content of this file is as following:

    <?php
    $config['language'] = array('nl_NL', 'Dutch');


    By simply changing the word 'Dutch' to something like 'DDutch', the template works as intended. I have tried this with other languages, but as far as I know it's only with the word 'Dutch' as the second array value that the template displays blank.

    Migajek, I assume you are using the same language setting?

    What on earth could this be? Must be some conflict, but I have no idea how to track this one down.
  • This solution must be upgrade to comply with PHP 5.3.2. Replace the FPDI files in vendor/pdf/fPDI with the files found on: http://www.setasign.de/products/pdf-php-solutions/fpdi/. Make sure to delete the decoders folder and add the filters folder. The "wrapper_functions.php" file is now not need with this upgrade; so, it can be deleted. In making this upgrade, be sure to replace the require statements in these files with Kohana's find_file function. As of this comment, the FPDF files do not need to be updated. However, if you would like to check to see if the FPDF files need to be updated go to: http://www.fpdf.org/.

    Also, the example given by bennythemink on Jan 15th 2009 should be updated. The "require" statements should be placed at the top of the file (rather than in the function) and should be:

    require_once Kohana::find_file('vendor/pdf/fPDF', 'fpdf');
    require_once Kohana::find_file('vendor/pdf/fPDI', 'fpdi');
  • It doesn't work with PHP 5.2.13. I got a message "Assigning the return value of new by reference is deprecated" with a line 96 in APPPATH/vendor/pdf/fPDI/fpdi.php

    $this->parsers[$fn] =& new fpdi_pdf_parser($fn,$this);

    Has anyone where i can find valid version of fpdi and fpdf ??
  • Just a quick note: I don't know fPDI, but there's a fork of fPDF called TCPDF which is actively being developed, PHP 5.3 compatible and UTF8 by default. (LGPL)

    TCPDF homepage

  • Just for the info.

    I have downloaded the latest DOMPDF and it just worked out of the box after I dropped in application/vendor dir.

    I did not have to make any any changes to the DOMPDF code and example code given in forum post generates PDF file for download.

    I am able to create a PDF file from a view file!

  • I also recommend using TCPDF instead of FPDF. It is FPDF compatible in most of the functions and also add many good features like barcode generator and some others. But the best feauture of TCPDF is that in TCPDF is used UTF-8 encoding (in FPDF it is ISO-8859-1 I think) for fonts. If any other language than English is used you'd appreciate it.

  • @snoblucha,

    I have downloaded TCPDF and put it in application/vendor

    Can you tell me how do I use this in KO3.1.3.1 ?

    Thanks for your help and support.

  • Inside your controller:

    $this->auto_render = FALSE;
    $html = 'Hello world!';
    $pdf = new Pdf();
    $pdf->content($html);
    $pdf->save('test');
    

    Put this inside your class folder and make sure tcpdf folder is inside vendor folder:

    class Pdf
    {
    
        protected $_pdf;
    
        /**
         * Set up default values
         */
        public function __construct($lang = NULL)
        {
            
            if ($lang === NULL)
            {
                $lang = 'eng';
            }
    
            require Kohana::find_file('vendor/tcpdf', 'tcpdf');
            require Kohana::find_file('vendor/tcpdf/config/lang', $lang);
    
            // Create new PDF document - const. from tcpdf_config
            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, TRUE, 'UTF-8', FALSE);
    
            // Set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('');
            $pdf->SetTitle('');
            $pdf->SetSubject('');
            $pdf->SetKeywords('');
    
            // Remove default header/footer
            $pdf->setPrintHeader(FALSE);
            $pdf->setPrintFooter(FALSE);
    
            // Set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    
            // Set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
            // Set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    
            // Set image scale factor
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
            // Set font
            $pdf->SetFont('dejavusans', '', 10);
    
            // Add a page
            $pdf->AddPage();
            $this->_pdf = $pdf;
        }
    
        public function content($html)
        {
            $this->_pdf->writeHTML($html, TRUE, FALSE, TRUE, FALSE, '');
        }
    
        /**
         * Rendering PDF with given values
         * [1] Name
         * [2] D - Download, I - default action defined in client browser (main options)
         */
        public function save($name)
        {
            $this->_pdf->Output($name . '.pdf', 'I');
            Request::current()->response()->headers('Content-Type', 'application/pdf');
        }
    }
    
  • @matino

    Thanks for the code... It works!

    Now any pointers to enable external CSS?

  • My fork

    https://github.com/seyfer/mpdf

    You are wellcome to make changes and pull request's. :)

  • Nothing like spamming 3 dead threads to promote your repo!

Howdy, Stranger!

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

In this Discussion