Created
September 6, 2019 14:13
-
-
Save cnicodeme/6850f040dc43ca7bf7d8792ed1fb3c31 to your computer and use it in GitHub Desktop.
Browser side version of PDFShift conversion script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: | |
// pdfshift({'source': 'https://en.wikipedia.org/wiki/PDF', 'use_print': true}, 'your_api_key').then(...) | |
function pdfshift(data, apiKey) { | |
/** | |
* Javascript function for PDFShift | |
* @param apiKey: Your API key from PDFShift. Optional. Can be null | |
* @param data: Parameters to send to PDFShift. Must contains at least the "source" | |
*/ | |
return new Promise(function (resolve, reject) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', 'https://api.pdfshift.io/v2/convert/', true); | |
xhr.setRequestHeader("Content-Type", "application/json"); | |
if (apiKey) { | |
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(apiKey + ':')); | |
} | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState === 4) { | |
if (xhr.status === 200) { | |
// The request was successful! | |
// The xhr.responseText contains the PDF | |
resolve(xhr.responseText); | |
} else { | |
// An error occured! | |
var json = JSON.parse(xhr.responseText); | |
reject(json); | |
} | |
} | |
}; | |
xhr.send(JSON.stringify(data)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment