Search code examples
phppdfmoodletcpdf

Moodle loads white page TCPDF


I want to genarate custom PDF in Moodle (and later develop it further with the data from every course). I read the examples for TCPDF, but the system throws error every time, when trying to test them.

In printpdf.php in my local folder I have:

require_once ('/tcpdf.php');

$pdf = new TCPDF(P, 'mm', 'A4', true, 'UTF-8', false);

$pdf->SetTitle('Example');
$pdf->SetProtection(array('modify'));
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();

When trying to call the page mymoodle/local/custom/classes/printpdf.php it shows me:

Warning: require_once(/tcpdf.php): failed to open stream: No such file or directory in C:\xampp\htdocs\mymoodle\local\custom\classes\local\printpdf.php on line 24

Fatal error: require_once(): Failed opening required '/tcpdf.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\mymoodle\local\custom\classes\local\printpdf.php on line 24

Solution

  • You should not directly use the TCPDF library within Moodle - there is a wrapper around this that allows it to work correctly.

    require_once(dirname(__FILE__).'/../../../../config.php');
    require_once($CFG->libdir.'/pdflib.php');
    
    $pdf = new pdf();
    

    Note that you may need to adjust the number of '/../' in the first line, depending on how deeply nested below the top-level directory you are.

    Note also, that you appear to have placed your code inside the 'classes' directory, whilst not defining a PHP class; scripts to be directly accessed by the end user should be placed outside of the classes directory in your plugin.