简体   繁体   English

如何以编程方式在PDF中呈现动态HTML / PHP页面

[英]How to render a dynamic HTML/PHP page in a PDF programmatically

I've got a PHP page that I generate, forming an expense report for clients. 我有一个生成的PHP页面,用于为客户形成费用报告。 I've found that the clients end up file->saving the output, emailing it around, and printing it. 我发现客户端最终将文件->保存输出,通过电子邮件发送并打印输出。 Since emailing HTML and PHP isn't really ideal (ie, images are lost, formatting is wonky), I'd like to render the page to a pdf and stream that to them. 由于通过电子邮件发送HTML和PHP并不是很理想(例如,图像丢失,格式不正确),因此我想将页面呈现为pdf并将其流式传输到它们。

Now, I've thought of good ol' "Print as PDF," but not all clients have that ability. 现在,我已经想到了“以PDF格式打印”,但是并不是所有的客户都具备这种能力。 I've looked into doing it myself with PHP PDFLib, but that gets pretty hairy. 我已经考虑过自己使用PHP PDFLib进行此操作,但这变得非常繁琐。 I've looked into DOMPDF and DocRaptor, but they attempt to parse the DOM and generate a pdf, which doesn't work well for more complex designs. 我研究了DOMPDF和DocRaptor,但他们尝试解析DOM并生成pdf,这对于更复杂的设计而言效果不佳。

Here's the tantalizing thing: I use a Mac, and print->preview on the Mac does exactly what I want. 令人着迷的是:我使用Mac,而Mac上的print-> preview正是我想要的。 It takes the pixels of the rendered page and generates a pdf out of it. 它获取渲染页面的像素并从中生成一个pdf。 If only I could harness that power! 如果我能利用这种力量! Is there a way? 有办法吗? What can I do? 我能做什么?

This library seems to have the right ingredients: http://www.rustyparts.com/pdf.php 该库似乎具有正确的成分: http : //www.rustyparts.com/pdf.php

1) I never used it though, so can't tell much about it ( cUrl -> html2ps -> ps2pdf ) 1)虽然我从未使用过它,所以不能透露太多(cUrl-> html2ps-> ps2pdf)

2) Also maybe it will be easier to write a shell script ( cUrl, html2ps, ps2pdf ), and execute it in php, if it's an option (though not the best practice - security-wise) 2)如果可以的话,也许写一个shell脚本(cUrl,html2ps,ps2pdf)并在php中执行会更容易(尽管不是最佳实践-从安全角度考虑)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM