Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save html file as PDF

Tags:

html

php

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!

like image 632
Cal Brown Avatar asked Jul 31 '17 15:07

Cal Brown


2 Answers

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
    • Cloud-based
      • HTM2PDF: Source
      • PDFmyURL: Source
      • PDFCrowd: Source 1, Source 2
      • PDFLayer: Source
      • RotativaHQ: Source
    • Client-side
      • jsPDF: Source
    • Server-side
      • TCPDF - Many people recommended this option: Source
      • ZendPDF - Part of Zend Framework: Source
      • flying-saucer - Java library usable via system(): Source 1, Source 2
      • CutyCapt: Source
      • PhantomJS: Source
      • Snappy: Source
      • DOMPDF: Source
      • HTML2PDF: Source
      • PDFReactor
      • HTML2PS - No solid links for this project, so I linked to Google search for it
      • Apache FOP
      • PHP - PHP has its native library for creating PDFs, I assume this is probably one of the most difficult ways to go about doing this, but if you're really adventurous, why not?
      • PDFLib - Many other libraries are based off this one
      • ReportLab - Python-based
      • iText - Java-based: Source
      • ActivePDF
      • WeasyPrint - Python-based. This is apparently really good?
      • xHTML2PDF - Python-based

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!

like image 107
ctwheels Avatar answered Nov 03 '22 01:11

ctwheels


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.

like image 21
Tilal Ahmad Avatar answered Nov 03 '22 00:11

Tilal Ahmad