TCPDF を使用して PHP/HTML を PDF に変換するときに問題が発生しました。
私の方法は次のとおりです。
まず、現在の PHP プロセスからバッファリングされた HTML ファイルを作成し、その HTML を TCPDF で変換します。
しかし、PDF の結果は、私が期待したものとは異なります。最初の行はインデントされています。
これが私のコードです:
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()); ?>
「保存」ボタンをクリックすると、次のようにバッファリングされます。
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 -->
ただし、PDF の結果は最初の行でインデントされます。
index1.pdf
Line 0
Line 1
Line 2
Line 3
Line 4
Line 5
ここに私のプロセスコードがあります:
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");
それは私の方法または私のコードが間違っていますか?
または、PHP で処理されたページを PDF に保存するより良い方法はありますか?