0

I have a PHP project in which I use TCPDF. I had a PHP File which creates an HTML theme with bootstrap. Inside this file I include my pdfGeneration.php. It looks like that:

require_once('tcpdf/tcpdf.php');
$pdfAuthor = "Test";
$pdfName = "Test.pdf";
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($pdfAuthor);
$pdf->SetTitle($pdfName);
$pdf->SetSubject($pdfName);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();

$htmlContent = 'Test';
$pdf->writeHTML($htmlContent, true, false, true, false, '');
ob_end_clean();

$outputFile = 'myServerPathHere';
$pdf->Output($outputFile, 'F');

echo '<span>PDF Creation finished. <a href="' . $outputFilePath . pdfName'" target='blank'>You can download the file here</a></span>';

If I don't include this pdfGeneration everything works fine. But when I include it I got only a blank white site with that span in it as a result. The rest of the HTML code that is located inside the file that includes the pdfGeneration is totally lost. I can't find an error?!

My goal is a site where the pdf is generated and in the best case, the pdf is directly opened in a new tab. I don't find a solution for that so I solved it with the link. But my first problem is the missing HTML.

Vidhyut Pandya
  • 1,605
  • 1
  • 14
  • 27
  • 2
    I supose the missing `"` on the second line is a typo? – Jerodev Mar 15 '19 at 11:17
  • Yes it is! But I think after two days of searching I've finally found the error. It is the ob_end_clean();. I added it because tcpdf in another print mode (for direct display) otherwise threw an error. The problem is that all the HTML code in the buffer is deleted. None of it will be output anymore. – HobbyCoder Mar 15 '19 at 11:39

1 Answers1

0

Excuse my question, I found the error myself. The function "ob_end_clean(); " is the error. PHP Docs: ob_end_clean - Clean (erase) the output buffer and turn off output buffering. This probably causes the entire previously generated HTML code to be lost. If I remove the command it works.