· 5 years ago · Jan 10, 2020, 10:34 AM
1window.onload = function () {
2 const APPLICATION_ID = ''
3 const SECRET_KEY = ''
4
5 const downloadPdf = async (document, filename) => {
6 const headers = new Headers()
7 headers.append('X-ApplicationID', APPLICATION_ID)
8 headers.append('X-SecretKey', SECRET_KEY)
9
10 const formData = new FormData()
11 formData.append('inputFile', document, 'file.html')
12
13 //optionsJSON has to be a JSON object even if it is empty e.g. '{}'
14 formData.append('optionsJSON', '{}')
15
16 const requestOptions = {
17 method: 'POST',
18 headers,
19 body: formData,
20 redirect: 'follow'
21 }
22
23 const promise = fetch('https://api.docconversionapi.com/convert?outputFormat=PDF', requestOptions)
24
25 const res = await promise
26 const blob = await res.blob()
27
28 const pdf = new File([blob], filename + '.pdf', { type: 'application/pdf' })
29
30 // saveAs(pdf) // fileSaver lib
31
32 return promise
33 }
34
35 //Call download function on file upload
36 document.querySelector('input').addEventListener('change', function () {
37 downloadPdf(this.files[0], 'toto')
38 }, false);
39}