HTML to PDF JavaScript

Convert HTML to PDF with Chrome headless: pixel-perfect rendering, JS/CSS injection, AdBlock, waits, headers, and auth.

Web & HTML Tools

HTML to PDF JavaScript Overview

Easily convert HTML to PDF with a developer-friendly JS module built for speed and flexibility. Render pages exactly as they appear in the browser including HTML5, CSS3, JavaScript, responsive designs, and SPAs. Handle high volumes or real-time jobs with ease. Customize output by injecting your own JS or CSS, enabling AdBlock, hiding cookie banners, and more. Ideal for any workflow needing a reliable HTML to PDF microservice or JS module.

Fast and Scalable

High-speed conversions built for large volumes of HTML documents and real-time use.

Pixel-Perfect Rendering

Converts HTML to PDF exactly as displayed in a browser with great flexibility.

Supports Modern Web Tech

Handles and accepts custom CSS3, JavaScript, HTML5, as well as responsive designs.

Dynamic Content Ready

Renders SPAs, interactive forms, charts, and animations.

Custom Rendering Settings

Enable AdBlock, hide Cookie Consents, lazy load content, and inject your custom JS/CSS.

Trusted Security

Your HTML and PDFs are protected under ISO 27001, GDPR, and HIPAA standards.

Customizable Parameters

Fine-tune your automation with these powerful conversion options

File

File Supported formats: .htm .html

File to be converted. Value can be URL or file content.

ConversionDelay

Integer Default: 0

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

Range:   0 .. 30

Version

Collection Default: 148

Specify a particular version of the Chromium engine to render the web page.

Values:   latest 146 147 148

AdBlock

Bool Default: False

Block ads in converting page.

CookieConsentBlock

Bool Default: False

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

JavaScript

Bool Default: True

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 Default: screen

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.

Headers

String

Set custom HTTP headers to include in the request. Format: Header1:Value1|Header2:Value2|Header3:Value3

  • Each header is separated by |.
  • Each name/value pair is separated by :.

LoadLazyContent

Bool Default: False

Load page images that loads only when they are visible.

ViewportWidth

Integer Default: 1366

Sets browser viewport width.

Range:   200 .. 4000

ViewportHeight

Integer Default: 1024

Sets browser viewport height.

Range:   200 .. 4000

Background

Bool Default: True

Convert web page background.

FixedElements

Collection Default: fixed

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.

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. Example: cookiename1=cookievalue1; cookiename2=cookievalue2; cookiename3=cookievalue3

RespectViewport

Bool Default: True

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 Default: 100

Set web page scale value in percentage.

Range:   10 .. 200

PageOrientation

Collection Default: portrait

PDF page orientation.

Values:   portrait landscape

PageSize

Collection Default: letter

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 Default: 10

Set the page top margin in millimeters (mm).

Range:   0 .. 500

MarginRight

Integer Default: 10

Set the page right margin in millimeters (mm).

Range:   0 .. 500

MarginBottom

Integer Default: 10

Set the page bottom margin in millimeters (mm).

Range:   0 .. 500

MarginLeft

Integer Default: 10

Set the page left margin in millimeters (mm).

Range:   0 .. 500

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.

Range:   10 .. 30000

PageHeight

Integer

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

Range:   10 .. 30000

PageRange

String Default: 1-100

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

StoreFile

Bool Default: False

When the StoreFile parameter is set to True, your converted file is written to ConvertAPI’s encrypted, temporary storage and made available via a time-limited secure download URL, valid for up to 3 hours. After this period, the file is permanently deleted.

When StoreFile is set to False, conversion happens entirely in-memory. The raw file bytes are streamed back in the API response without touching disk or external storage, ensuring maximum security and zero persistence so that only you can access the content.

Step-by-Step Guide

Easy HTML to PDF integration programmatically using our simple JavaScript SDK

1. ConvertAPI JavaScript library install

ConvertAPI provides a JavaScript module that allows you to perform a HTML to PDF conversion with just a few lines of code. First, install the convertapi-js@~1.1 package from npm:

NPM package >
npm i convertapi-js@~1.1

If you are interested in our old version of non-module JavaScript library you can find it here.

2. Authenticate your JavaScript library

Next, obtain your API Token by signing up for a free account. Once you sign up, you'll receive 250 free conversions instantly! Grab your API Token from the account dashboard, and authenticate the ConvertAPI JavaScript library like this:

import ConvertApi from 'convertapi-js'
let convertApi = ConvertApi.auth('api_token')

3. Convert HTML to PDF using JavaScript in no time!

Once you have your authentication in place, simply copy-paste this html to pdf conversion code snippet into your JavaScript project. Convert HTML to PDF documents using JavaScript with no effort at all!

Integrate within minutes

Easy HTML to PDF automation using our simple JavaScript SDK

Try the HTML to PDF conversion online

Try it Free

Compatible With all JavaScript Frameworks & Tools

Compatible with JavaScript Compatible with TypeScript Compatible with React Compatible with Vue.js Compatible with Angular Compatible with Webpack Compatible with WebStorm

Businesses trust us

Highest rated File Conversion API on major B2B software listing platforms: Capterra, G2, and Trustpilot.

"ConvertAPI has been a game-changer for our document automation workflows. Their conversion accuracy and API reliability are unmatched in the industry for over 7 years."

"ConvertAPI is a reliable, cost-effective solution with a proven track record of stability. It has grown significantly in maturity, adopting enterprise-grade practices over the years."

"We've integrated ConvertAPI across our entire document processing platform. The performance is exceptional and the support team is always responsive. Highly recommended!"

Enterprise-Grade Security

We ensure that all document processing is handled securely in the cloud, adhering to industry-leading standards like ISO 27001, GDPR, and HIPAA. To enhance security even further, we can ensure that no files or data are stored on our servers and never leave your country.

Learn more about security

Ready to Streamline Your File Conversions?