Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert a PDF using NodeJS and Axios
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