PHP中的HTML2PDF-转换实用程序&脚本-示例和演示 [英] HTML2PDF in PHP - convert utilities & scripts - examples & demos

查看:90
本文介绍了PHP中的HTML2PDF-转换实用程序&脚本-示例和演示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML/CSS布局非常复杂,我想在服务器上将其转换为PDF.我已经尝试过DOMPDF,但是不幸的是,它没有以正确的布局转换HTML.我已经考虑过HTMLDOC,但是我听说它在很大程度上忽略了CSS,所以我认为布局也将与该工具分开.

I have a quite complicated HTML/CSS layout which I would like to convert to PDF on my server. I already have tryed DOMPDF, unfortunately it did not convert the HTML with correct layout. I have considered HTMLDOC but I have heard that it ignores CSS to a large extent, so I suppose the layout would break apart with that tool too.

我的问题是-是否可以使用其他在线演示工具(例如wkhtmltopdf)来验证HTML的转换方式?在度过余生之前,请先安装&一个接一个地测试?

My question therefor is - are there any online demos for other tools (like wkhtmltopdf i.e.) that I could use to verify how my HTML is converted? Before spending the rest of my life installing & testing one by one?

不幸的是,我无法更改HTML布局以适合这些工具.或者更好的说-我可以,如果其中任何一个接近可接受的结果...

Unfortunately, I can't change the HTML layout to fit those tools. Or better said - I could, if any of them would get close to an acceptable result...

推荐答案

不是一个真正的答案,但针对上述问题,但我会尽力提供一些经验,也许会在将来对某人有所帮助. /p>

Not really an answer but for the question above, but I'll try to provide some of my experience, maybe it will help someone somwhere in the future.

  1. wkthmltopdf 确实是唯一适用于我的解决方案产生我所说的可接受的结果.仍然需要对CSS进行一些小的修改,但是,在呈现内容时,它确实工作得很好.所有其他软件包实际上仅适用于只有一个基本表等简单文档的情况.没有机会让它们在具有设计元素,css,多个重叠图像等的复杂文档上产生公平的结果. -不要花时间(像我一样)-直接进入wkhtmltopdf .

  1. wkthmltopdf is really THE ONLY solution that worked for me that could produce what I call acceptable results. Still, some minor modifications to the CSS had to be made, however, it worked really well when it comes to rendering the content. All the other packages are really only suitable if you have a rather simply document with one basic table etc. No chance to get them to produce fair results on complex docs with design elements, css, multiple overlapping images etc. If complex documents are in game - do not spend the time (like I did) - go straight to wkhtmltopdf.

当心- wkhtmltopdf安装非常棘手.这对我来说并不像他们在评论中所说的那么容易(原因之一可能是我对Linux不太熟悉).静态二进制文件对我不起作用,原因是我无法解释.我怀疑版本存在问题-显然,针对不同操作系统和处理器的版本之间存在差异,也许我的版本不正确.首先,要安装非静态版本,您必须具有对服务器的root访问权限. 我使用PuTTy在apt-get中将其安装了,效果很好.我很幸运,我的服务器已经具备安装wkhtmltopdf的所有条件.所以这对我来说很容易:)(顺便说一句,您不必像许多教程所介绍的那样去关心符号链接或包装器-我花了数小时试图弄清楚该部分的用法,最后我给了它起来,一切都很好)

Beware - the wkhtmltopdf installation is tricky. It was not so easy for me as the guys said in their comments (one of the reasons might be that I am not too familiar with Linux). The static binary did not work for me for some reason I can't explain. I suspect that there were problems with the version - apparently there is a difference between versions for different OS and processors, maybe I have the vrong version. For installing the non-static version first of all you have to have root access to the server, that's obvious. I installed it with apt-get using PuTTy, went quite well. I was lucky that my server already had all the predispositions to install wkhtmltopdf. So this was the easy part for me :) (btw, you don't have to care for symbolic links or wrappers as many tutorials tell you - I spent hours trying to figure out how to do that part, in the end I gave it up and everything works well though)

安装后,我遇到了非常著名的Cannot connect to X server错误.这是由于我们需要在虚拟" x服务器 .解决这个问题也非常简单(如果不关心符号链接的话). 我用apt-get install xvfb 安装了它.这对我来说也很顺利,没有问题.

After the install I got the quite famous Cannot connect to X server error. This is due to the fact that we need to run wkhtmltopdf headless on a 'virtual' x server. Getting around this was also quite simple (if one does not care for the symbolic links). I installed it with apt-get install xvfb. This also went quite well for me, no problems.

完成此操作后,我可以运行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命令行选项-它使我的内容更加居中;我将其留在原处以演示如何使用命令行选项).

