JavaScript preprocessing
In most HTML to PDF conversion scenarios, JavaScript execution is not necessary. However, certain cases require JavaScript to run before PDF generation, such as when elements are dynamically created.
To enable JavaScript execution, add the parameter prince_options[javascript]=true
to your API call. Prince supports most modern JavaScript features as outlined 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.
Advanced JavaScript Preprocessing
For HTML documents containing JavaScript that exceeds Prince’s support capabilities, you can enable JavaScript preprocessing by setting javascript=true
in your API call.
When preprocessing is enabled, your document is first processed by Google Chrome running in headless mode. Chrome parses the HTML, loads all assets, and executes all JavaScript. The resulting DOM is then captured and passed to Prince for PDF generation.
Delay dumping the DOM
By default, the DOM is captured immediately after the page loads. In some scenarios, you may need to delay this capture to allow asynchronous scripts to complete or animations to finish.
The DOM capture process is controlled by the JavaScript function EuroPDF.readyToRender()
. The DOM is captured as soon as this function returns true. To implement a delay, you can override this function with your own custom logic.
Here is an example that delays DOM capture by 5 seconds:
let renderingFinished = false
EuroPDF.readyToRender = () => renderingFinished
// wait for 5 seconds before dumping the DOM
setTimeout(() => { renderingFinished = true }, 5000)
Limitations of JavaScript Preprocessing
While JavaScript preprocessing offers enhanced capabilities, it does have a few limitations:
- PDF generation takes longer (be aware of the time limits for PDF generation)
- The following API parameters are not supported 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]