不是答案,但对于上面的问题,但我会尝试提供一些我的经验,也许它将帮助某人在未来的某个地方.
> wkthmltopdf真的是唯一能够产生我称之为可接受结果的解决方案.尽管如此,必须对CSS进行一些小的修改,但是,在渲染内容时,它的效果非常好.所有其他包真的只适用于你有一个简单的文档与一个基本表等.没有机会让他们在复杂的文档上产生公平的结果与设计元素,CSS,多个重叠的图像等.如果复杂的文档在游戏中 – 不要花时间(像我一样) – 直接去wkhtmltopdf.
>注意 – wkhtmltopdf安装很棘手.对于我来说,这并不像他们在评论中所说的那样容易(其中一个原因可能是我对Linux不太熟悉).由于某些我无法解释的原因,静态二进制文件对我不起作用.我怀疑该版本存在问题 – 显然不同操作系统和处理器的版本之间存在差异,也许我有错误的版本.首先,要安装非静态版本,您必须具有对服务器的root访问权限,这是显而易见的.我用apt-get使用PuTTy安装它,运行得很顺利.我很幸运,我的服务器已经具备了安装wkhtmltopdf的所有优势.所以这对我来说很简单:)(顺便说一句,你不必像许多教程告诉你的那样关心符号链接或包装器 – 我花了好几个小时试图弄清楚如何做这个部分,最后我给了它尽管如此,一切运作良好
>安装后我得到了相当有名的无法连接到X服务器错误.这是因为我们需要在‘virtual’ x server上运行wkhtmltopdf无头.解决这个问题也非常简单(如果不关心符号链接).我用apt-get install xvfb安装它.这对我来说也很顺利,没问题.
>完成此操作后,我能够运行wkhtmltopdf.要小心 – 我花了一些时间才弄清楚尝试运行xvfb是错误的方式 – 而是你必须运行xvfb-run.我的PHP代码现在看起来像这个exec(“xvfb-run wkhtmltopdf –margin-left 16 /data/web/domain.com/source.html /data/web/domain.com/target.pdf”); (注意wkhtmltopdf的–margin-left 16命令行选项 – 它使我的内容更加集中;我留下它来演示如何使用命令行选项).
>我还想保护生成的PDF文件不被编辑(在我的情况下,也可以进行打印保护).在做了一些研究之后,我从ID Security Suite找到了this class.首先我要说 – IT’老(我正在运行PHP 5).但是,我对它做了一些改进.首先 – 它是FPDF库的包装器,因此包中有一个名为fpdf.php的文件.我用最新的FPDF版本替换了这个文件,我得到了from here.它使我的PHP警告看起来更具可持续性.我还改了$pdf =& new FPDI_Protection();并删除了&签名,因为我得到了一个弃用的警告.但是,还有更多的未来.而不是搜索和修改代码我只是使用error_reporting(0)将错误报告lvl变为0; (虽然关闭警告只应该足够).现在有人会说这不是“好习惯”.我在内部系统上使用这些东西,所以我真的不必关心.确保脚本可以修改以匹配最新要求.对我来说,我不想再花上几个小时的工作.请注意脚本中的$pdf-> SetProtection(array(‘print’),”,$password); (我允许打印我的文件,你可以看到).我花了一段时间才弄清楚第一个参数是权限.第二个是USER PASSWORD – 如果你提供这个,那么文档将需要一个密码才能打开(我把这个留空).第三个是所有者密码 – 这是你需要使文档“安全”反对编辑,复制等.
我的整个代码现在看起来像:
// get the HTML content of the file we want to convert
$invoice = file_get_contents("http://www.domain.com/index.php?s=invoices-print&invoice_no=".$_GET['invoice_no'];
// replace the CSS style from a print version to a specially modified PDF version
$invoice = str_replace('href="design/css/base.print.css"','href="design/css/base.pdf.css"',$invoice);
// write the modified file to disk
file_put_contents("docs/invoices/tmp/".$_GET['invoice_no'].".html", $invoice);
// do the PDF magic
exec("xvfb-run wkhtmltopdf --margin-left 16 /data/web/domain.com/web/docs/invoices/tmp/".$_GET['invoice_no'].".html /data/web/domain.com/web/docs/invoices/".$_GET['invoice_no'].".pdf");
// delete the temporary HTML data - we don't need that anymore since our PDF is created
unlink("docs/invoices/tmp/".$_GET['invoice_no'].".html");
// workaround the warnings
error_reporting(0);
// script from ID Security Suite
function pdfEncrypt ($origFile, $password, $destFile){
require_once('libraries/fpdf/FPDI_Protection.php');
$pdf = new FPDI_Protection();
$pdf->FPDF('P', 'in');
//Calculate the number of pages from the original document.
$pagecount = $pdf->setSourceFile($origFile);
//Copy all pages from the old unprotected pdf in the new one.
for ($loop = 1; $loop <= $pagecount; $loop++) {
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
//Protect the new pdf file, and allow no printing, copy, etc. and
//leave only reading allowed.
$pdf->SetProtection(array('print'), '', $password);
$pdf->Output($destFile, 'F');
return $destFile;
}
//Password for the PDF file (I suggest using the email adress of the purchaser).
$password = md5(date("Ymd")).md5(date("Ymd"));
//Name of the original file (unprotected).
$origFile = "docs/invoices/".$_GET['invoice_no'].".pdf";
//Name of the destination file (password protected and printing rights removed).
$destFile = "docs/invoices/".$_GET['invoice_no'].".pdf";
//Encrypt the book and create the protected file.
pdfEncrypt($origFile, $password, $destFile );
希望这可以帮助别人节省一些时间.整个解决方案花了我12个小时来实现我们的发票系统.如果wkhtmltopdf对于像我这样熟悉Linux / UNIX的用户有更好的信息,我本可以节省一些花在这上面的时间.
然而 – 什么不会杀死你让你更强大:)所以我现在更加完美,我做了这个运行:)