Skip to content

Instantly share code, notes, and snippets.

@cnicodeme
Created September 6, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnicodeme/6850f040dc43ca7bf7d8792ed1fb3c31 to your computer and use it in GitHub Desktop.
Save cnicodeme/6850f040dc43ca7bf7d8792ed1fb3c31 to your computer and use it in GitHub Desktop.
Browser side version of PDFShift conversion script.
// 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