用PHP将HTML + CSS转换为PDF ?

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

I have an HTML (not XHTML) document that renders fine in Firefox 3 and IE 7. It uses fairly basic CSS to style it and renders fine in HTML.

我有一个HTML(不是XHTML)文档,在Firefox 3和IE 7中表现良好。它使用相当基本的CSS来样式化它,并在HTML中表现得很好。

I'm now after a way of converting it to PDF. I have tried:

我现在想把它转换成PDF格式。我有尝试:

  • DOMPDF: it had huge problems with tables. I factored out my large nested tables and it helped (before it was just consuming up to 128M of memory then dying--thats my limit on memory in php.ini) but it makes a complete mess of tables and doesn't seem to get images. The tables were just basic stuff with some border styles to add some lines at various points;
  • DOMPDF:它的表格有很大的问题。我提出了我的大型嵌套表,它起了作用(之前它只消耗了128M的内存,然后就会死——这是我对php.ini中的内存的限制),但它把表搞得一塌糊涂,似乎无法获得图像。表只是一些基本的东西,带有一些边框样式,在不同的点上添加一些线条;
  • HTML2PDF and HTML2PS: I actually had better luck with this. It rendered some of the images (all the images are Google Chart URLs) and the table formatting was much better but it seemed to have some complexity problem I haven't figured out yet and kept dying with unknown node_type() errors. Not sure where to go from here; and
  • HTML2PDF和HTML2PS:实际上我有更好的运气。它呈现了一些图像(所有的图像都是谷歌图表url),表格格式也更好,但是它似乎有一些复杂的问题,我还没有弄清楚,并且不断地使用未知的node_type()错误。不知道从这里去哪里;和
  • Htmldoc: this seems to work fine on basic HTML but has almost no support for CSS whatsoever so you have to do everything in HTML (I didn't realize it was still 2001 in Htmldoc-land...) so it's useless to me.
  • Htmldoc:这似乎在基本的HTML上工作得很好,但是几乎不支持CSS,所以你必须用HTML做所有的事情(我不知道在Htmldoc-land还是2001年……),所以对我来说,它毫无用处。

I tried a Windows app called Html2Pdf Pilot that actually did a pretty decent job but I need something that at a minimum runs on Linux and ideally runs on-demand via PHP on the Webserver.

我尝试了一个叫做Html2Pdf Pilot的Windows应用程序,它实际上做得相当不错,但我需要的东西至少能在Linux上运行,最好能在Webserver上通过PHP按需运行。

What am I missing, or how can I resolve this issue?

我错过了什么,或者我该如何解决这个问题?

30 个解决方案

#1


430  

Important: Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2018. Online alternatives like PDFShift are better today at this than they were back then.

重要提示:请注意,这个答案写于2009年,它可能不是目前2018年最具成本效益的解决方案。如今,像PDFShift这样的在线选择比过去更好。


Have a look at PrinceXML.

看看PrinceXML。

It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming might not be free either, so if it saves you 10 hours of work, you're home free (since you also need to take into account that the alternative solutions will require you to setup a dedicated server with the right software)

这绝对是最好的HTML / CSS PDF转换器,虽然这不是免费的(但是,嘿,你的编程可能不是免费的,如果它可以节省你10个小时的工作,你回来免费(因为您还需要考虑替代方案将要求您设置一个专用的服务器使用正确的软件)

Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2 ?

哦,是的,我提到过这是第一个(可能也是唯一的)HTML2PDF的解决方案,它可以实现完全的ACID2吗?

PrinceXML Samples

PrinceXML样品

#2


619  

Have a look at wkhtmltopdf . It is open source, based on webkit and free.

看看wkhtmltopdf。它是开源的,基于webkit和免费。

We wrote a small tutorial here.

我们在这里写了一个小教程。

EDIT( 2017 ):

编辑(2017):

If it was to build something today, I wouldn't go that route anymore.
But would use http://pdfkit.org/ instead.
Probably stripping it of all its nodejs dependencies, to run in the browser.

如果今天要做点什么,我不会再走那条路了。但是可以使用http://pdfkit.org/代替。可能会剥离所有nodejs依赖项,在浏览器中运行。

#3


138  

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

经过一些调查和一般性的研究,解决方案似乎是HTML2PDF。DOMPDF在表格、边框甚至是适度复杂的布局上做得很糟糕,htmldoc看起来相当健壮,但几乎完全不考虑CSS,我不想只针对那个程序做HTML布局。

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

HTML2PDF看起来是最有希望的,但是我一直有这个关于node_type的空引用参数的奇怪错误。我终于找到了解决办法。基本上,PHP 5.1。在任何大小的字符串中,x都可以使用regex替换(preg_replace_*)。PHP 5.2.1引入了PHP。ini配置指令称为pcr .backtrack_limit。这个配置参数的作用是限制完成匹配的字符串长度。我不知道为什么会出现这种情况。默认值选择为100,000。为什么这么低的价值?再一次,不知道。

A bug was raised against PHP 5.2.1 for this, which is still open almost two years later.

针对PHP 5.2.1提出了一个bug,该bug在两年后仍然开放。

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

令人震惊的是,当超过极限时,替换就会悄无声息地失败。至少,如果一个错误被提出并记录下来,您就会知道发生了什么,为什么,以及要修改什么来修复它。但是没有。

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

所以我有一个70k的HTML文件要转换成PDF。它需要以下php。ini设置:

  • pcre.backtrack_limit = 2000000; # probably more than I need but that's OK
  • pcre。backtrack_limit = 2000000;可能比我需要的多,但没关系
  • memory_limit = 1024M; # yes, one gigabyte; and
  • memory_limit = 1024;#是的,1 g;和
  • max_execution_time = 600; # yes, 10 minutes.
  • max_execution_time = 600;#是的,10分钟。

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

现在精明的读者可能已经注意到我的HTML文件小于100k。我能猜到我为什么会遇到这个问题的唯一原因是html2pdf将转换为xhtml作为过程的一部分。也许这让我受不了(尽管将近50%的肿胀看起来很奇怪)。无论如何,上述方法都是有效的。

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

html2pdf是一个资源占有者。我的70k文件需要大约5分钟和至少500-600M的RAM来创建一个35页的PDF文件。不幸的是,对于实时下载来说(到目前为止)还不够快,内存使用使内存使用率达到了1000比1 (70k文件的600M内存),这是非常荒谬的。

Unfortunately, that's the best I've come up with.

不幸的是,这是我想出的最好的办法。

#4


114  

Why don’t you try mPDF version 2.0? I used it for creating PDF a document. It works fine.

为什么不试试mPDF版本2.0呢?我用它来创建PDF文档。它将正常工作。

Meanwhile mPDF is at version 5.7 and it is actively maintained, in contrast to HTML2PS/HTML2PDF

与此同时,mPDF的版本是5.7,并且它是主动维护的,而不是HTML2PS/HTML2PDF

But keep in mind, that the documentation can really be hard to handle. For example, take a look at this page: https://mpdf.github.io/.

但是请记住,文档确实很难处理。例如,看看这个页面:https://mpdf.github.io/。

Very basic tasks around html to pdf, can be done with this library, but more complex tasks will take some time reading and "understanding" the documentation.

关于html到pdf的非常基本的任务,可以使用这个库完成,但是更复杂的任务需要一些时间来阅读和“理解”文档。

#5


60  

1) use MPDF !

a) extract in yourfolder

yourfolder)提取

b) create file.php in yourfolder and insert such code:

b)创建文件。在您的文件夹中插入php代码:

<?php
include('../mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<p style="color:red;">Hallo World<br/>Fisrt sentencee</p>');
$mpdf->Output();   exit;
 ?>

c) open file.php from your browser

打开的文件。php从您的浏览器




2) Use pdfToHtml !

1) extract pdftohtml.exe to your root folder:

1)提取pdftohtml。exe到您的根文件夹:

2) inside that folder, in anyfile.php file, put this code (assuming, there is a source example.pdf too):

2)在文件夹内,在任何文件中。php文件,把这个代码(假设,这里有一个源代码示例)。pdf):

<?php
$source="example.pdf";
$output_fold="FinalFolder";

    if (!file_exists($output_fold)) { mkdir($output_fold, 0777, true);}
$result= passthru("pdftohtml $source $output_fold/new_filename",$log);
//var_dump($result); var_dump($log);
?>

3) enter FinalFolder, and there will be the converted files (as many pages, as the source PDF had..)

3)输入FinalFolder(最终文件夹),就会有转换过的文件(如源PDF所含的页数)。

#6


50  

Checkout TCPDF. It has some HTML to PDF functionality that might be enough for what you need. It's also free!

TCPDF结帐。它有一些HTML到PDF的功能,可能足以满足您的需要。这也是免费的!

#7


28  

I suggest DocRaptor (which uses PrinceXML as the "engine")

我建议使用DocRaptor(使用PrinceXML作为“引擎”)

#8


24  

Just to bump the thread, I've tried DOMPDF and it worked perfectly. I've used DIV and other block level elements to position everything, kept it strictly CSS 2.1 and it played veru nicely.

我试过了DOMPDF,它运行得很好。我使用了DIV和其他块级元素来定位所有的东西,严格地保持它的CSS 2.1并且它运行得很好。

#9


23  

Good news! Snappy!!

好消息!时髦的! !

Snappy is a very easy open source PHP5 library, allowing thumbnail, snapshot or PDF generation from a url or a html page. And... it uses the excellent webkit-based wkhtmltopdf

Snappy是一个非常简单的开放源码PHP5库,允许从url或html页面生成缩略图、快照或PDF。和…它使用了优秀的基于webkit的wkhtmltopdf

Enjoy! ^_^

享受吧!^ _ ^

#10


21  

Well if you want to find a perfect XHTML+CSS to PDF converter library, forget it. It's far from possible. Because it's just like finding a perfect browser (XHTML+CSS rendering engine). Do we have one? IE or FF?

好吧,如果你想找到一个完美的XHTML+CSS到PDF转换器库,那就忘掉它吧。它是远离可能的。因为这就像找到一个完美的浏览器(XHTML+CSS呈现引擎)。我们有一个吗?IE或FF吗?

I have had some success with DOMPDF. The thing is that you have to modify your HTML+CSS code to go with the way the library is meant to work. Other than that, I have pretty good results.

我在DOMPDF上取得了一些成功。问题是,您必须修改HTML+CSS代码,以适应库的工作方式。除此之外,我有很好的结果。

See below:

见下文:

Original HTML

原始HTML

Converting HTML to PDF

将HTML转换为PDF

#11


18  

It's already been mentioned, but I'd just like to confirm that mpdf is the easiest, most powerful and most free html to pdf converter out there. The sky's really the limit. You can even generate pdfs of dynamic, user generated data.

已经提到了,但是我想确认mpdf是最简单、最强大、最免费的html到pdf转换器。天空真的是极限。您甚至可以生成动态的、用户生成的数据的pdf文件。

For instance, a client wanted a CMS system so he could update the track list of the music he played at his club. That was no problem, but he also wanted users to be able download a .pdf of the playlist, and so this downloadable pdf had to be updated by the cms too. Thanks to mpdf, with some simple loops and interspersed variables I could do just that. Something that I thought would take me weeks literally took me minutes.

例如,一个客户想要一个CMS系统,这样他就可以更新他在俱乐部演奏的音乐的曲目列表。这不是问题,但他也希望用户可以下载播放列表的.pdf,所以这个可下载的pdf也必须由cms更新。多亏了mpdf,通过一些简单的循环和穿插的变量,我可以做到这一点。一些我认为需要几周时间的事情真的花了我几分钟。

Great article that helped me get started.

伟大的文章帮助我开始。

#12


17  

I am using fpdf to produce pdf files using php. It's working well for me so far to produce simple outputs.

我正在使用fpdf来生成使用php的pdf文件。到目前为止,对我来说,制作简单的输出效果还不错。

#13


17  

The HTML2PDF and HTML2PS that was originally mentioned in opening post was talking about a 2009 package with this link

最初在《开放邮报》中提到的HTML2PDF和HTML2PS都在谈论一个带有这个链接的2009包

But there is a better HTML2PDF

但是还有更好的HTML2PDF

It is based on TCPDF though it is partly in French.

它是基于TCPDF的,虽然部分是法语。

You can have table headers or footers that repeat on the pages and have page numbers and total pages. See its examples. I have been using it for over three years and recommend it.

您可以拥有在页面上重复的表头或页脚,以及页码和总页数。看到它的例子。我已经使用它三年多了,并推荐它。

#14


16  

There's a tutorial on Zend's devzone on generating pdf from php (part 1, part 2) without any external libraries. I never implemented this sort of solution, but since it's all php, you might find it more flexible to implement and debug.

有一个关于Zend的devzone的教程,介绍如何在没有任何外部库的情况下从php生成pdf(第1部分,第2部分)。我从未实现过这种解决方案,但是由于它都是php,所以您可能会发现实现和调试更加灵活。

#15


13  

In terms of cost, using a web-service (API) may in many cases be the more sensible approach. Plus, by outsourcing this process you unburden your own infrastructure/backend and - provided you are using a reputable service - ensure compatibility with adjusting web standards, uptime, short processing times and quick content delivery.

在成本方面,使用web服务(API)在很多情况下可能是更合理的方法。另外,通过外包这个过程,您可以减轻自己的基础设施/后端负担,并且——如果您使用的是信誉良好的服务——确保与调整web标准、正常运行时间、短处理时间和快速内容交付的兼容性。

I've done some research on most of the web services currently on the market, please find below the APIs that I feel are worth mentioning on this thread, in an order based on price/value ratio. All of them are offering pre-composed PHP classes and packages.

我已经对目前市场上的大多数web服务做了一些研究,请在下面找到我认为值得在这个线程上提及的api,以基于价格/价值比的顺序。它们都提供预组合的PHP类和包。

  1. pdflayer.com - Cost: $ - Quality: ☆☆☆☆
  2. pdflayer.com成本:$ -质量:☆☆☆☆
  3. docraptor.com - Cost: $$$ - Quality: ☆☆☆☆☆
  4. docraptor.com成本:$ $ $ -质量:☆☆☆☆☆
  5. pdfcrowd.com - Cost: $$ - Quality: ☆☆☆
  6. pdfcrowd.com成本:$ $ -质量:☆☆☆

Quality:

质量:

Having the high-quality engine PrinceXML as a backbone, DocRaptor clearly offers the best PDF quality, returning highly polished and well converted PDF documents. However, the pdflayer API service gets pretty close here. Pdfcrowd does not necessarily score with quality, but with processing speed.

DocRaptor拥有高质量的引擎PrinceXML作为主干,显然提供了最好的PDF质量,返回高度抛光和良好转换的PDF文档。但是,pdflayer API服务在这里非常接近。Pdfcrowd并不一定以质量得分,而是以处理速度得分。

Cost:

成本:

pdflayer.com - As indicated above, the most cost-effective option here is pdflayer.com, offering an entirely free subscription plan for 100 monthly PDFs and premium subscriptions ranging between $9.99-$119.99. The price for 10,000 monthly PDF documents is $39.99.

pdflayer.com——如上所述,最具成本效益的选择是pdflayer.com,它为100个月的pdf文件提供完全免费的订阅计划,收费从9.99美元到119.99美元不等。每月一万份PDF文档的价格是39.99美元。

docraptor.com - Offering a 7-Day Free Trial period. Premium subscription plans range from $15-$2250. The price for 10,000 monthly PDF documents is ~ $300.00.

提供7天免费试用期。保费认购计划从15美元到2250美元不等。每月1万份PDF文件的价格约为300.00美元。

pdfcrowd.com - Offering 100 PDFs once for free. Premium subscription plans range from $9-$89. The price for 10,000 monthly PDF documents is ~ $49.00.

pdfcrowd.com -免费提供100个pdf文件。保费认购计划从9美元到89美元不等。每月10,000份PDF文件的价格在$49.00左右。

I've used all three of them and this text is supposed to help anyone decide without having to pay for all of them. This text has not been written to endorse any one product and I have no affiliation with any of the products.

我已经使用了这三种方法,这篇文章应该可以帮助任何人做出决定,而不需要为所有这些方法付费。本文本并不是为任何一个产品而写的,我与任何一个产品都没有关联。

#16


12  

If you have access to the command line it's possible to use PhantomJS to create the PDF from an URL (remote or local).

如果可以访问命令行,可以使用PhantomJS从URL(远程或本地)创建PDF。

It works really well, and is a free solution.

它工作得很好,是一个免费的解决方案。

Take a look at this example script made for this exact problem.

请看这个为这个问题而制作的示例脚本。

#17


12  

Darryl Hein's mention above of TCPDF is likely a great idea. Nicola Asuni's code is pretty handy and powerful. The only killer is if you ever plan on merging PDF files with your generated PDF it doesn't have those features. You would have to create the PDF and then merge it using something like PDFTK by Sid Steward (www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).

Darryl Hein在上面提到TCPDF可能是个好主意。Nicola Asuni的代码非常方便和强大。唯一的杀手是,如果你计划将PDF文件与生成的PDF文件合并,它没有这些功能。您必须创建PDF,然后使用Sid Steward的PDFTK合并它(www.pdflabs.com/tools/pdftk- PDF -toolkit/)。

#18


10  

This question is pretty old already, but haven't seen anyone mentioning CutyCapt so I will :)

这个问题已经很老了,但是还没有人提到CutyCapt,所以我会:)

CutyCapt

CutyCapt

CutyCapt is a small cross-platform command-line utility to capture WebKit's rendering of a web page into a variety of vector and bitmap formats, including SVG, PDF, PS, PNG, JPEG, TIFF, GIF, and BMP

CutyCapt是一个小型的跨平台命令行实用程序,用于将WebKit的web页面呈现为各种矢量和位图格式,包括SVG、PDF、PS、PNG、JPEG、TIFF、GIF和BMP

#19


10  

Try grabbing the latest nightly dompdf build - I was using an older version that was a terrible resource hog and took forever to render my pdf. After grabbing a nightly from here.

试着获取最新的夜间dompdf构建——我使用的是一个老版本,这是一个可怕的资源占用,并且花了很长时间来呈现我的pdf。在这里度过了一个夜晚。

It only took a few seconds to generate the PDF - AND it was just as nicely rendered as with PrinceXML / Docraptor. Seems like they've seriously optimized the dompdf code since I last used it!

只需要几秒钟就能生成PDF,而且它和PrinceXML / Docraptor一样漂亮。似乎自从我上次使用dompdf代码以来,他们已经对它进行了认真的优化!

#20


9  

I developed a public API to build PDF files from web pages. It has a nice PHP client class that makes it super easy to use. It uses wkhtmltopdf to render the PDF in the cloud.

我开发了一个公共API来从web页面构建PDF文件。它有一个很好的PHP客户端类,使它非常容易使用。它使用wkhtmltopdf在云中呈现PDF。

No need for anything special in the HTML. No need for absolute URLS in images/css/js links. Works on localhost (dev machine) too.

在HTML中不需要任何特殊的东西。在图像/css/js链接中不需要绝对url。也可以在localhost (dev机器)上工作。

Currently the service has endpoints in 4 Azure regions: US East, US West, EU North, Southeast Asia.

目前该服务在4个Azure区域有终点站:美国东部、美国西部、欧盟北部、东南亚。

It's fast since it uses a proprietary protocol to send the web page contents to the API for conversion to PDF.

由于它使用专有协议将web页面内容发送到API以转换为PDF,所以速度很快。

It's reliable because all endpoints are load balanced.

它是可靠的,因为所有的端点都是负载均衡的。

Free account available for testing or low usage. Details on the web site:

免费帐户可用于测试或低使用率。网站详情:

https://rotativahq.com

https://rotativahq.com

#21


8  

Perhaps you might try and use Tidy before handing the file to the converter. If one of the renderer chokes on some HTML problem (like unclosed tag), it might help it.

也许您可以在将文件提交给转换器之前尝试使用Tidy。如果一个渲染器在某个HTML问题(比如未关闭的标记)上卡住了,它可能会有所帮助。

#22


8  

I dont think a php class will be the best for render an xHtml page with css.

我认为php类不适合用css呈现xHtml页面。

What happen when a new css rule come out? (soon css 3.0...)

当一个新的css规则出现时,会发生什么?(很快css 3.0…)

The best way to render an html page is, obvisiuly, a browser. Firefox 3.0 can natively 'print' in pdf format, torisugary developed an extension (command line print) to use it. Here you'll find it.

呈现html页面的最佳方式是浏览器。Firefox 3.0可以以pdf格式“打印”,tori糖类开发了一个扩展(命令行打印)来使用它。在这里你会找到它。

Anyway, there are still many problmes runninr firefox just as a pdf converter...

无论如何,仍然有很多问题,runninr firefox仅仅作为一个pdf转换器……

At the moment, i think that wkhtmltopdf is the best (that is the one used by the safari browser), fast, quick, awesome. Yes, opensource as well... Give it a look

目前,我认为wkhtmltopdf是最好的(即safari浏览器使用的),快速、快速、棒极了。是的,开源也……给它一个看

#23


7  

Although there are many solutions offered already, I recommend the following two:

虽然已经提供了许多解决方案,但我建议以下两点:

  1. HTM2PDF - offers an API to convert HTML to PDF and also has a PHP SDK, which makes it very easy to implement in PHP; It offers a choice of server locations in Europe, Asia and the USA
  2. HTM2PDF -提供一个将HTML转换为PDF的API,并且还有一个PHP SDK,这使得在PHP中实现它非常容易;它提供在欧洲、亚洲和美国的服务器地点的选择
  3. PDFmyURL - offers an API that does URL and HTML to PDF as well, with roughly the same functionality as HTM2PDF, but works on a load balanced landscape and has been around a little longer
  4. PDFmyURL——提供了一个API,可以将URL和HTML转换为PDF,其功能与HTM2PDF大致相同,但是可以在负载均衡的环境下工作,而且已经运行了一段时间

The thing that's different about these two APIs from all the previously mentioned solutions, is that - besides converting HTML to PDF with CSS and JavaScript - it also offers PDF rights management, watermarking and encryption. Therefore it's an all-in-one solution for those who want to hit the ground running.

这两个api与前面提到的所有解决方案的不同之处在于,除了用CSS和JavaScript将HTML转换为PDF之外,它还提供PDF权限管理、水印和加密。因此,对于那些想要脚踏实地的人来说,这是一个一举两得的解决方案。

Disclaimer: I work for Kaiomi, a company that operates both of these websites.

免责声明:我在Kaiomi工作,一家同时经营这两个网站的公司。

#24


5  

Fine rendering doesn't mean anything. Does it validate?

好的渲染没有任何意义。验证吗?

All browsers do the most they can to just show something on the screen, no matter how bad the input. And of course they do not do the same thing. If you want the same rendering as FireFox, you could use its rendering engine. There are pdf generators for it. It is an awful lot of work, though.

所有浏览器都尽可能在屏幕上显示一些东西,不管输入有多糟糕。当然他们不会做同样的事。如果你想要和火狐一样的渲染,你可以使用它的渲染引擎。有pdf生成器。不过,这是一项艰巨的工作。

#25


5  

TCPDF works fine, no dependencies, is free and constantly bugfixed. It has reasonable speed if supplied HTML/CSS contents is well formated. I normally generate from 50 - 300 kB of HTML input (including CSS) and get PDF output within 1-3 secs with 10 - 15 PDF pages.

TCPDF工作得很好,没有依赖,是免费的,并且经常存在错误。如果提供的HTML/CSS内容格式良好,则具有合理的速度。我通常从50 - 300kb的HTML输入(包括CSS)中生成PDF,并在1-3秒内输出10 - 15个PDF页面。

I strongly recommend using tidy library as HTML pretty formatter before sending anything to TCPDF.

在向TCPDF发送任何东西之前,我强烈建议使用整洁的库作为HTML格式的格式化程序。

#26


4  

I recommend TCPDF or DOMPDF, is that order

我推荐TCPDF或DOMPDF,是这个订单吗

#27


4  

I've tried a lot of different libraries for PHP. All the listed I've tried. In my opinion TCPDF library is the best compromise performance/usability. It's very simply to install and use, also good performance in small medium application. If you need high performance and very big PDF document, use Zend_PDF module, but get ready to coding hard!

我尝试了很多不同的PHP库。我试过的所有列表。在我看来,TCPDF库是最好的折衷性能/可用性。它的安装和使用非常简单,在中小型应用中也有很好的性能。如果您需要高性能和非常大的PDF文档,请使用Zend_PDF模块,但是要做好努力编写代码的准备!

#28


4  

Does the HTML to PDF conversion really need to occur server-side using PHP?

HTML到PDF的转换真的需要使用PHP进行服务器端转换吗?

I just came across jsPDF, a client-side solution using HTML5/JavaScript. The MIT-licensed code is on GitHub, too.

我刚刚遇到了jsPDF,一种使用HTML5/JavaScript的客户端解决方案。mit许可的代码也在GitHub上。

#29


3  

Web API

If there is people who always search for this kind of stuff, there is a free website which allow you to convert html code & pages to pdf. There is also a (very small) api which allow you to get pdf file from url.

如果有人总是搜索这样的东西,有一个免费的网站允许你把html代码和页面转换成pdf。还有一个(非常小的)api允许您从url获取pdf文件。

Check it here

检查在这里

#30


2  

not PHP, but a Java library, which does the thing:

不是PHP,而是一个Java库,它负责:

Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF

飞碟采用XML或XHTML并对其应用符合CSS 2.1的样式表,以便呈现为PDF

It is usable from PHP via system() or a similar call. Although it requires XML well-formedness of the input.

它可以通过system()或类似的调用从PHP中使用。尽管它要求XML具有良好的输入格式。

#1


430  

Important: Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2018. Online alternatives like PDFShift are better today at this than they were back then.

重要提示:请注意,这个答案写于2009年,它可能不是目前2018年最具成本效益的解决方案。如今,像PDFShift这样的在线选择比过去更好。


Have a look at PrinceXML.

看看PrinceXML。

It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming might not be free either, so if it saves you 10 hours of work, you're home free (since you also need to take into account that the alternative solutions will require you to setup a dedicated server with the right software)

这绝对是最好的HTML / CSS PDF转换器,虽然这不是免费的(但是,嘿,你的编程可能不是免费的,如果它可以节省你10个小时的工作,你回来免费(因为您还需要考虑替代方案将要求您设置一个专用的服务器使用正确的软件)

Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2 ?

哦,是的,我提到过这是第一个(可能也是唯一的)HTML2PDF的解决方案,它可以实现完全的ACID2吗?

PrinceXML Samples

PrinceXML样品

#2


619  

Have a look at wkhtmltopdf . It is open source, based on webkit and free.

看看wkhtmltopdf。它是开源的,基于webkit和免费。

We wrote a small tutorial here.

我们在这里写了一个小教程。

EDIT( 2017 ):

编辑(2017):

If it was to build something today, I wouldn't go that route anymore.
But would use http://pdfkit.org/ instead.
Probably stripping it of all its nodejs dependencies, to run in the browser.

如果今天要做点什么,我不会再走那条路了。但是可以使用http://pdfkit.org/代替。可能会剥离所有nodejs依赖项,在浏览器中运行。

#3


138  

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

经过一些调查和一般性的研究,解决方案似乎是HTML2PDF。DOMPDF在表格、边框甚至是适度复杂的布局上做得很糟糕,htmldoc看起来相当健壮,但几乎完全不考虑CSS,我不想只针对那个程序做HTML布局。

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

HTML2PDF看起来是最有希望的,但是我一直有这个关于node_type的空引用参数的奇怪错误。我终于找到了解决办法。基本上,PHP 5.1。在任何大小的字符串中,x都可以使用regex替换(preg_replace_*)。PHP 5.2.1引入了PHP。ini配置指令称为pcr .backtrack_limit。这个配置参数的作用是限制完成匹配的字符串长度。我不知道为什么会出现这种情况。默认值选择为100,000。为什么这么低的价值?再一次,不知道。

A bug was raised against PHP 5.2.1 for this, which is still open almost two years later.

针对PHP 5.2.1提出了一个bug,该bug在两年后仍然开放。

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

令人震惊的是,当超过极限时,替换就会悄无声息地失败。至少,如果一个错误被提出并记录下来,您就会知道发生了什么,为什么,以及要修改什么来修复它。但是没有。

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

所以我有一个70k的HTML文件要转换成PDF。它需要以下php。ini设置:

  • pcre.backtrack_limit = 2000000; # probably more than I need but that's OK
  • pcre。backtrack_limit = 2000000;可能比我需要的多,但没关系
  • memory_limit = 1024M; # yes, one gigabyte; and
  • memory_limit = 1024;#是的,1 g;和
  • max_execution_time = 600; # yes, 10 minutes.
  • max_execution_time = 600;#是的,10分钟。

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

现在精明的读者可能已经注意到我的HTML文件小于100k。我能猜到我为什么会遇到这个问题的唯一原因是html2pdf将转换为xhtml作为过程的一部分。也许这让我受不了(尽管将近50%的肿胀看起来很奇怪)。无论如何,上述方法都是有效的。

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

html2pdf是一个资源占有者。我的70k文件需要大约5分钟和至少500-600M的RAM来创建一个35页的PDF文件。不幸的是,对于实时下载来说(到目前为止)还不够快,内存使用使内存使用率达到了1000比1 (70k文件的600M内存),这是非常荒谬的。

Unfortunately, that's the best I've come up with.

不幸的是,这是我想出的最好的办法。

#4


114  

Why don’t you try mPDF version 2.0? I used it for creating PDF a document. It works fine.

为什么不试试mPDF版本2.0呢?我用它来创建PDF文档。它将正常工作。

Meanwhile mPDF is at version 5.7 and it is actively maintained, in contrast to HTML2PS/HTML2PDF

与此同时,mPDF的版本是5.7,并且它是主动维护的,而不是HTML2PS/HTML2PDF

But keep in mind, that the documentation can really be hard to handle. For example, take a look at this page: https://mpdf.github.io/.

但是请记住,文档确实很难处理。例如,看看这个页面:https://mpdf.github.io/。

Very basic tasks around html to pdf, can be done with this library, but more complex tasks will take some time reading and "understanding" the documentation.

关于html到pdf的非常基本的任务,可以使用这个库完成,但是更复杂的任务需要一些时间来阅读和“理解”文档。

#5


60  

1) use MPDF !

a) extract in yourfolder

yourfolder)提取

b) create file.php in yourfolder and insert such code:

b)创建文件。在您的文件夹中插入php代码:

<?php
include('../mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<p style="color:red;">Hallo World<br/>Fisrt sentencee</p>');
$mpdf->Output();   exit;
 ?>

c) open file.php from your browser

打开的文件。php从您的浏览器




2) Use pdfToHtml !

1) extract pdftohtml.exe to your root folder:

1)提取pdftohtml。exe到您的根文件夹:

2) inside that folder, in anyfile.php file, put this code (assuming, there is a source example.pdf too):

2)在文件夹内,在任何文件中。php文件,把这个代码(假设,这里有一个源代码示例)。pdf):

<?php
$source="example.pdf";
$output_fold="FinalFolder";

    if (!file_exists($output_fold)) { mkdir($output_fold, 0777, true);}
$result= passthru("pdftohtml $source $output_fold/new_filename",$log);
//var_dump($result); var_dump($log);
?>

3) enter FinalFolder, and there will be the converted files (as many pages, as the source PDF had..)

3)输入FinalFolder(最终文件夹),就会有转换过的文件(如源PDF所含的页数)。

#6


50  

Checkout TCPDF. It has some HTML to PDF functionality that might be enough for what you need. It's also free!

TCPDF结帐。它有一些HTML到PDF的功能,可能足以满足您的需要。这也是免费的!

#7


28  

I suggest DocRaptor (which uses PrinceXML as the "engine")

我建议使用DocRaptor(使用PrinceXML作为“引擎”)

#8


24  

Just to bump the thread, I've tried DOMPDF and it worked perfectly. I've used DIV and other block level elements to position everything, kept it strictly CSS 2.1 and it played veru nicely.

我试过了DOMPDF,它运行得很好。我使用了DIV和其他块级元素来定位所有的东西,严格地保持它的CSS 2.1并且它运行得很好。

#9


23  

Good news! Snappy!!

好消息!时髦的! !

Snappy is a very easy open source PHP5 library, allowing thumbnail, snapshot or PDF generation from a url or a html page. And... it uses the excellent webkit-based wkhtmltopdf

Snappy是一个非常简单的开放源码PHP5库,允许从url或html页面生成缩略图、快照或PDF。和…它使用了优秀的基于webkit的wkhtmltopdf

Enjoy! ^_^

享受吧!^ _ ^

#10


21  

Well if you want to find a perfect XHTML+CSS to PDF converter library, forget it. It's far from possible. Because it's just like finding a perfect browser (XHTML+CSS rendering engine). Do we have one? IE or FF?

好吧,如果你想找到一个完美的XHTML+CSS到PDF转换器库,那就忘掉它吧。它是远离可能的。因为这就像找到一个完美的浏览器(XHTML+CSS呈现引擎)。我们有一个吗?IE或FF吗?

I have had some success with DOMPDF. The thing is that you have to modify your HTML+CSS code to go with the way the library is meant to work. Other than that, I have pretty good results.

我在DOMPDF上取得了一些成功。问题是,您必须修改HTML+CSS代码,以适应库的工作方式。除此之外,我有很好的结果。

See below:

见下文:

Original HTML

原始HTML

Converting HTML to PDF

将HTML转换为PDF

#11


18  

It's already been mentioned, but I'd just like to confirm that mpdf is the easiest, most powerful and most free html to pdf converter out there. The sky's really the limit. You can even generate pdfs of dynamic, user generated data.

已经提到了,但是我想确认mpdf是最简单、最强大、最免费的html到pdf转换器。天空真的是极限。您甚至可以生成动态的、用户生成的数据的pdf文件。

For instance, a client wanted a CMS system so he could update the track list of the music he played at his club. That was no problem, but he also wanted users to be able download a .pdf of the playlist, and so this downloadable pdf had to be updated by the cms too. Thanks to mpdf, with some simple loops and interspersed variables I could do just that. Something that I thought would take me weeks literally took me minutes.

例如,一个客户想要一个CMS系统,这样他就可以更新他在俱乐部演奏的音乐的曲目列表。这不是问题,但他也希望用户可以下载播放列表的.pdf,所以这个可下载的pdf也必须由cms更新。多亏了mpdf,通过一些简单的循环和穿插的变量,我可以做到这一点。一些我认为需要几周时间的事情真的花了我几分钟。

Great article that helped me get started.

伟大的文章帮助我开始。

#12


17  

I am using fpdf to produce pdf files using php. It's working well for me so far to produce simple outputs.

我正在使用fpdf来生成使用php的pdf文件。到目前为止,对我来说,制作简单的输出效果还不错。

#13


17  

The HTML2PDF and HTML2PS that was originally mentioned in opening post was talking about a 2009 package with this link

最初在《开放邮报》中提到的HTML2PDF和HTML2PS都在谈论一个带有这个链接的2009包

But there is a better HTML2PDF

但是还有更好的HTML2PDF

It is based on TCPDF though it is partly in French.

它是基于TCPDF的,虽然部分是法语。

You can have table headers or footers that repeat on the pages and have page numbers and total pages. See its examples. I have been using it for over three years and recommend it.

您可以拥有在页面上重复的表头或页脚,以及页码和总页数。看到它的例子。我已经使用它三年多了,并推荐它。

#14


16  

There's a tutorial on Zend's devzone on generating pdf from php (part 1, part 2) without any external libraries. I never implemented this sort of solution, but since it's all php, you might find it more flexible to implement and debug.

有一个关于Zend的devzone的教程,介绍如何在没有任何外部库的情况下从php生成pdf(第1部分,第2部分)。我从未实现过这种解决方案,但是由于它都是php,所以您可能会发现实现和调试更加灵活。

#15


13  

In terms of cost, using a web-service (API) may in many cases be the more sensible approach. Plus, by outsourcing this process you unburden your own infrastructure/backend and - provided you are using a reputable service - ensure compatibility with adjusting web standards, uptime, short processing times and quick content delivery.

在成本方面,使用web服务(API)在很多情况下可能是更合理的方法。另外,通过外包这个过程,您可以减轻自己的基础设施/后端负担,并且——如果您使用的是信誉良好的服务——确保与调整web标准、正常运行时间、短处理时间和快速内容交付的兼容性。

I've done some research on most of the web services currently on the market, please find below the APIs that I feel are worth mentioning on this thread, in an order based on price/value ratio. All of them are offering pre-composed PHP classes and packages.

我已经对目前市场上的大多数web服务做了一些研究,请在下面找到我认为值得在这个线程上提及的api,以基于价格/价值比的顺序。它们都提供预组合的PHP类和包。

  1. pdflayer.com - Cost: $ - Quality: ☆☆☆☆
  2. pdflayer.com成本:$ -质量:☆☆☆☆
  3. docraptor.com - Cost: $$$ - Quality: ☆☆☆☆☆
  4. docraptor.com成本:$ $ $ -质量:☆☆☆☆☆
  5. pdfcrowd.com - Cost: $$ - Quality: ☆☆☆
  6. pdfcrowd.com成本:$ $ -质量:☆☆☆

Quality:

质量:

Having the high-quality engine PrinceXML as a backbone, DocRaptor clearly offers the best PDF quality, returning highly polished and well converted PDF documents. However, the pdflayer API service gets pretty close here. Pdfcrowd does not necessarily score with quality, but with processing speed.

DocRaptor拥有高质量的引擎PrinceXML作为主干,显然提供了最好的PDF质量,返回高度抛光和良好转换的PDF文档。但是,pdflayer API服务在这里非常接近。Pdfcrowd并不一定以质量得分,而是以处理速度得分。

Cost:

成本:

pdflayer.com - As indicated above, the most cost-effective option here is pdflayer.com, offering an entirely free subscription plan for 100 monthly PDFs and premium subscriptions ranging between $9.99-$119.99. The price for 10,000 monthly PDF documents is $39.99.

pdflayer.com——如上所述,最具成本效益的选择是pdflayer.com,它为100个月的pdf文件提供完全免费的订阅计划,收费从9.99美元到119.99美元不等。每月一万份PDF文档的价格是39.99美元。

docraptor.com - Offering a 7-Day Free Trial period. Premium subscription plans range from $15-$2250. The price for 10,000 monthly PDF documents is ~ $300.00.

提供7天免费试用期。保费认购计划从15美元到2250美元不等。每月1万份PDF文件的价格约为300.00美元。

pdfcrowd.com - Offering 100 PDFs once for free. Premium subscription plans range from $9-$89. The price for 10,000 monthly PDF documents is ~ $49.00.

pdfcrowd.com -免费提供100个pdf文件。保费认购计划从9美元到89美元不等。每月10,000份PDF文件的价格在$49.00左右。

I've used all three of them and this text is supposed to help anyone decide without having to pay for all of them. This text has not been written to endorse any one product and I have no affiliation with any of the products.

我已经使用了这三种方法,这篇文章应该可以帮助任何人做出决定,而不需要为所有这些方法付费。本文本并不是为任何一个产品而写的,我与任何一个产品都没有关联。

#16


12  

If you have access to the command line it's possible to use PhantomJS to create the PDF from an URL (remote or local).

如果可以访问命令行,可以使用PhantomJS从URL(远程或本地)创建PDF。

It works really well, and is a free solution.

它工作得很好,是一个免费的解决方案。

Take a look at this example script made for this exact problem.

请看这个为这个问题而制作的示例脚本。

#17


12  

Darryl Hein's mention above of TCPDF is likely a great idea. Nicola Asuni's code is pretty handy and powerful. The only killer is if you ever plan on merging PDF files with your generated PDF it doesn't have those features. You would have to create the PDF and then merge it using something like PDFTK by Sid Steward (www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).

Darryl Hein在上面提到TCPDF可能是个好主意。Nicola Asuni的代码非常方便和强大。唯一的杀手是,如果你计划将PDF文件与生成的PDF文件合并,它没有这些功能。您必须创建PDF,然后使用Sid Steward的PDFTK合并它(www.pdflabs.com/tools/pdftk- PDF -toolkit/)。

#18


10  

This question is pretty old already, but haven't seen anyone mentioning CutyCapt so I will :)

这个问题已经很老了,但是还没有人提到CutyCapt,所以我会:)

CutyCapt

CutyCapt

CutyCapt is a small cross-platform command-line utility to capture WebKit's rendering of a web page into a variety of vector and bitmap formats, including SVG, PDF, PS, PNG, JPEG, TIFF, GIF, and BMP

CutyCapt是一个小型的跨平台命令行实用程序,用于将WebKit的web页面呈现为各种矢量和位图格式,包括SVG、PDF、PS、PNG、JPEG、TIFF、GIF和BMP

#19


10  

Try grabbing the latest nightly dompdf build - I was using an older version that was a terrible resource hog and took forever to render my pdf. After grabbing a nightly from here.

试着获取最新的夜间dompdf构建——我使用的是一个老版本,这是一个可怕的资源占用,并且花了很长时间来呈现我的pdf。在这里度过了一个夜晚。

It only took a few seconds to generate the PDF - AND it was just as nicely rendered as with PrinceXML / Docraptor. Seems like they've seriously optimized the dompdf code since I last used it!

只需要几秒钟就能生成PDF,而且它和PrinceXML / Docraptor一样漂亮。似乎自从我上次使用dompdf代码以来,他们已经对它进行了认真的优化!

#20


9  

I developed a public API to build PDF files from web pages. It has a nice PHP client class that makes it super easy to use. It uses wkhtmltopdf to render the PDF in the cloud.

我开发了一个公共API来从web页面构建PDF文件。它有一个很好的PHP客户端类,使它非常容易使用。它使用wkhtmltopdf在云中呈现PDF。

No need for anything special in the HTML. No need for absolute URLS in images/css/js links. Works on localhost (dev machine) too.

在HTML中不需要任何特殊的东西。在图像/css/js链接中不需要绝对url。也可以在localhost (dev机器)上工作。

Currently the service has endpoints in 4 Azure regions: US East, US West, EU North, Southeast Asia.

目前该服务在4个Azure区域有终点站:美国东部、美国西部、欧盟北部、东南亚。

It's fast since it uses a proprietary protocol to send the web page contents to the API for conversion to PDF.

由于它使用专有协议将web页面内容发送到API以转换为PDF,所以速度很快。

It's reliable because all endpoints are load balanced.

它是可靠的,因为所有的端点都是负载均衡的。

Free account available for testing or low usage. Details on the web site:

免费帐户可用于测试或低使用率。网站详情:

