Web/HTML To PDF REST API and Javascript Sample

Convert html to pdf in Javascript with SelectPdfSelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications. The API is easy to use and the integration takes only a few lines of code. The following Javascript code samples uses SelectPdf Online HTML to PDF API.

It’s very easy to convert any URL or raw HTML string to PDF in Javascript using SelectPdf HTML To PDF REST API.

Later Edit: SelectPdf Online REST API can be also used from Node.js using the dedicated Node.js Client Library for SelectPdf API.

Example #1 – How to convert the current page to a PDF with a POST request and stream it to the user in the browser

This code converts an url to pdf in Javascript using SelectPdf REST API through a POST request. The parameters are json encoded (application/json). The content is sent to the browser.

The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF REST API page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.

Notes:

1. Try the above sample from a web server (over HTTP). It will not work if ran directly from the file system.
2. The sample converts the current page to PDF. To be able to use SelectPdf online API, the current page must be accessible over the internet.
3. To convert any other url (and not the current page) to PDF, modify the value for the “url” parameter.