Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
372 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...