HTML to PDF JavaScript Execution

The DocRaptor HTML to PDF API offers two different engines for JavaScript execution. Both options are disabled by default (it makes your document processing a lot faster).

Note: This page is about executing JavaScript while converting your HTML document into a PDF. You might also be interested in our Top Node.js HTML to PDF libraries guide if you want to convert your document using JavaScript.

Enabling JavaScript

JavaScript is disabled by default (to speed up document generation), but if you want charts, custom fonts, or any other JavaScript-based functionality, you will need to enable JavaScript on your API call. Simply set the javascript parameter to true, or for the Prince engine, set the prince_options[javascript] parameter to true.

Selecting a JavaScript Engine

Our primary JavaScript engine is recommended for most users. It has been specifically selected to support popular JavaScript tools and libraries like React, Adobe Typekit, and HighCharts. By default, we stop running JavaScript when your page is finished rendering. If you have any delayed or asynchronous JavaScript on your page, you can customize a rendering delay to wait for your JavaScript to finish loading.

Additionally, our HTML to PDF generator, Prince, has their own JavaScript engine. It offers advanced, PDF-specific functionality, such as calling JavaScript functions from CSS, multi-pass rendering, and PDF box access. It also supports the creation of canvas objects, which our standard library does not. Prince is not a web browser, though, and sometimes their JavaScript engine behaves differently or doesn't fully support all the same JavaScript functions as modern web browsers.

If you run into ES6 compatibility problems with either engine, we have a JavaScript troubleshooting guide to assist with working around those issues.

Referencing JavaScript Assets

DocRaptor must know where on the internet to find all your external assets. That means you must do one of the following:

  1. Use an absolute URL (begins in http:// or https://)

  2. Use the prince_options[baseurl] API parameter to inform us which URL to use to create absolute URLs on your behalf

  3. Use a <base> tag in your document <head>, which will also inform us which URL to use to create absolute URLs on your behalf

If you're working on a local development machine, we have some additional tips on how to test your DocRaptor setup while on localhost.

Ready to get started? Try DocRaptor for free with unlimited test documents.