从 HTML PHP 生成 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8163963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:05:24  来源:igfitidea点击:

Generate PDF from HTML PHP

phphtmlpdf-generation

提问by Viralk

I want to generate PDF from a PHP file that includes HTML controls like textbox, and textarea. I attached CSS in the same. I tried FPDF, DOMPDF and TCPDF, but still I don't get exactly what I want. How do I pass HTML controls with PHP variables and CSS to these libraries?

我想从包含 HTML 控件(如文本框和文本区域)的 PHP 文件生成 PDF。我附上了 CSS。我尝试了 FPDF、DOMPDF 和 TCPDF,但仍然没有得到我想要的。如何将带有 PHP 变量和 CSS 的 HTML 控件传递给这些库?

回答by Mark Baker

mpdfis another option that you could try.

mpdf是您可以尝试的另一种选择。

回答by Hardik Thaker

EDIT :Found another solution for it, TCPDFis a FLOSS PHP class for generating PDF documents. Looks more dominating library.

编辑:为它找到了另一个解决方案,TCPDF是一个用于生成 PDF 文档的 FLOSS PHP 类。看起来更占主导地位的图书馆。

"PRINCEXML" is a good library (not completely free now).

PRINCEXML”是一个很好的库(现在不是完全免费的)。

Others:

其他:

If your meaning is to create a PDF file from PHP, pdflib will help you (as some other suggested).

如果您的意思是从PHP创建 PDF 文件, pdflib 将帮助您(如其他建议)。

Else, if you want to convert an HTML page in PDF via PHP, you'll find a little trouble outta here.. For three years I have been trying to do it as best as I can.

否则,如果您想通过 PHP 将 HTML 页面转换为 PDF,您会发现这里有点麻烦.. 三年来,我一直在尽我所能地做到这一点。

So, the options I know are:

所以,我知道的选项是:

HTML2PS: same of DOMPDF, but this one convert first in .ps (Ghostscript), then, in whatever format you need (PDF, JPEG, PNG). For me it is a little better than dompdf, but I have the same speed problem.. Oh, it has better compatibility with CSS.

HTML2PS:与 DOMPDF 相同,但这个首先转换为 .ps ( Ghostscript),然后转换为您需要的任何格式 (PDF, JPEG, PNG)。对我来说它比 dompdf 好一点,但我有同样的速度问题.. 哦,它与CSS有更好的兼容性。

Those two are PHP classes, but if you can install some software on the server, and access it through passthru() or system(), have a look at these too:

这两个是 PHP 类,但如果你可以在服务器上安装一些软件,并通过 passthru() 或 system() 访问它,也看看这些:

wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerful... It seem like it is the best one (atm) for converting HTML pages to PDF on the fly, taking only two seconds for a three pages XHTMLdocument with CSS 2. It is a recent project. Anyway, the Google Codepage is often updated.

wkhtmltopdf:基于 webkit(safari 的包装器),非常快速和强大......它似乎是将 HTML 页面即时转换为 PDF 的最佳(atm),只需两秒钟即可将三页XHTML文档与CSS 2. 这是一个最近的项目。无论如何,Google 代码页经常更新。

htmldoc: this one is a tank, it really never stops orcrashes... The project seems to have died in 2007, but anyway if you don't need CSS compatibility this can be nice for you.

htmldoc:这是一辆坦克,它真的永远不会停止或崩溃......该项目似乎在 2007 年就已经死了,但无论如何,如果您不需要 CSS 兼容性,这对您来说会很好。

** Thumbs Up For Strae.

** 为Strae竖起大拇指。

回答by Sonal Khunt

http://www.fpdf.org/is the site having a great HTML-to-PDF class which work well. I am using it, but you have to first study its functionality and then start.

http://www.fpdf.org/是一个有很棒的 HTML-to-PDF 类的站点,它运行良好。我正在使用它,但您必须先研究其功能,然后再开始。

回答by Shai Mishali

If I understand your needs correctly I don't think any PHP-PDF class would do that.

如果我正确理解您的需求,我认为任何 PHP-PDF 类都不会这样做。

Mostly you could insert only text and images to a PDF file, so if you would want something that looks like an HTML element you would need to insert it as an image.

大多数情况下,您只能将文本和图像插入 PDF 文件,因此如果您想要看起来像 HTML 元素的内容,则需要将其作为图像插入。

Usually just putting HTML doesn't mean all your elements would stay intact in the PDF . (Different world, after all)

通常只放置 HTML 并不意味着您的所有元素都会在 PDF 中保持完整。(毕竟不同的世界)