Created
December 23, 2020 14:54
-
-
Save cnicodeme/28ade69b269ca0a4af0a7c29c479b747 to your computer and use it in GitHub Desktop.
Convert a PDF using NodeJS and Axios
This file contains hidden or 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
const axios = require('axios'); | |
const fs = require('fs'); | |
function pdfshift(api_key, data) { | |
return new Promise((resolve, reject) => { | |
let asJson = false | |
if ('filename' in data || 'webhook' in data) { | |
asJson = true | |
} | |
axios.request({ | |
method: 'post', | |
url: 'https://api.pdfshift.io/v3/convert/pdf', | |
responseType: (asJson ? 'json' : 'arraybuffer'), | |
data: data, | |
auth: { username: 'api', password: api_key } | |
}).then(resolve).catch(response => { | |
// Handle any error that might have occured | |
reject(response) | |
}) | |
}) | |
} | |
// Here's a sample of what to do | |
pdfshift('your_api_key', { source: 'https://www.example.com' }).then(response => { | |
fs.writeFileSync('example.com.pdf', response.data, "binary", function () {}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment