1

I've created a simple wrapper class for a TCPDF example I got off the web. My aim is to call the methods in the class in succession, then output a pdf file to a directory in my project.

The code worked fine when I had a version of it in one blob and calling it from a page. After putting it into a class it seems to hang/do nothing around the call to Output(). I cannot step into it when debugging in netbeans, and no error appears to be thrown.

In case it was a folder permissions issue I've run chmod on the output directory in question.

Here is the class:

<?php

define('IMAGE_DIR', '/home/user/NetBeansProjects/PDF_Quote/img/');
require_once('lib/tcpdf/tcpdf.php');

class PDF_Test extends TCPDF {


    function __construct() {
        parent::__construct(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    }

    public function set_document_info($creator = '', $author = '', $title = '', $subject = '', $keywords = '')
    {            
        $creator = PDF_CREATOR;
        $author = 'Author';
        $title = 'Title Example';
        $subject = 'Subject Example';
        $keywords = 'TCPDF, PDF, example, test, guide';

        // set document information
        $this->SetCreator($creator);
        $this->SetAuthor($author);
        $this->SetTitle($title);
        $this->SetSubject($subject);
        $this->SetKeywords($keywords);
    }

    public function header($logo_img = '', $title_text = '', $addit_text = '')
    {
        $logo_img = IMAGE_DIR . 'headerimg.png';
        $title_text = 'the title';
        $addit_text = 'additional text';

        $this->SetHeaderData($logo_img, PDF_HEADER_LOGO_WIDTH, $title_text.' 001', $addit_text, array(0,64,255), array(0,64,128));
        $this->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    }

    public function footer($logo_img = '', $text = '')
    {
        $this->setFooterData(array(0,64,0), array(0,64,128));
        $this->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    }

    public function set_default_monospaced_font($font = '')
    {
        if (strlen($font) === 0) { $font = 'courier'; }
        $this->SetDefaultMonospacedFont($font);
    }

    public function set_margins($margin_left = '', $margin_top = '', $margin_right = '', $margin_header = '', $margin_footer = '')
    {
        $margin_left = PDF_MARGIN_LEFT; $margin_top = PDF_MARGIN_TOP; $margin_right = PDF_MARGIN_RIGHT;
        $margin_header = PDF_MARGIN_HEADER; $margin_foot = PDF_MARGIN_FOOTER;

        $this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $this->SetHeaderMargin(PDF_MARGIN_HEADER);
        $this->SetFooterMargin(PDF_MARGIN_FOOTER);            
    }

    public function set_auto_page_break($set = True, $margin = '')
    {
        $set = True;
        $margin = PDF_MARGIN_BOTTOM;

        $this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    }

    public function set_image_scale($img_scale_ratio = '')
    {
        $img_scale_ratio = PDF_IMAGE_SCALE_RATIO;

        $this->setImageScale(PDF_IMAGE_SCALE_RATIO);
    }

    public function set_language_array()
    {
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $this->setLanguageArray($l);
    }
    }

    public function set_font_subsetting($set = True)
    {
        $set = True;

        $this->setFontSubsetting($set);
    }

    public function set_font($family = '', $style = '', $size = '', $fontfile = '', $subset = '', $out = True)
    {
        $family = 'dejavusans'; $size = '14'; 

        $this->SetFont($family, $style, $size, '', $out);
    }

    public function add_page()
    {
        $this->AddPage();
    }

    // Can be html or just plain text
    public function add_text_blob($html = '')
    {
        $html = '<h1>This is some stuff</h1><p style="background-color: green;">This is some content adhjdjasjd</p>';

        $this->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    }

    public function output($path, $file_name)
    {
        $path = dirname(__FILE__) . '/pdfcache/'; //'/home/user/NetBeansProjects/PDF_Quote/pdfcache/';
        $file_name = 'file_' . date('Y_m_d_H_i_s') . '.pdf';

        try
        {
            $fp = $path . 'example1.pdf'; //. $file_name; //'/home/user/NetBeansProjects/PDF_Quote/pdfcache/example_001.pdf';
            $this->Output($fp, 'F');
        }
        catch (exception $ex)
        {
            return $ex;
        }            
        return $path . $file_name;
    }
}

?>

Here is the code that calls the class in another php file:

require_once('PDF_Test.php');
$pdf = new PDF_Test();

$pdf->set_document_info();
$pdf->header();
$pdf->footer();
$pdf->set_default_monospaced_font();
$pdf->set_margins();
$pdf->set_auto_page_break();
$pdf->set_image_scale();
$pdf->set_language_array();
$pdf->set_font_subsetting();
$pdf->set_font();
$pdf->add_page();
$pdf->add_text_blob();
$pdf->output();

I haven't managed to find any similar issues in my googling, but I'm new to PHP, so I'm not sure if I'm overlooking anything glaringly obvious either.

1 Answer 1

1

I would recommend using the Output method in a way that it returns the PDF as binary string, so you can do whatever you want with it. This would be $pdfdoc = $pdf->Output('', 'S').

This way, you can still put the file to a path of your choice, but also dump it directly to the client, or attach it to an e-mail … and you can do a better error handling, because you have control over what TCPDF produces.

By the way, when you override a method in PHP, and you want to call the parent method, then you must use parent::METHOD(). Because you're actually overriding the header, footer, and output methods of TCPDF with yours (even if yours are lowercased).

1
  • My shoddy overloading seems to have been the issue alright. As soon as I replaced the "$this->" with "parent::" wherever I called a TCPDF method, all began outputting as it should. Thanks!
    – dbr
    Sep 4, 2014 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.