如何用php将html转换成pdf ?

时间:2022-05-11 09:00:17

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?

你能帮助吗?有什么建议吗?

6 个解决方案

#1


2  

DOMpdf is the best free one.

DOMpdf是最好的免费版本。

If money isn't an issue, PrinceXML is best.

如果钱不是问题,PrinceXML是最好的。

#2


3  

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. Works good, customizable (if you know php), based on pdflib, if i remember right it takes even some CSS. Bad news: slow when the html is big or complex.

DOMPDF:封装html并构建pdf的php类。如果我没记错的话,它甚至需要一些CSS。坏消息:当html很大或很复杂时速度会变慢。

HTML2PS: same of DOMPDF, but this one convert first in .ps (ghostscript), then, in whatever format you need (pdf, jpg, png). For me this is little better than dompdf, but have the same speed problem.. oh, better compatibility with css.

HTML2PS:与DOMPDF相同,但是这个首先转换为.ps (ghostscript),然后转换为您需要的任何格式(pdf、jpg、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(), 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. Is a recent project, anyway, the google.code page is often updated.

wkhtmltopdf:基于webkit (safari的包装),非常快速和强大。似乎是在动态html页面上转换为pdf的最佳方式(atm),使用CSS2的3页xHTML文档只需要2秒。反正是最近的一个项目,谷歌。代码页经常更新。

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. Main Features of tcpdf and it is also having less execution time with great output. For a detailed tutorial on using the two most popular pdf generation classes: TCPDF and FPDF. Please follow this link.

tcpdf -这是一个增强和维护的fpdf版本。tcpdf的主要特性,它的执行时间也更短,输出也更大。关于使用最流行的两个pdf生成类的详细教程:TCPDF和FPDF。请按照这个链接。

See this posts also.

看到这个帖子。

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

#3


2  

The html2pdf library might help you ; I've heard it's generally doing nice job -- but might require you to adapt your HTML a little bit.

html2pdf库可能会帮助您;我听说它通常做得很好——但是可能需要您稍微调整一下HTML。

#4


2  

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

?>

#5


0  

The simplest way to do it is http://www.tcpdf.org/

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

#6


0  

I'm surprised nobody mentioned it, but you can also use online APIs to do the job for you. 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).

我很惊讶没有人提到它,但是你也可以使用在线api来完成这项工作。根据您的使用情况,它可以是免费的,您可以依赖它们的最新特性(如CSS3和web字体),并且您将处理外部化,从而减少硬件工作(或者给它更多的资源来处理其他请求)。

There are a lot of APIs dedicated to converting HTML to PDF. PDFShift is one of them (I work at), and the syntax is really simple (of course it differs from API).

有很多api致力于将HTML转换为PDF。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具有相同的语法,或多或少,并且在价格、性能、处理等方面有所不同。

#1


2  

DOMpdf is the best free one.

DOMpdf是最好的免费版本。

If money isn't an issue, PrinceXML is best.

如果钱不是问题,PrinceXML是最好的。

#2


3  

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. Works good, customizable (if you know php), based on pdflib, if i remember right it takes even some CSS. Bad news: slow when the html is big or complex.

DOMPDF:封装html并构建pdf的php类。如果我没记错的话,它甚至需要一些CSS。坏消息:当html很大或很复杂时速度会变慢。

HTML2PS: same of DOMPDF, but this one convert first in .ps (ghostscript), then, in whatever format you need (pdf, jpg, png). For me this is little better than dompdf, but have the same speed problem.. oh, better compatibility with css.

HTML2PS:与DOMPDF相同,但是这个首先转换为.ps (ghostscript),然后转换为您需要的任何格式(pdf、jpg、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(), 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. Is a recent project, anyway, the google.code page is often updated.

wkhtmltopdf:基于webkit (safari的包装),非常快速和强大。似乎是在动态html页面上转换为pdf的最佳方式(atm),使用CSS2的3页xHTML文档只需要2秒。反正是最近的一个项目,谷歌。代码页经常更新。

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. Main Features of tcpdf and it is also having less execution time with great output. For a detailed tutorial on using the two most popular pdf generation classes: TCPDF and FPDF. Please follow this link.

tcpdf -这是一个增强和维护的fpdf版本。tcpdf的主要特性,它的执行时间也更短,输出也更大。关于使用最流行的两个pdf生成类的详细教程:TCPDF和FPDF。请按照这个链接。

See this posts also.

看到这个帖子。

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

#3


2  

The html2pdf library might help you ; I've heard it's generally doing nice job -- but might require you to adapt your HTML a little bit.

html2pdf库可能会帮助您;我听说它通常做得很好——但是可能需要您稍微调整一下HTML。

#4


2  

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

?>

#5


0  

The simplest way to do it is http://www.tcpdf.org/

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

#6


0  

I'm surprised nobody mentioned it, but you can also use online APIs to do the job for you. 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).

我很惊讶没有人提到它,但是你也可以使用在线api来完成这项工作。根据您的使用情况,它可以是免费的,您可以依赖它们的最新特性(如CSS3和web字体),并且您将处理外部化,从而减少硬件工作(或者给它更多的资源来处理其他请求)。

There are a lot of APIs dedicated to converting HTML to PDF. PDFShift is one of them (I work at), and the syntax is really simple (of course it differs from API).

有很多api致力于将HTML转换为PDF。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具有相同的语法,或多或少,并且在价格、性能、处理等方面有所不同。