2

so I am new with html to pdf so I am trying to learn it. When I googled for a good html to pdf converter, I came across TCPDF. Which is perfect for me because I use php. But when I require the tcpdf.php file, my page gets fully blank and I can't seem to get any errors.

What I have tried are the following things:

TCPDF destroyed HTML (blank page)
PHP Require makes my page blank?

I tried to make a test project which only includes the files that are required. And it worked! But on a different project I have this problem.

I tried to get an error by using catch Exception. But still no signs of information of whats going on.

I googled many things but I can't figure it out, how is it possible that in one project everything works fine, but it an another project, nothing seems to work. Where am I missing something?

This is the code I have

<?php
/**
 * Created by PhpStorm.
 * User: Mark
 * Date: 18-2-2019
 * Time: 15:54
 */

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require "init.php";
//require_once "tcpdf.php"; // this is what I have normally, and yes the location is correct

try {
  require_once "tcpdf.php"; // if I remove this everything works just fine.
} catch (Exception $e) {
  var_dump($e);
}

print_r(get_loaded_extensions());

// Include the main TCPDF library (search for installation path).
require_once('examples/tcpdf_include.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
dd($pdf);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// 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 auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

// Set some content to print
$html = <<<EOD
<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
<i>This is the first example of TCPDF library.</i>
<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
<p>Please check the source code documentation and other examples for further information.</p>
<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
EOD;

// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_001.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

I use php version 7.2.9

Mark_
  • 113
  • 1
  • 12

1 Answers1

0

Whening outputting a PDF to a webpage you cannot output anything before it as it has to set the HTTP headers. The PHP headers function cannot set them if there is anything on the screen.

From the docs https://www.php.net/manual/en/function.header.php:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Just as an extra note, you can include TCPDF through composer using tecnickcom/tcpdf as it looks like you are probably already using composer with either symfony or laravel as you are using the dd method.

Note that TCPDF doesn't throw exceptions as well. If you want it to throw exceptions you can overwrite the error method. E.g.

<?php

class MyPdf extends TCPDF
{
    public function error($msg)
    {
        throw new Exception($msg);
    }
}
Isaac Skelton
  • 156
  • 1
  • 8
  • Thanks for your reply, I think I found something, I added `header('Content-Type: application/pdf');` at the top of my page, and now it loads a PDF. But the content of the pdf says: "Cannot load pdf-document". Am I understanding your question correctly with the header situation? – Mark_ Nov 15 '19 at 10:06
  • @Mark_ Not quite, one of you included files, e.g. `init.php` may be outputting something to the screen before you call `$pdf->output()`. Also remove the `dd()` and `print_r`. Alternatively, try to output to a file rather than inline and see if that works. I would recommend Rubydocs for TCPDF documentation, it isn't completely up to date but it is better documentation then the official website IMO https://www.rubydoc.info/gems/rfpdf/1.17.4/TCPDF. – Isaac Skelton Nov 15 '19 at 23:22