Convert HTML to PDF with PHP

You can convert HTML files to PDF format using PHP. In the tutorial below, we will show you how you can easily do this.

Try HTML2PDF Rocket for FREE

and start converting HTML to PDF instantly!

Code Examples to Convert HTML to PDF with PHP

    • Example #1 - How to use PHP to convert an HTML string to a PDF and save to disk

    • Example #2 - How to convert a PHP webpage to a PDF and stream it to a user as an attachment with PHP

    • Example also shows how you can download the file as an attachment, or show it inline in the web browser.

    • Example #3 - How convert an webpage to an image thumbnail using PHP

    • You can also convert an HTML string to a PDF by supplying the HTML directly. It can be as complex as you like and may include image references and stylesheeet information -- just make sure it is valid HTML

    • Example #4 - How to add a standard header or footer when converting HTML to PDF using PHP

    • Be sure to urlencode any extra parameters you send to the API, especially if they have funny HTML characters in them.

    • Example #5 - Using POST rather than GET when calling the API to convert HTML to PDF's

    • For long strings PHP may have a limitation with GET but you can POST the parameters instead using this syntax

    • Example #6 - PDF Custom HTML Headers and Footers

    • Instead of using the Footer and Header Left/Right parameters which allow basic text, you can have a full HTML based header or footer by including a URL or a string to an HTML snippet in the HeaderUrl/HeaderHtml or FooterUrl/FooterHtml parameters. You can use page numbering, fonts, colours and images to lay out the header as you see fit here is an example of a standard HTML header. As by default page margins are set to 0 to allow full access to the page, make sure you specify are margin parameter large enough to allow the header or footer to fit. Make sure your header/footer code starts with <!DOCTYPE html> and include the <html> and <body> tags

    • Here is an example custom HTML header file with page numbers. Do not forget the doctype.

      This is an example which hides the header on page 1 and 3

    • Example #7 - Batch/Asynchronous Process

    • For PDF conversion taking more than 30 seconds or input/output files more than 6 Mb, you need to use the batch/asynchronous parameters.

Need another PHP example? Would you mind dropping us a quick one line note to say which example you would have liked and we'll put the most commonly used ones here.

About HTML2PDF Rocket

Html2PDF Rocket is based on an open source technology. It is a free and easy to use web service to convert html webpages into PDF files or images. HTML2PDF Rocket is trusted by over 10,500 companies worldwide.

What is an HTML to PDF API?

An HTML to PDF API is a web service that will read your webpage and dynamically generate a PDF or an image. It will convert the contents of a webpage to a PDF file. It handles fairly complex documents and layouts, including images.

Why Convert HTML to PDF?

Converting HTML to PDF is great for reporting, creating brochures, and printing invoices. Just create one HTML or PHP webpage, and use it as a source to generate downloadable PDF or viewable PDF pages. PDF is a fixed-layout document created by Adobe Systems. It is a perfect document for downloading text or HTML content in a file, with consistent layout that does not depend on the device for viewing and does not require internet connection.

Try HTML2PDF Rocket for FREE

and start converting HTML to PDF instantly!

Conversion Tip


The number #1 reason a conversion fails to come out how you expect is because it contains references to external links or images that don't exist - or you are trying to link to localhost! If there is a conversion error, please check the page using your webbrowsers 'developer' network tab to ensure there are no 404 errors.