尝试使用dompdf将html呈现为pdf时遇到问题。
我把代码放在一个类中,在过程代码之后,我希望它创建一个html的pdf。
这是我目前掌握的密码:
$templatefile = file_get_contents("templates/costreport.htm");
//fill headers
$templatefile = str_replace("%DATES%",stripslashes($startdate)." - ".stripslashes($enddate),$templatefile);
if ($siteid>0) {
$pdfname = "costreport-".$clientid.".pdf";
} else {
$pdfname = "costreport-".$clientid."-".$siteid.".pdf";
}
//insert into database
//Close and output PDF document
$pdfname = str_replace("/","-",$pdfname);
$pdfname = str_replace("\\","-",$pdfname);
//create pdf
// unregister Yii's autoloader
spl_autoload_unregister('my_autoloader');
// register dompdf's autoloader
require_once("../system/dompdf/dompdf_config.inc.php");
// register Yii's autoloader again
spl_autoload_register('my_autoloader');
$dompdf = new DOMPDF();
$dompdf->set_paper("A4","portrait");
$dompdf->load_html($templatefile);
//set_time_limit(240);
$dompdf->render();
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it
// to the folder.
file_put_contents('../tmp/'.$clientid.'/'.$pdfname, $pdf);
当dompdf->render();进入时,代码失败,但一旦我取出那一行代码,代码就会工作,文件也会创建,但如果没有呈现,我就无法打开它。
我试过调试代码,并使模板是HTML有效的,但我现在不知所措。
当我运行包含
boolen false
的脚本时,返回的错误就是dompdf->render();
。 最佳答案
问题是我把这段代码放在班上的首位。
/set up path to new dir including dir to be created
chdir("../tmp/");
$newdirpath = getcwd()."/".$clientid;
//if an old invoice ticket pack exists unlink it so as not to get things confused with the new pack being created.
if (file_exists(getcwd()."/$clientid_$siteid_CostReport.pdf")) {
unlink(getcwd()."/$clientid_$siteid_CostReport.pdf");
}
//setup temp dir
if (!is_dir($newdirpath)) {
//create new dir if it doesn't already exist!
mkdir($newdirpath);
} else {
//dir already exists we need to empty it out first incase there's old stuff in there we don't want to duplicate data
//$this->removedirectory($newdirpath,false);
}
关于php - dompdf-> render();在api类中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29944222/