Skip to content

Instantly share code, notes, and snippets.

@cnicodeme
Created December 23, 2020 14:54
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/28ade69b269ca0a4af0a7c29c479b747 to your computer and use it in GitHub Desktop.
Save cnicodeme/28ade69b269ca0a4af0a7c29c479b747 to your computer and use it in GitHub Desktop.
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