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 .htmlFile to be converted. Value can be URL or file content.
ConversionDelay
Integer Default: 0Delay in seconds before page load and PDF creation. Sometimes useful to let web page fully load.
Range: 0 .. 30Version
Collection Default: 148Specify a particular version of the Chromium engine to render the web page.
Values: latest 146 147 148AdBlock
Bool Default: FalseBlock ads in converting page.
CookieConsentBlock
Bool Default: FalseTries to remove EU regulation required cookie warnings from web pages.
JavaScript
Bool Default: TrueAllow web pages to run JavaScript.
WaitElement
StringElement selector string of the DOM element. Converter will wait for this element to appear in DOM before conversion begins.
UserJs
StringExecute provided JavaScript before conversion begins.
UserCss
StringApply additional CSS before conversion begins.
HideElements
StringElement selector string of the DOM elements that needs to be hidden during conversion.
CssMediaType
String Default: screenUse 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
StringSet 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: FalseLoad page images that loads only when they are visible.
ViewportWidth
Integer Default: 1366Sets browser viewport width.
Range: 200 .. 4000ViewportHeight
Integer Default: 1024Sets browser viewport height.
Range: 200 .. 4000Background
Bool Default: TrueConvert web page background.
FixedElements
Collection Default: fixedChange fixed elements CSS 'position' property to adapt page for conversion.
Values: fixed absolute relative hideShowElements
StringElement selector string of the DOM elements that should be visible during conversion. Other elements will be hidden.
AvoidBreakElements
StringCSS selector for the elements that pages should not break.
BreakBeforeElements
StringCSS selector for the elements that should apply page break before it.
BreakAfterElements
StringCSS selector for the elements that should apply page break after it.
AuthUsername
StringHTTP authentication username. Could be used if conversion web page is protected with HTTP authentication.
AuthPassword
StringHTTP authentication password. Could be used if conversion web page is protected with HTTP authentication.
Cookies
StringSet additional cookies for the page request. Example: cookiename1=cookievalue1; cookiename2=cookievalue2; cookiename3=cookievalue3
RespectViewport
Bool Default: TrueIf 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: 100Set web page scale value in percentage.
Range: 10 .. 200PageOrientation
Collection Default: portraitPDF page orientation.
Values: portrait landscapePageSize
Collection Default: letterPDF page size.
Values: a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b0 b1 b2 b3 b4 b5 letter legal ledgerMarginTop
Integer Default: 10Set the page top margin in millimeters (mm).
Range: 0 .. 500MarginRight
Integer Default: 10Set the page right margin in millimeters (mm).
Range: 0 .. 500MarginBottom
Integer Default: 10Set the page bottom margin in millimeters (mm).
Range: 0 .. 500MarginLeft
Integer Default: 10Set the page left margin in millimeters (mm).
Range: 0 .. 500Header
StringThis 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
StringThis 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
IntegerCustom page width in millimeters (mm). This option override PageSize option.
Range: 10 .. 30000PageHeight
IntegerCustom page height in millimeters (mm). This option override PageSize option.
Range: 10 .. 30000PageRange
String Default: 1-100Set page range. Example 1-10 or 1,2,5.
StoreFile
Bool Default: FalseWhen 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 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:
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
Compatible With all JavaScript Frameworks & Tools
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