SelectPdf 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
function SaveAsPdf() { var api_endpoint = "https://selectpdf.com/api2/convert/"; var api_key = "your license key here"; var url = window.location.href; // current page var params = { key: api_key, url: url } var xhr = new XMLHttpRequest(); xhr.open('POST', api_endpoint, true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.responseType = 'arraybuffer'; xhr.onload = function (e) { if (this.status == 200) { //console.log('Conversion to PDF completed ok.'); var blob = new Blob([this.response], { type: 'application/pdf' }); var url = window.URL || window.webkitURL; var fileURL = url.createObjectURL(blob); //window.location.href = fileURL; //console.log('File url: ' + fileURL); var fileName = "Document.pdf"; if (navigator.appVersion.toString().indexOf('.NET') > 0) { // This is for IE browsers, as the alternative does not work window.navigator.msSaveBlob(blob, fileName); } else { // This is for Chrome, Firefox, etc. var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; a.href = fileURL; a.download = fileName; a.click(); } } else { //console.log("An error occurred during conversion to PDF: " + this.status); alert("An error occurred during conversion to PDF.\nStatus code: " + this.status + ", Error: " + String.fromCharCode.apply(null, new Uint8Array(this.response))); } }; xhr.send(JSON.stringify(params)); } |
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.