PHP 中的 HTML 到 PDF 转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18056021/
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
HTML to PDF converter in PHP
提问by Sam
To generate pdf from html page I have tried many pdf libraries like FPDF,HTML2FPDF,CEZPDF,DOMPDF and MPDF.
为了从 html 页面生成 pdf,我尝试了许多 pdf 库,如FPDF、HTML2FPDF、CEZPDF、DOMPDF 和 MPDF。
But in every pdf library I found some issues which don't produce required layout of the page.
但是在每个 pdf 库中,我都发现了一些无法生成所需页面布局的问题。
For example in MPDF I found page breaks but it was a nice html to pdf converter.
例如,在 MPDF 中,我发现了分页符,但它是一个不错的 html 到 pdf 转换器。
So kindly suggest me which library should I use to generate pdf? I don't care if it needs to purchase.
所以请建议我应该使用哪个库来生成pdf?我不在乎它是否需要购买。
Thanks in advance.
提前致谢。
回答by Shankar Damodaran
I wonder why you haven't tried TCPDFwhich is currently the most stable version. Hereis the clean example of what you are looking for.
我想知道你为什么没有尝试过目前最稳定的版本TCPPDF。这是您正在寻找的干净示例。
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 049');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 049', PDF_HEADER_STRING);
]$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10);
$pdf->AddPage();
$html = '<h1>Test TCPDF Methods in HTML</h1>
<h2 style="color:red;">IMPORTANT:</h2>
<span style="color:red;">If you are using user-generated content, the tcpdf tag can be unsafe.<br />
You can disable this tag by setting to false the <b>K_TCPDF_CALLS_IN_HTML</b> constant on TCPDF configuration file.</span>
<h2>write1DBarcode method in HTML</h2>';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array('CODE 39', 'C39', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
$html .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array('CODE 128', 'C128', '', '', 80, 30, 0.4, array('position'=>'S', 'border'=>true, 'padding'=>4, 'fgcolor'=>array(0,0,0), 'bgcolor'=>array(255,255,255), 'text'=>true, 'font'=>'helvetica', 'fontsize'=>8, 'stretchtext'=>4), 'N'));
$html .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
$html .= '<tcpdf method="AddPage" /><h2>Graphic Functions</h2>';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array(0));
$html .= '<tcpdf method="SetDrawColor" params="'.$params.'" />';
$params = TCPDF_STATIC::serializeTCPDFtagParameters(array(50, 50, 40, 10, 'DF', array(), array(0,128,255)));
$html .= '<tcpdf method="Rect" params="'.$params.'" />';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('example_049.pdf', 'I');
?>
回答by Shafeeque
You can try this http://www.tcpdf.org/. Site providing some good examples and also you can design page layout as your own styles
你可以试试这个http://www.tcpdf.org/。网站提供了一些很好的例子,您也可以将页面布局设计为您自己的风格
回答by Zsolt Tolvaly
I've used TCPDF and DOMPDF.
我使用过 TCPDF 和 DOMPDF。
FOR:
为了:
- TCPDF:
- You can design the page layout very easily
- DOMPDF:
- almost every css feature support, if you design the page in html, css, the printed pdf will be the same. This is the easiest library available on the internet.
- you also can use php scripts, and DOMPDF will run it on pdf generation.
- TCPDF:
- 您可以非常轻松地设计页面布局
- DOMPDF:
- 几乎所有的 css 功能都支持,如果你用 html、css 设计页面,打印出来的 pdf 是一样的。这是互联网上最简单的图书馆。
- 您也可以使用 php 脚本,DOMPDF 将在 pdf 生成上运行它。
Against:
反对:
- TCPDF:
- you have to write a lot...
- very hard to use css
- DOMPDF:
- on large tables, it has a huge memory consumption, and could crash.
- tricky page layout design ( header and footer)
- TCPDF:
- 你必须写很多...
- 很难使用 css
- DOMPDF:
- 在大型表上,它具有巨大的内存消耗,并且可能会崩溃。
- 棘手的页面布局设计(页眉和页脚)
回答by user1914292
Try HTM2PDF - it supports page breaking, single page layouts and many other options.
试试 HTM2PDF - 它支持分页、单页布局和许多其他选项。
A HTML to PDF APIis available for all programming languages as well as a PHP HTML to PDF SDK.
一个HTML到PDF API可用于所有的编程语言,以及PHP的HTML到PDF SDK。