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.

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.