I have problem when converting PHP/HTML to PDF using TCPDF.
My method is:
First, I make a buffered HTML file from current PHP process, then I convert that HTML with TCPDF.
But the PDF results differently from what I expected. The first line is indented.
Here is my code:
index1.php
<form action="index1-process.php" method="post">
<input type="submit" value="Save">
</form>
<?php
function loop(){
for($i=0;$i<=5;$i++){
echo "Line ".$i."<br />\r\n";
}
}
?>
<!-- start buffering -->
<?php ob_start(); ?>
<html>
<body>
<?php loop() ?>
</body>
</html>
<!-- save buffer to file -->
<?php file_put_contents("index1.html", ob_get_contents()); ?>
After I clicked in the 'Save' button it buffers to:
index1.html
<html>
<body>
Line 0<br />
Line 1<br />
Line 2<br />
Line 3<br />
Line 4<br />
Line 5<br />
</body>
</html>
<!-- save buffer to file -->
but the PDF result is indented in first line:
index1.pdf
Line 0
Line 1
Line 2
Line 3
Line 4
Line 5
Here is my process code:
index1-process.php
<?php
require_once('./tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins("10","10","10");
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont("times");
$pdf->AddPage();
$html = file_get_contents("index1.html");
$pdf->writeHTML($html);
$pdf->Output("index1.pdf","D");
Is it my method or my code wrong?
Or is there any better way to save a PHP processed page to PDF?