将HTML文件保存为PDF

10
我正在使用PHP输出缓冲区创建一个动态的“数据审查”页面的HTML文件,然后将该输出保存为HTML文件并存储到服务器上,我希望创建一个PDF文件以便于查看(存储在服务器上),但是我看到的每个解决方案都需要将HTML代码放入变量中,而我有一个.HTML文件,想要自动转换成PDF文件,但似乎找不到解决方案。
总体思路是通过电子邮件向用户提供“数据审查”的“副本”,所以我认为PDF可能是最好的选择,但如果有其他建议,我也很愿意考虑其他东西。
非常感谢你的任何帮助。
谢谢!

1
你可以尝试使用 https://github.com/mikehaertl/phpwkhtmltopdf 这个库,它非常棒。 - wahdan
https://tcpdf.org/ 中的 writeHTML 函数可能会有用。 - T.Salvin
为什么不直接向客户发送数据审查页面的链接呢?他们可以在浏览器中查看HTML。 - Ray Paseur
我已经编写了一个基于TFPDF的HTML到PDF库,完全支持UTF-8、中文、日语等,https://github.com/HoldOffHunger/php-html-to-pdf - HoldOffHunger
2个回答

27

我已经深入研究了使用PHP生成PDF,以下是我在几年时间里发现的内容...


PDF转换工具

  • FPDF

    • This option is really good if you want to generate a PDF file using the PDF method (I will coin it this because you literally generate the PDF piece by piece).

      • Features include:

        • Choice of measure unit, page format and margins
        • Page header and footer management
        • Automatic page break
        • Automatic line break and text justification
        • Image support (JPEG, PNG and GIF)
        • Colors
        • Links
        • TrueType, Type1 and encoding support
        • Page compression
      • Notes

        • Performance: Fast
        • Cost: Free
        • Ease of use: Difficult
          • Difficult to use unless you play a lot with it.
          • Good documentation.
        • Other:
          • Duplication of files (need to have HTML version of a page and an FPDF version of a page if you need to generate PDFs)
  • MPDF
    • This option is really good if you want to generate a PDF file from HTML and CSS and still have additional and extensive PDF customization.
      • Features include:
        • PDF generation from UTF-8 encoded HTML
        • It is based on FPDF and HTML2FPDF with a number of enhancements
      • Notes
        • Performance: Mediocre
          • Not the fastest but does the job
        • Cost: Free
        • Ease of use: Easy
          • Hardest part is knowing what is and is not valid HTML and CSS for MPDF)
          • Great documentation.
        • Not all CSS is supported and some CSS is extended causing some confusion
  • PrinceXML
    • This option is probably the best if you want high performance and high reliability.
      • Features include:
        • Powerful Layout
          • Headers and footers
          • Page numbers, duplex printing
          • Tables, lists, columns, floats
          • Footnotes, cross-references
        • Web Standards
          • HTML, XHTML, XML, SVG
          • Cascading Style Sheets (CSS)
          • JavaScript/ECMAScript
          • JPEG, PNG, GIF, TIFF
        • PDF Output
          • Bookmarks, links, metadata
          • Encryption and Document Security
          • Font embedding and subsetting
          • PDF attachments
        • Easy Integration
          • PHP and Ruby on Rails
          • Java class for servlets
          • .NET for C# and ASP
          • ActiveX/COM for VB6
        • Fonts & Unicode
          • OpenType fonts, TrueType and CFF
          • Kerning, Ligatures, Small Caps
          • Chinese, Japanese, Korean, Arabic, Hebrew, Hindi and others
        • Friendly Support
          • Prompt email support
          • Web forum, user guide
          • Regular upgrades
      • Notes
        • Performance: Fast
        • Pricing: $$$
          • Server License
            • 1 license - $3,800
            • 2 license - $3,420
            • 3 license - $3,040
            • 4 license - $2,850
            • 5+ license - $2,800
          • OEM (with minimum commitment of 2 years, can be run on any number of servers; so you can create a server farm if you really need)
            • 20,000 documents/month at $5,000
            • 100,000 documents/month at $7,500
            • 500,000 documents/month at $10,000
          • They also have an academic discount of 50% at $1,900 and a Desktop License for $495 as well as other plans (see here for full list)
        • Ease of use: Easy
        • I have not used PrinceXML directly (pricey), but we are currently looking into this as an option for our business.
  • DocRaptor

    • This option is really good if you want a high quality API. This is a cloud-hosted option for creating PDF and XLS files. Uses PrinceXML in the backend.

      • Features include:

        • You just send HTML, JS, and CSS
        • Uptime guaranteed
        • Unlimited document size
        • Expert support, including document debugging
        • Pretty much offers everything that PrinceXML does, but double check with their support or documentation for anything specific you may require.
        • API-based: Works with PHP, NodeJS, Ruby, Python, Java, C#
      • Notes
        • Performance: Fast
          • Depends on internet connection, so if your internet goes down, so does this part of your code.
        • Pricing: $ - $$$
          • Currently, their pricing plans are as follows (taken from their website):
            • Basic - 125 docs/mo - $15/mo
            • Professional - 325 docs/mo - $29/mo
            • Premium - 1,250 docs/mo - $75/mo
            • Max - 5,000 docs/mo - $149/mo
            • Bronze - 15,000 docs/mo - $399/mo
            • Silver - 40,000 docs/mo - $1,000/mo
            • Gold - 100,000 docs/mo - $2,250/mo
            • Enterprise - ∞ docs/mo - unlisted (contact them)
        • Ease of use: Very easy
          • Probably the easiest because you don't actually deal with the document or setup, etc. You just send your files and get a PDF back.
          • Great documentation
        • I contacted their support in the past and it was actually very helpful.
        • They use a proprietary JavaScript engine that allows you to use delayed or asynchronous JavaScript
  • wkhtmltopdf
    • This option is really good if you want the next best thing behind the purchased options above (PrinceXML and DocRaptor).
      • Features include:
        • [Uses] the Qt WebKit rendering engine
        • Create your HTML document that you want to turn into a PDF (or image). Run your HTML document through the tool.
      • Notes
        • Performance: Fast
        • Cost: Free
        • Ease of use: Easy
          • Uses command line unless you use a library such as the one created by MikeHaertl
        • We currently use this option and find it performs very well and has great support for HTML tags and CSS properties.
        • If you need to send variables to the PDF pages that need to be generated, you cannot use $_SESSION variables as this is ran through the command line and uses a separate browser. You need to pass all your variables through $_GET variables.
  • Other options: Many taken from this question

其他选项

我们与许多供应商打交道。一些供应商向我们发送其发票或其他文件的PDF,而另一些则向我们发送HTML电子邮件(其中包含我们所有的发票信息),还有一些人甚至向我们发送发票链接。

最简单的选择是在HTML中创建文档,并向用户发送该文档的链接(显然要经过安全保护)。这将使用户随时查看发票(并且可以从任何带有浏览器的设备查看),如果需要,还可以允许他们从浏览器中打印。此方法还会为您的网站生成流量,这通常对业务也有益。

我们过去所做的是在网站上创建一个文件链接(受到保护),以便他们可以在浏览器中查看它,然后有一个按钮可下载发票(只需使用上面列出的PDF转换工具之一生成该网页的PDF版本 - 目前是wkhtmltopdf)。

在我看来,最好的方法是将所有交付方式合并为一种。在电子邮件的HTML内容中发送包含文件信息的电子邮件,并附加该文件的PDF。在电子邮件内容的标题部分(在电子邮件顶部),发送一个链接,使收件人可以直接访问包含所有信息的网页(位于他们在您安全门户中的帐户内)。这使他们可以在浏览器中查看它,以防他们无法在电子邮件中正确查看它,并且在他们没有PDF查看器的情况下(我知道现在很少见,但你会惊讶地发现有多少人拥有过时的系统-我们仍然需要向某些客户发送传真,因为他们仍然没有电子邮件; 是的,现在还是在2017年,叹气...)。在您的网站上,还为他们提供PDF文档的下载链接(这将再次将他们当前所在的页面转换为PDF,并通过浏览器自动下载它)。
我希望这能帮到您!

很棒的写作!我是DocRaptor的开发人员之一。我还要指出,除了强大的PrinceXML PDF引擎外,DocRaptor还有一个独立的(而且更好的)JavaScript引擎。 - jamespaden
@jamespaden,谢谢你。我已经编辑了我的回答以反映你的评论。很有趣,但实际上你是我从DocRaptor获得一些信息的联系人之一!世界真小。 - ctwheels
非常全面!我在 Kaiomi 工作,运营 Saas 服务 PDFmyURL 和 HTM2PDF。我们提供其他平台不支持的几个自定义功能,例如完整网站转换、水印、PDF 保护等等。这使得我们的服务更像是一个“一站式”概念,而不仅仅是(高质量的)网页到 PDF 转换。 - user1914292
有哪些最好的推荐的Python库(不是服务)可以创建PDF?(Google在生成PDF时使用了什么,无论是在日历中的下载还是从Google文档中,甚至是打印/另存为PDF?它能与Python一起使用吗?) - Vishal
@Vishal 我建议你查看标记为 [tag:python] 的问题,而不是这个问题,因为它更具体于 PHP(尽管我提供了非基于 PHP 的项目的链接)。你可能会在 这个 问题中有更好的运气。上面大多数基于云的解决方案也应该适用于 Python。 - ctwheels

2
我想在可能的解决方案列表中再添加一个选项。Aspose.PDF Cloud API 也提供将HTML转换为PDF的功能。它为所有流行的编程语言提供 SDK
HTML到PDF转换的PHP示例代码:
//Html file with resource files
$name = "HtmlWithImage.zip";
$html_file_name = "HtmlWithImage.html";
$height = 650;
$width = 250;
$src_path = $name;
$response = pdfApi->getHtmlInStorageToPdf($src_path, $html_file_name, $height, $width);
print_r($response);
echo "Completed!!!!";

我是一名Aspose的开发者推广专员。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接