如何将 PHP 网页转换为 PDF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18119481/
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
How to convert a PHP web page to PDF?
提问by QKWS
I have a PHP file that generates a report from the database, and I want this page to be converted into a PDF file, so it can be saved and printed. How do I convert a web page to a PDF? Is there any tool available or a PHP script?
我有一个从数据库生成报告的 PHP 文件,我希望将此页面转换为 PDF 文件,以便可以保存和打印。如何将网页转换为 PDF?有没有可用的工具或PHP脚本?
采纳答案by Goutam Pal
You can use MPDF, a PHP library which generates PDF files from UTF-8 encoded HTML. It's under GNU GPL v2 licence.
您可以使用 MPDF,这是一个 PHP 库,可从 UTF-8 编码的 HTML 生成 PDF 文件。它在 GNU GPL v2 许可下。
回答by Shankar Damodaran
Yes.
是的。
Make use of TCPDFPlugin.
使用TCPDF插件。
Here's an example and try this after configuring the above on your code.
这是一个示例,在您的代码上配置上述内容后尝试此操作。
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '<h1>Example of HTML text flow</h1>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. <em>Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?</em> <em>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</em><br /><br /><b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i> -> <b>A</b> + <b>B</b> = <b>C</b> -> <i>C</i> - <i>B</i> = <i>A</i> -> <i>C</i> - <i>A</i> = <i>B</i><br /><br /><b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u> <b>Bold</b><i>Italic</i><u>Underlined</u>';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('example_021.pdf', 'I');
?>
回答by D. Schalla
I used pretty often:
我经常使用:
Made so far a good job, there are a few bugs but else it does very well.
到目前为止做得很好,有一些错误,但它做得很好。
With TCPDF I had the most problems, since it seems that it doesn't support the convertation from HTML to PDF that well.
使用 TCPDF 我遇到的问题最多,因为它似乎不支持从 HTML 到 PDF 的转换。
回答by 0xBAADF00D
If you want generate PDF client-side (with a button on the html report), you could use jsPDF (http://parall.ax/products/jspdf)
如果你想生成 PDF 客户端(在 html 报告上有一个按钮),你可以使用 jsPDF ( http://parall.ax/products/jspdf)
回答by 0xBAADF00D
DomPDF looks like what you're looking for.
DomPDF 看起来像您要找的东西。
dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
dompdf 是一个 HTML 到 PDF 的转换器。从本质上讲,dompdf 是(主要)符合 CSS 2.1 的 HTML 布局和用 PHP 编写的渲染引擎。它是一个样式驱动的渲染器:它将下载和读取外部样式表、内联样式标签以及单个 HTML 元素的样式属性。它还支持大多数展示性 HTML 属性。
Reference links:
参考链接:
回答by Manigandan Arjunan
You can use wkhtmltopdfor tcpdfscripts to convert html pages to pdf
您可以使用wkhtmltopdf或tcpdf脚本将 html 页面转换为 pdf