After completing this I was able to run wkhtmltopdf. Beware - it took me some time to figure out that trying to run xvfb was the wrong way - instead you have to run xvfb-run. My PHP code now looks like this exec("xvfb-run wkhtmltopdf --margin-left 16 /data/web/domain.com/source.html /data/web/domain.com/target.pdf"); (notice the --margin-left 16 command line option for wkhtmltopdf - it makes my content more centered; I left it in place to demonstrate how you can use command line options).

我还想保护生成的PDF文件,以防编辑(在我的情况下,也可以进行打印保护).经过研究后,我发现ID Security的 此类套房.首先,我必须说-它很旧(我运行的是PHP 5+).但是,我对此做了一些改进.首先-它是 FPDF 的包装,因此在其中有一个名为fpdf.php的文件.包装.我从从此处获得的最新FPDF版本中替换了此文件.它使我的PHP警告看起来更可持续.我还更改了$pdf =& new FPDI_Protection();并删除了&标志,因为我已收到不赞成使用的警告.但是,还有更多的东西要来.而不是搜索和修改代码,我只是使用error_reporting(0);将错误报告lvl设置为0(尽管仅关闭警告应该足够了).现在有人会说这不是好的做法".我在内部系统上使用了全部内容,因此我不必在意.确保可以修改脚本以符合最新要求.对我来说,我不想再花几个小时来处理它. 请注意脚本中显示$pdf->SetProtection(array('print'), '', $password); 的位置(如您所见,我允许打印文档).我花了一段时间才弄清楚第一个参数是权限. 秒是用户密码-如果您提供此密码,那么文档将需要密码才能打开(我将此留空). 第三个是所有者密码-这是使文档受保护"以防编辑,复制等的条件.

I also wanted to protect the generated PDF files from editing (in my case, print protect is also possible). After doing some research I found this class from ID Security Suite. First of all I have to say - IT'S OLD (I am running PHP 5+). However, I made some improvements to it. First of all - it's a wrapper around the FPDF library, so there is a file called fpdf.php in the package. I replaced this file from the latest FPDF version I got from here. It made my PHP warnings look more sustainable. I also changed the $pdf =& new FPDI_Protection(); and removed the & sign as I was getting an deprecated warning for it. However, there are more of those to come. Instead of searching and modifying the code I just turned the error reporting lvl to 0 with error_reporting(0); (although turning off the warnings only should be sufficient). Now someone will say that this is not "good practice". I am using this whole stuff on an internal system, so I do not really have to care. For sure the scripts could be modifiyed to match latest requirements. For me I didn't want to spend another hours working on it. Be careful where the script says $pdf->SetProtection(array('print'), '', $password); (I allowed printing my documents as you can see). It took me a while to figure out that the first argument is the permissions. The second is the USER PASSWORD - if you provide this then the docs will require a password to open (I left this blank). The third is the OWNER PASSWORD - this is what you need to make the docs "secured" against editing, copying etc.

我的整个代码现在看起来像:

My whole code now looks like:

// 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个小时来将其实施到我们的帐单系统中.如果对像我这样不熟悉Linux/UNIX的用户有关于wkhtmltopdf的更好的信息,我可以节省一些时间在此上.

Hope this helps someone to save some time in the future. This whole solution took me like 12 hours to implement into our invoicing system. If there was better info on wkhtmltopdf for users like me, who are not that familiar with Linux/UNIX, I could have saved some of the hours spent on this.

但是-不会杀死你的东西会使你变得更强大:)所以,我进行了这次跑步后,我变得更加完美了:)

However - what doesn't kill you makes you stronger :) So I am a bit more perfect now that I made this run :)

这篇关于PHP中的HTML2PDF-转换实用程序&amp;脚本-示例和演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