JavaScript preprocessing

Most of the time, JavaScript is not relevant when converting HTML documents to PDF. However, in some cases, it may be necessary to execute JavaScript before creating the PDF (for example, if elements are dynamically created).

If you call the API with prince_options[javascript]=true, Prince will execute JavaScript. It supports most of today’s JavaScript features, as described in the Prince documentation:

Prince supports most of ECMAScript 5th edition (ES5), with the exception of strict mode. ES6 is not fully supported yet, and later editions of ECMAScript are not supported.

If your HTML document contains JavaScript not fully supported by Prince you can call the API with javascript=true to enable JavaScript preprocessing.

If enabled, your document is first passed to an instance of Google Chrome running in headless mode. Chrome parses the HTML, loads all assets, and executes included JavaScript. The resulting DOM is then dumped and used as input for Prince.

JavaScript preprocessing comes with some downsides though:

  • Generating the PDF will take longer (note the time limits for generating PDFs).
  • Some API parameters are ignored during preprocessing:
    • ignore_resource_errors
    • prince_options[baseurl] (use a <base href="…"> element in the HTML instead)
    • prince_options[http_timeout]
    • prince_options[insecure]
    • prince_options[media]
    • prince_options[no_author_style]
    • prince_options[no_default_style]
    • prince_options[no_network]
    • prince_options[no_parallel_downloads]