https://rotativahq.com

https://rotativahq.com

#21


8  

Perhaps you might try and use Tidy before handing the file to the converter. If one of the renderer chokes on some HTML problem (like unclosed tag), it might help it.

也许您可以在将文件提交给转换器之前尝试使用Tidy。如果一个渲染器在某个HTML问题(比如未关闭的标记)上卡住了,它可能会有所帮助。

#22


8  

I dont think a php class will be the best for render an xHtml page with css.

我认为php类不适合用css呈现xHtml页面。

What happen when a new css rule come out? (soon css 3.0...)

当一个新的css规则出现时,会发生什么?(很快css 3.0…)

The best way to render an html page is, obvisiuly, a browser. Firefox 3.0 can natively 'print' in pdf format, torisugary developed an extension (command line print) to use it. Here you'll find it.

呈现html页面的最佳方式是浏览器。Firefox 3.0可以以pdf格式“打印”,tori糖类开发了一个扩展(命令行打印)来使用它。在这里你会找到它。

Anyway, there are still many problmes runninr firefox just as a pdf converter...

无论如何,仍然有很多问题,runninr firefox仅仅作为一个pdf转换器……

At the moment, i think that wkhtmltopdf is the best (that is the one used by the safari browser), fast, quick, awesome. Yes, opensource as well... Give it a look

目前,我认为wkhtmltopdf是最好的(即safari浏览器使用的),快速、快速、棒极了。是的,开源也……给它一个看

#23


7  

Although there are many solutions offered already, I recommend the following two:

虽然已经提供了许多解决方案,但我建议以下两点:

  1. HTM2PDF - offers an API to convert HTML to PDF and also has a PHP SDK, which makes it very easy to implement in PHP; It offers a choice of server locations in Europe, Asia and the USA
  2. HTM2PDF -提供一个将HTML转换为PDF的API,并且还有一个PHP SDK,这使得在PHP中实现它非常容易;它提供在欧洲、亚洲和美国的服务器地点的选择
  3. PDFmyURL - offers an API that does URL and HTML to PDF as well, with roughly the same functionality as HTM2PDF, but works on a load balanced landscape and has been around a little longer
  4. PDFmyURL——提供了一个API,可以将URL和HTML转换为PDF,其功能与HTM2PDF大致相同,但是可以在负载均衡的环境下工作,而且已经运行了一段时间

The thing that's different about these two APIs from all the previously mentioned solutions, is that - besides converting HTML to PDF with CSS and JavaScript - it also offers PDF rights management, watermarking and encryption. Therefore it's an all-in-one solution for those who want to hit the ground running.

这两个api与前面提到的所有解决方案的不同之处在于,除了用CSS和JavaScript将HTML转换为PDF之外,它还提供PDF权限管理、水印和加密。因此,对于那些想要脚踏实地的人来说,这是一个一举两得的解决方案。

Disclaimer: I work for Kaiomi, a company that operates both of these websites.

免责声明:我在Kaiomi工作,一家同时经营这两个网站的公司。

#24


5  

Fine rendering doesn't mean anything. Does it validate?

好的渲染没有任何意义。验证吗?

All browsers do the most they can to just show something on the screen, no matter how bad the input. And of course they do not do the same thing. If you want the same rendering as FireFox, you could use its rendering engine. There are pdf generators for it. It is an awful lot of work, though.

所有浏览器都尽可能在屏幕上显示一些东西,不管输入有多糟糕。当然他们不会做同样的事。如果你想要和火狐一样的渲染,你可以使用它的渲染引擎。有pdf生成器。不过,这是一项艰巨的工作。

#25


5  

TCPDF works fine, no dependencies, is free and constantly bugfixed. It has reasonable speed if supplied HTML/CSS contents is well formated. I normally generate from 50 - 300 kB of HTML input (including CSS) and get PDF output within 1-3 secs with 10 - 15 PDF pages.

TCPDF工作得很好,没有依赖,是免费的,并且经常存在错误。如果提供的HTML/CSS内容格式良好,则具有合理的速度。我通常从50 - 300kb的HTML输入(包括CSS)中生成PDF,并在1-3秒内输出10 - 15个PDF页面。

I strongly recommend using tidy library as HTML pretty formatter before sending anything to TCPDF.

在向TCPDF发送任何东西之前,我强烈建议使用整洁的库作为HTML格式的格式化程序。

#26


4  

I recommend TCPDF or DOMPDF, is that order

我推荐TCPDF或DOMPDF,是这个订单吗

#27


4  

I've tried a lot of different libraries for PHP. All the listed I've tried. In my opinion TCPDF library is the best compromise performance/usability. It's very simply to install and use, also good performance in small medium application. If you need high performance and very big PDF document, use Zend_PDF module, but get ready to coding hard!

我尝试了很多不同的PHP库。我试过的所有列表。在我看来,TCPDF库是最好的折衷性能/可用性。它的安装和使用非常简单,在中小型应用中也有很好的性能。如果您需要高性能和非常大的PDF文档,请使用Zend_PDF模块,但是要做好努力编写代码的准备!

#28


4  

Does the HTML to PDF conversion really need to occur server-side using PHP?

HTML到PDF的转换真的需要使用PHP进行服务器端转换吗?

I just came across jsPDF, a client-side solution using HTML5/JavaScript. The MIT-licensed code is on GitHub, too.

我刚刚遇到了jsPDF,一种使用HTML5/JavaScript的客户端解决方案。mit许可的代码也在GitHub上。

#29


3  

Web API

If there is people who always search for this kind of stuff, there is a free website which allow you to convert html code & pages to pdf. There is also a (very small) api which allow you to get pdf file from url.

如果有人总是搜索这样的东西,有一个免费的网站允许你把html代码和页面转换成pdf。还有一个(非常小的)api允许您从url获取pdf文件。

Check it here

检查在这里

#30


2  

not PHP, but a Java library, which does the thing:

不是PHP,而是一个Java库,它负责:

Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF

飞碟采用XML或XHTML并对其应用符合CSS 2.1的样式表,以便呈现为PDF

It is usable from PHP via system() or a similar call. Although it requires XML well-formedness of the input.

它可以通过system()或类似的调用从PHP中使用。尽管它要求XML具有良好的输入格式。