10

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!

Cal Brown
  • 145
  • 1
  • 1
  • 8

2 Answers2

27

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!

ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • Great writeup! I'm one of the devs for DocRaptor. I would also note that DocRaptor has a separate (and much better) JavaScript engine in addition to the awesome PrinceXML PDF engine. – jamespaden Aug 01 '17 at 14:00
  • @jamespaden thank you. I've edited my answer to reflect your comment. It's funny but you were actually one of my contacts at DocRaptor from which I got some of the information! Small world – ctwheels Aug 01 '17 at 14:46
  • Very comprehensive! I work at Kaiomi that runs the Saas services PDFmyURL and HTM2PDF. We offer several custom functions that other platforms don't support - such as full website conversion, watermarking, PDF protection and many more. It makes our services more of a 'all-in-one' concept rather than only (high-quality) web to PDF conversion. – user1914292 Sep 15 '17 at 16:48
  • What are the best recommended python libraries (not services) to create PDF? (What does Google use to generate PDF whether in calendar under Download or from Google Docs or even Print/Save as PDF? Does it work with python?) – Vishal Jun 25 '18 at 04:31
  • @Vishal I would suggest looking at questions that are tagged [tag:python] rather than this question as it's more specific to PHP (although I did provide links to non-PHP based projects). You might have better luck with [this](https://stackoverflow.com/questions/2252726) question. Most of the cloud-based solutions above should work with python as well. – ctwheels Jun 25 '18 at 15:43
2

I would like to add another option in the probable solution list. Aspose.PDF Cloud API also offers features to convert HTML to PDF. It provides SDKs for all popular programming languages.

PHP sample code for HTML to PDF conversion:

//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!!!!";

I work with Aspose as developer evangelist.

Tilal Ahmad
  • 940
  • 5
  • 9