将HTML文件另存为PDF [英] Save html file as PDF

查看:95
本文介绍了将HTML文件另存为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP输出缓冲区创建动态数据查看"页面的HTML文件,然后将该输出另存为HTML文件到服务器,并希望创建此HTML文件的PDF文件(存储在服务器上),但我查看过的每个解决方案都要求您将HTML代码放入变量中,但是我有要自动转换为PDF的.HTML文件,但似乎找不到解决方案. /p>

这里的总体思路是通过电子邮件向用户提供数据查看的副本",因此我认为PDF是最好的,但是如果有其他建议,我会很乐意考虑其他事情.

任何帮助将不胜感激.

谢谢!

解决方案

我一直在努力地用PHP生成PDF,所以这是我几年来发现的东西...


PDF转换工具

  • FPDF

    • 如果您想使用 PDF方法生成PDF文件,则此选项非常好(我将之称为硬币,因为您实际上是逐段生成PDF).

      • 功能包括:

        • 选择度量单位,页面格式和边距
        • 页面页眉和页脚管理
        • 自动分页符
        • 自动换行和文本对齐
        • 图像支持(JPEG,PNG和GIF)
        • 颜色
        • 链接
        • TrueType,Type1和编码支持
        • 页面压缩

      • 注释

        • 性能:Fast
        • 费用:Free
        • 易于使用:Difficult
          • 除非您经常使用它,否则很难使用.
          • 好的文档.
        • 其他:
          • 文件复制(如果需要生成PDF,则必须具有页面的HTML版本和页面的FPDF版本)
  • MPDF
    • 如果您想从HTML和CSS生成PDF文件并且仍然具有其他广泛的PDF定制功能,则此选项非常有用.
      • 功能包括:

        • 从UTF-8编码的HTML生成PDF
        • 它基于FPDF和HTML2FPDF,具有许多增强功能

      • 注意事项
        • 性能:Mediocre
          • 不是最快的,但是可以完成工作
        • 费用:Free
        • 易于使用:Easy
          • 最困难的部分是了解什么是对MPDF有效的HTML和CSS,
          • 出色的文档.
        • 并非所有CSS都受支持,并且某些CSS进行了扩展,引起了混乱
  • PrinceXML
    • 如果您想要高性能和高可靠性,此选项可能是最好的.
      • 功能包括:

        • 强大的布局
          • 页眉和页脚
          • 页码,双面打印
          • 表格,列表,列,浮点数
          • 脚注,交叉引用
        • 网络标准
          • HTML,XHTML,XML,SVG
          • 级联样式表(CSS)
          • JavaScript/ECMAScript
          • JPEG,PNG,GIF,TIFF
        • PDF输出
          • 书签,链接,元数据
          • 加密和文档安全性
          • 字体嵌入和子集
          • PDF附件
        • 轻松集成
          • PHP和Ruby on Rails
          • 用于servlet的Java类
          • .NET for C#和ASP
          • 用于VB6的ActiveX/COM
        • 字体和统一码
          • OpenType字体,TrueType和CFF
          • 紧缩,结扎,小型股
          • 中文,日文,韩文,阿拉伯文,希伯来文,北印度文等
        • 友好的支持
          • 提示电子邮件支持
          • 网络论坛,用户指南
          • 定期升级

      • 注意事项
        • 性能:Fast
        • 价格:$$$
          • 服务器许可证

            • 1个许可证-3,800美元
            • 2个许可证-$ 3,420
            • 3个许可证-$ 3,040
            • 4个许可证-2,850美元
            • 5个以上许可证-2,800美元

          • OEM(最低承诺期限为2年,可以在任意数量的服务器上运行;因此,您可以根据需要创建服务器场)
            • 每月20,000个文档,每月$ 5,000
            • 每月100,000个文档,价格为$ 7,500
            • 每月500,000个文档,费用为10,000美元
          • 他们还有$ 1,900的学术折扣可享受50%的折扣,以及$ 495的桌面许可证以及其他计划(请参阅这里以获取完整列表)
        • 易于使用:Easy
        • 我还没有直接使用PrinceXML(价格昂贵),但是我们目前正在考虑将此作为我们业务的一种选择.
  • DocRaptor

    • 如果您想要高质量的API,则此选项非常好.这是用于创建PDF和XLS文件的云托管选项.在后端使用PrinceXML.

      • 功能包括:

        • 您只需发送HTML,JS和CSS
        • 保证正常运行时间
        • 无限的文档大小
        • 专家支持,包括文档调试

        • 几乎可以提供PrinceXML所做的一切,但是请仔细检查其支持或文档以了解您可能需要的特定内容.
        • 基于API:可与PHP,NodeJS,Ruby,Python,Java,C#一起使用
      • 注意事项
        • 性能:Fast
          • 取决于Internet连接,因此,如果您的Internet断开,那么代码的这一部分也会如此.
        • 定价:$-$$$
          • 当前,他们的定价计划如下(摘自其网站):

            • 基本-125 docs/mo-$ 15/mo
            • 专业-325文档/月-$ 29/月
            • 高级-1,250文档/月-$ 75/月
            • 最高-5,000 docs/mo-$ 149/mo
            • 铜牌-15,000个文档/月-399美元/月
            • 银-40,000个文档/月-$ 1,000/月
            • 金-100,000个文档/月-$ 2,250/mo
            • 企业-∞docs/mo-未列出(与他们联系)

        • 易于使用:Very easy
          • 可能最简单,因为您实际上不处理文档或设置等.您只需发送文件并获取PDF.
          • 出色的文档
        • 我过去曾联系过他们的支持,这实际上非常有帮助.
        • 他们使用专有的JavaScript引擎,使您可以使用延迟或异步JavaScript
  • wkhtmltopdf
    • 如果您想在上面购买的选项(PrinceXML和DocRaptor)之后次之,,此选项非常好.
      • 功能包括:

        • [使用] Qt WebKit渲染引擎
        • 创建要转换为PDF(或图像)的HTML文档.通过该工具运行HTML文档.

      • 注意事项
        • 性能:Fast
        • 费用:Free
        • 易于使用:Easy
        • 我们目前使用此选项,发现它的性能非常好,并且对HTML标记和CSS属性有很好的支持.
        • 如果需要将变量发送到需要生成的PDF页面,则不能使用$_SESSION变量,因为这是通过命令行运行的,并且使用单独的浏览器.您需要通过$_GET变量传递所有变量.
  • 其他选项:许多选项取自问题

其他选项

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

最简单的选择是用HTML创建文档,并向用户发送指向该文档的链接(显然是安全的).这将使用户可以在需要时(以及使用浏览器的任何设备)查看发票,也可以根据需要从浏览器进行打印.这种方法还会为您的网站带来流量,这通常也对业务有利.

我们过去所做的是在网站上创建该文件的链接(受保护),以便他们可以在浏览器中查看该文件,然后有一个按钮来下载发票(该发票仅下载PDF版本)上面列出的一种PDF转换工具(当前为wkhtmltopdf)生成的网页的一部分.

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

我希望这会有所帮助!

I'm using a PHP Output Buffer to create an HTML file of a dynamic 'Data Review' page, I then save this output as an HTML file to the server and would like to create a PDF file of this HTML file (stored on the server) but every solution I've looked at requires you to put in HTML code into a variable, but I have the .HTML file that I want to convert to PDF automatically but can't seem to find a solution.

The overall idea here is to supply the user a 'copy' of the data review via email, so I assumed a PDF would be best, but if there are any other suggestions, I would happily consider something else.

Any help would be greatly appreciated.

Thank you!

解决方案

I've looked heavily into generating PDFs in PHP and so here is what I've found over a few years...


PDF Conversion tools

  • 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

Other options

We deal with many vendors. Some vendors send us PDFs for their invoices or other documents while others send us HTML emails (with all our invoice information in it), and some others even send us links to the invoices.

The easiest option is to create the document in HTML and send users a link to that document (secured obviously). This would allow users to view the invoice whenever they want (and from any device with a browser) and would also allow them to print from the browser if needed. This method also generates traffic to your website which is usually also beneficial to the business.

What we've done in the past is create a link to the file on the website (secured) so that they can view it in the browser, and then have a button to download the invoice (which just downloads a PDF version of that webpage generated with one of the PDF Conversion tools listed above - currently wkhtmltopdf).

In my opinion, the best method would be to combine all delivery approaches into one. Send an email with the file information in the email's HTML content and attach a PDF of that file. Inside the header portion of the email content (at the top of the email), send a link giving the recipient direct access to the webpage containing all the information (located within their account in your secure portal). This allows them to view it in the browser just in case they can't view it properly in their email and in case they don't have a PDF viewer (I know it's rare nowadays, but you'd be surprised just how many people out there have outdated systems - we still need to send faxes to some clients because they still don't have emails; yes still now in 2017, sigh...). On your website, also provide them with a download link for the PDF document (which would again just take the page they are currently on and convert it into a PDF and automatically download it through the browser).

I hope this helps!

这篇关于将HTML文件另存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