- wkhtmltopdf
简单方便且强大的一个导出PDF工具
https://github.com/mreiferson/php-wkhtmltox - pdftk
强大的pdf文件操作工具
http://www.pdflabs.com/docs/install-pdftk/
为了实现不同格式的pdf共存,我想到一个笨办法~,不过还挺有用,先使用wkhtmktopdf把目标html页面导成pdf,当所有的pdf都导出来后,在用pdftk把所有的pdf合成一个pdf输出。
public function downpdf($html){
ob_start();
file_put_contents("first.html", $html['first']);
file_put_contents("second.html", $html['second']);
file_put_contents("three.html", $html['three']);
file_put_contents("four.html", $html['four']);
ob_end_clean();
while (!file_exists("first.pdf") || !file_exists("second.pdf")|| !file_exists("three.pdf")|| !file_exists("four.pdf")){
if(!file_exists("first.pdf")){
shell_exec("wkhtmltopdf --page-size A4 --margin-top 20mm first.html first.pdf");
}else if(!file_exists("second.pdf")){
shell_exec("wkhtmltopdf --margin-top 20mm --page-width 420mm --page-height 297mm second.html second.pdf");
}else if(!file_exists("three.pdf")){
shell_exec("wkhtmltopdf --margin-top 20mm --page-size A4 three.html three.pdf");
}else{
shell_exec("wkhtmltopdf --margin-top 20mm --page-width 420mm --page-height 297mm four.html four.pdf");
}
}
unlink("first.html");
unlink("second.html");
unlink("three.html");
unlink("four.html");
if(file_exists("first.pdf") && file_exists("second.pdf") && file_exists("three.pdf") && file_exists("four.pdf")){
$result = shell_exec("pdftk first.pdf second.pdf three.pdf four.pdf cat output SBCX.pdf");
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=SBCX.pdf");
echo file_get_contents("SBCX.pdf");
unlink("first.pdf");
unlink("second.pdf");
unlink("three.pdf");
unlink("four.pdf");
unlink("SBCX.pdf");
}else{
$this->error('异常','',500);
exit;
}
}