Created
August 28, 2024 17:17
-
-
Save cfjedimaster/24d4e5a78af3c30abd7a3321c686ec3a to your computer and use it in GitHub Desktop.
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
import fs from 'fs'; | |
let REST_API = "https://pdf-services.adobe.io/"; | |
let CLIENT_ID = process.env.CLIENT_ID; | |
let CLIENT_SECRET = process.env.CLIENT_SECRET; | |
let SOURCE_PDF = './form2_filled.pdf'; | |
async function delay(x) { | |
return new Promise(resolve => { | |
setTimeout(() => resolve(), x); | |
}); | |
} | |
async function getAccessToken(id, secret) { | |
const params = new URLSearchParams(); | |
params.append('client_id', id); | |
params.append('client_secret', secret); | |
let resp = await fetch('https://pdf-services-ue1.adobe.io/token', { | |
method: 'POST', | |
headers: { | |
'Content-Type':'application/x-www-form-urlencoded' | |
}, | |
body:params | |
}); | |
let data = await resp.json(); | |
return data.access_token; | |
} | |
async function getUploadData(mediaType, token, clientId) { | |
let body = { | |
'mediaType': mediaType | |
}; | |
body = JSON.stringify(body); | |
let req = await fetch(REST_API+'assets', { | |
method:'post', | |
headers: { | |
'X-API-Key':clientId, | |
'Authorization':`Bearer ${token}`, | |
'Content-Type':'application/json' | |
}, | |
body: body | |
}); | |
let data = await req.json(); | |
return data; | |
} | |
async function uploadFile(url, filePath, mediaType) { | |
let stream = fs.createReadStream(filePath); | |
let stats = fs.statSync(filePath); | |
let fileSizeInBytes = stats.size; | |
let upload = await fetch(url, { | |
method:'PUT', | |
redirect:'follow', | |
headers: { | |
'Content-Type':mediaType, | |
'Content-Length':fileSizeInBytes | |
}, | |
duplex:'half', | |
body:stream | |
}); | |
if(upload.status === 200) return; | |
else { | |
throw('Bad result, handle later.'); | |
} | |
} | |
async function pollJob(url, token, clientId) { | |
let status = null; | |
let asset; | |
while(status !== 'done') { | |
let req = await fetch(url, { | |
method:'GET', | |
headers: { | |
'X-API-Key':clientId, | |
'Authorization':`Bearer ${token}`, | |
} | |
}); | |
let res = await req.json(); | |
status = res.status; | |
if(status === 'done') { | |
asset = res; | |
} else { | |
await delay(2000); | |
} | |
} | |
return asset; | |
} | |
async function getFormDataJob(source, token, clientId) { | |
let body = { | |
'assetID': source.assetID | |
} | |
let resp = await fetch('https://pdf-services-ue1.adobe.io/operation/getformdata', { | |
method: 'POST', | |
headers: { | |
'Authorization':`Bearer ${token}`, | |
'X-API-KEY':clientId, | |
'Content-Type':'application/json' | |
}, | |
body:JSON.stringify(body) | |
}); | |
return resp.headers.get('location'); | |
} | |
(async () => { | |
let accessToken = await getAccessToken(CLIENT_ID, CLIENT_SECRET); | |
console.log('Got our access token.'); | |
let sourceAsset = await getUploadData('application/pdf', accessToken, CLIENT_ID); | |
await uploadFile(sourceAsset.uploadUri, SOURCE_PDF, 'application/pdf'); | |
console.log('Source PDF Uploaded.'); | |
let job = await getFormDataJob(sourceAsset, accessToken, CLIENT_ID); | |
console.log('Job created. Now to poll it.'); | |
let result = await pollJob(job, accessToken, CLIENT_ID); | |
console.log('Job is done.'); | |
let dataReq = await fetch(result.asset.downloadUri); | |
let data = await dataReq.json(); | |
console.log('Result:\n', data); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment