[英]PHP: converting html file to pdf
I have an html file named welcomemailtemplate.html and I need to convert that file to a PDF. 我有一个名为welcomemailtemplate.html的html文件,我需要将该文件转换为PDF。
I first read this file using the following method provided by Yii framework: 我首先使用Yii框架提供的以下方法读取此文件:
$filename = Yii::app()->basePath.'\views\email\welcomemailtemplate.html';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$name = $model->name;
fclose($handle);
$message = str_replace ( "[username]", $name, $contents );
Then, to generate the PDF file, the following parameters are set: 然后,要生成PDF文件,请设置以下参数:
Yii::import('application.vendors.*');
require_once('tcpdf/tcpdf.php');
require_once('tcpdf/config/lang/eng.php');
$pdf = new TCPDF();
$pdf->SetCreator("Torget");
$pdf->SetAuthor('test name');
$pdf->SetTitle('Savani Test');
$pdf->SetSubject(' Torget Order Confirmation');
$pdf->SetKeywords(' Torget, Order, Confirmation');
//$pdf->SetHeaderData('', 0, PDF_HEADER_TITLE, '');
$pdf->SetHeaderData('', 0, "Torget Order", '');
$pdf->setHeaderFont(Array('helvetica', '', 8));
$pdf->setFooterFont(Array('helvetica', '', 6));
$pdf->SetMargins(15, 18, 15);
$pdf->SetHeaderMargin(5);
$pdf->SetFooterMargin(10);
$pdf->SetAutoPageBreak(TRUE, 0);
$pdf->SetFont('dejavusans', '', 7);
$pdf->AddPage();
If I pass the content as follows, it creates the PDF: 如果我按以下方式传递内容,它将创建PDF:
$pdf->writeHTML("<span>Hello World!</span>", true, false, true, false, '');
But if I pass the read html file content for pdf creating using following method it gives following error: 但是,如果我使用以下方法将读取的html文件内容传递给pdf创建,则会出现以下错误:
$pdf->writeHTML($message, true, false, true, false, '');
$pdf->LastPage();
Error message: 错误信息:
Undefined index: thead
Try to validate the file using the w3c validator http://validator.w3.org/ . 尝试使用w3c验证程序http://validator.w3.org/验证文件。
I've worked with tcpdf before but i gave it up because it didn't seem reliable. 我以前曾使用过tcpdf,但由于它似乎不可靠而放弃了。 You can also try wkhtmltopdf binary (only if your hosting allows you to use
proc_open
/ proc_close
). 您也可以尝试wkhtmltopdf二进制文件(仅在您的主机允许您使用
proc_open
/ proc_close
)。 Seems a little more stable to me. 对我来说似乎更稳定。 It also has a PHP class to help you use it.
它还具有一个PHP类来帮助您使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.