HTML to PDF using PHP

The PHP library for converting HTML files to PDF files using Google Chrome browser.

Try for FREE

HTML to PDF features

The PHP SDK for converting HTML files to PDF files.

File conversion library for PHP

ConvertAPI provides a PHP library that allows you to perform a HTML to PDF conversion with just a few lines of code. Convert HTML to PDF documents using PHP programming language with no effort at all!

Execute the following command in your project root:
composer require convertapi/convertapi-php

Or install it manually using ConvertApi autoloader:

Following extensions are required: curl & json
require_once('/path/to/convertapi-php/lib/ConvertApi/autoload.php');

How To Convert HTML to PDF using PHP?

You can convert HTML to PDF in a few simple steps:

HTML to PDF - complete code snippet for PHP

Once you have your authentication in place, simply copy-paste this html to pdf conversion code snippet into your PHP project:

// Code snippet is using the ConvertAPI JavaScript Client: https://github.com/ConvertAPI/convertapi-js

// Code snippet is using the ConvertAPI Node.js Client: https://github.com/ConvertAPI/convertapi-nodejs

// Code snippet is using the ConvertAPI PHP Client: https://github.com/ConvertAPI/convertapi-php

// Code snippet is using the ConvertAPI Java Client: https://github.com/ConvertAPI/convertapi-java

// Code snippet is using the ConvertAPI C# Client: https://github.com/ConvertAPI/convertapi-dotnet

# Code snippet is using the ConvertAPI Ruby Client: https://github.com/ConvertAPI/convertapi-ruby

# Code snippet is using the ConvertAPI Python Client: https://github.com/ConvertAPI/convertapi-python

// Code snippet is using the ConvertAPI Go Client: https://github.com/ConvertAPI/convertapi-go

REM Code snippet is using the command line utility program: https://github.com/ConvertAPI/convertapi-cli

<!-- For conversions with the multiple file result please refer to this example: https://repl.it/@ConvertAPI/HTML-Form-with-multiple-file-result -->

Try the conversion online - no coding required!

You can try out advanced conversion parameters and test the conversion result online using our interactive demo tool. This tool will produce the same conversion output as if you were using the library from your solution, and it will auto-generate the code snippet for you!

Try for FREE!

Conversion parameters

AuthUsername String

HTTP authentication username. Could be used if conversion web page is protected with HTTP authentication.

AuthPassword String

HTTP authentication password. Could be used if conversion web page is protected with HTTP authentication.

Cookies String

Set additional cookies for the page request. Exaple: cookiename1=cookievalue1; cookiename2=cookievalue2; cookiename3=cookievalue3

PageRange String

Set page range. Example 1-10 or 1,2,5.

ConversionDelay Integer

Delay in seconds before page load and PDF creation. Sometimes useful to let web page fully load.

AdBlock Bool

Block ads in converting page.

CookieConsentBlock Bool

Tries to remove EU regulation required cookie warnings from web pages.

JavaScript Bool

Allow web pages to run JavaScript.

WaitElement String

Element selector string of the DOM element. Converter will wait for this element to appear in DOM before conversion begins.

UserJs String

Execute provided JavaScript before conversion begins.

UserCss String

Apply additional CSS before conversion begins.

HideElements String

Element selector string of the DOM elements that needs to be hidden during conversion.

CssMediaType String

Use CSS media type in conversion process. The default screen and print media types are supported but not limited to these. The custom media type, like convertapi can be used too.

LoadLazyContent Bool

Load page images that loads only when they are visible.

ViewportWidth Integer

Sets browser viewport width.

ViewportHeight Integer

Sets browser viewport height.

Background Bool

Convert web page background.

FixedElements Collection

Change fixed elements CSS 'position' property to adapt page for conversion.

Values:   fixed absolute relative hide

ShowElements String

Element selector string of the DOM elements that should be visible during conversion. Other elements will be hidden.

AvoidBreakElements String

CSS selector for the elements that pages should not break.

BreakBeforeElements String

CSS selector for the elements that should apply page break before it.

BreakAfterElements String

CSS selector for the elements that should apply page break after it.

RespectViewport Bool

If true, the converter will generate PDF as the content looks like in the browser. If is set to false, the converter acts like Chrome print to PDF function.

Scale Integer

Set web page scale value in percentage.

PageOrientation Collection

PDF page orientation.

Values:   portrait landscape

PageSize Collection

PDF page size.

Values:   a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b0 b1 b2 b3 b4 b5 letter legal ledger

MarginTop Integer

Set the page top margin in millimeters (mm).

MarginRight Integer

Set the page right margin in millimeters (mm).

MarginBottom Integer

Set the page bottom margin in millimeters (mm).

MarginLeft Integer

Set the page left margin in millimeters (mm).

Header String

This property will insert an HTML header into each page. HTML tags containing the classes pageNumber, totalPages, title, and date will be filled in with the metadata relevant to each individual page. Inline CSS could be utilized to style the HTML provided.

<style>
    .right {
        float: right;
    }

    .left {
        float: left;
    }
</style>
<span class='left'>
    page number 
    <span class='pageNumber'></span>
</span>
<span class='right'>
    date 
    <span class='date'></span>
</span>
Footer String

This property will insert an HTML footer into each page. HTML tags containing the classes pageNumber, totalPages, title, and date will be filled in with the metadata relevant to each individual page. Inline CSS could be utilized to style the HTML provided.

<style>
    .right {
        float: right;
    }

    .left {
        float: left;
    }
</style>
<span class='left'>
    page number 
    <span class='pageNumber'></span>
</span>
<span class='right'>
    date 
    <span class='date'></span>
</span>
PageWidth Integer

Custom page width in millimeters (mm). This option override PageSize option.

PageHeight Integer

Custom page height in millimeters (mm). This option override PageSize option.

CompressPDF Bool

It tries to produce smaller output files but requires Adobe Reader 6, released in 2003 or newer, to view created PDF files.

Try HTML to PDF for free!