简体   繁体   English

如何用PHP将html转换为pdf?

[英]How to convert html into pdf with php?

I have html in a mysql table and want to turn it into downloadable pdf files. 我在mysql表中有html,并希望将其转换为可下载的pdf文件。

There are tools to convert files into pdf, but I have not found one that works with php. 有工具将文件转换为PDF格式,但我还没有找到一个适用于PHP的工具。

Can you help? 你能帮我吗? Any suggestions? 有什么建议么?

If your meaning is to create a pdf from php, pdflib will help you. 如果您的意思是从php创建pdf,pdflib将帮助您。

Else, if you want to convert an HTML page in pdf via PHP, the options I know are: 另外,如果你想通过PHP 转换 pdf格式的HTML页面,我知道的选项是:

DOMPDF : php class that wrap the html and build the pdf. DOMPDF :包装html并构建pdf的php类。 Works good, customizable (if you know php), based on pdflib, if i remember right it takes even some CSS. 工作良好,可定制(如果你知道PHP),基于pdflib,如果我记得正确它甚至需要一些CSS。 Bad news: slow when the html is big or complex. 坏消息:当html很大或很复杂时会很慢。

HTML2PS : same of DOMPDF, but this one convert first in .ps (ghostscript), then, in whatever format you need (pdf, jpg, png). HTML2PS :与DOMPDF相同,但是这个以.ps(ghostscript)转换,然后以你需要的任何格式转换(pdf,jpg,png)。 For me this is little better than dompdf, but have the same speed problem .. oh, better compatibility with css. 对我来说这比dompdf好一点,但速度问题相同..哦,与css的兼容性更好。

Those two are php classes, but if you can install some software on the server, and access it through passthru() or system(), give a look to these too: 这两个是php类,但是如果你可以在服务器上安装一些软件,并通过passthru()或system()访问它,那么看看这些:

wkhtmltopdf : based on webkit (safari's wrapper), is really fast and powerful.. seem like is the best one (atm) for convert on the fly html pages to pdf, taking only 2 seconds for a 3 pages xHTML document with CSS2. wkhtmltopdf :基于webkit(safari的包装器),真的是快速而强大的..似乎是最好的一个(atm)用于将html页面转换为pdf,只需2秒即可获得带有CSS2的3页xHTML文档。 Is a recent project, anyway, the google.code page is often updated. 无论如何,这是一个最近的项目,google.code页面经常更新。

htmldoc : this one is a tank, it really never stop/crash.. the project seem death in the 2007, but anyway if you don't need css compatibility this can be nice for you. htmldoc :这个是坦克,它真的永远不会停止/崩溃..项目似乎在2007年死亡,但无论如何,如果你不需要css兼容性,这对你来说很好。

tcpdf - this is an enhanced and maintained version of fpdf. tcpdf - 这是fpdf的增强和维护版本。 Main Features of tcpdf and it is also having less execution time with great output. tcpdf的主要特点是它具有较少的执行时间和较高的输出。 For a detailed tutorial on using the two most popular pdf generation classes: TCPDF and FPDF. 有关使用两个最流行的pdf生成类的详细教程:TCPDF和FPDF。 Please follow this link . 请点击此链接

See this posts also. 也请看这篇文章。

  1. Convert HTML + CSS to PDF with PHP? 用PHP将HTML + CSS转换为PDF?
  2. Which one is the best PDF-API for PHP? 哪一个是PHP最好的PDF-API?
  3. Export a html into PDF in PHP? 用PHP导出HTML到PDF?
  4. Writing HTML with PHP variables to PDF file? 用PHP变量编写HTML到PDF文件?
  5. Tool for exporting html as pdf 用于将html导出为pdf的工具
  6. Converting HTML in PHP File to PDF File 将PHP文件中的HTML转换为PDF文件

You may also try using mPDF it's free and has an extensive documentation. 您也可以尝试免费使用mPDF并拥有大量文档。

Example: 例:

<?php

include('mpdf/mpdf.php'); 

$mpdf=new mPDF(); 

$mpdf->WriteHTML('<img src="images/logo.png">'); //Write HTML

$mpdf->Output(); //Show the output

?>

The html2pdf library might help you ; html2pdf库可能对您有所帮助; I've heard it's generally doing nice job -- but might require you to adapt your HTML a little bit. 我听说它通常做得很好 - 但可能需要你稍微调整你的HTML。

DOMpdf is the best free one. DOMpdf是最好的免费的。

If money isn't an issue, PrinceXML is best. 如果钱不是问题, PrinceXML是最好的。

I'm surprised nobody mentioned it, but you can also use online APIs to do the job for you. 我很惊讶没有人提到它,但您也可以使用在线API为您完成这项工作。 Depending on your usage, it can be free and you can rely on their up-to-date features (like CSS3 and WebFonts) and you externalize the processing, which reduces the hardware work (or give it more resources to handle other requests). 根据您的使用情况,它可以是免费的,您可以依赖它们的最新功能(如CSS3和WebFonts),并将处理外部化,从而减少硬件工作(或为其提供更多资源来处理其他请求)。

There are a lot of APIs dedicated to converting HTML to PDF. 有很多专门用于将HTML转换为PDF的API。 PDFShift is one of them (I work at), and the syntax is really simple (of course it differs from API). PDFShift就是其中之一(我工作),语法非常简单(当然它与API不同)。

For instance, converting an URL to PDF is as simple as doing a curl_ POST request: 例如,将URL转换为PDF就像执行curl_ POST请求一样简单:

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.pdfshift.io/v2/convert/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode({'source': 'https://pdfshift.io/documentation'}),
    CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
    CURLOPT_USERPWD => 'YOUR_API_KEY:'
));

$response = curl_exec($curl);
file_put_content('pdfhsift-documentation.pdf', $response);

Most of the online APIs have the same syntax, more or less, and differs in term of price, performance, processing, etc. 大多数在线API具有相同的语法,或多或少,并且在价格,性能,处理等方面有所不同。

最简单的方法是http://www.tcpdf.org/

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

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