1const fs = require('fs');
2const util = require('util');
3const exec = util.promisify(require('child_process').exec);
4const Apify = require('apify');
5const requestPromise = require('request-promise');
6
7Apify.main(async () => {
8
9
10 const input = await Apify.getValue('INPUT');
11 if (!input || !input.url) throw new Error('Received invalid input');
12
13 console.log(`Downloading PDF file: ${input.url}`);
14 const options = {
15 url: input.url,
16 encoding: null
17 };
18 const response = await requestPromise(options);
19 const buffer = Buffer.from(response);
20
21 const tmpTarget = 'temp.pdf';
22 console.log('Saving PDF file to: ' + tmpTarget);
23 fs.writeFileSync(tmpTarget, buffer);
24
25 const { stdout, stderr } = await exec('pdf2htmlEX --zoom 1.3 temp.pdf');
26 console.log('stdout:', stdout);
27 console.log('stderr:', stderr);
28
29 const htmlBuffer = fs.readFileSync('temp.html');
30
31 console.log(`Saving HTML (size: ${htmlBuffer.length} bytes) to output...`);
32 await Apify.setValue('OUTPUT', htmlBuffer, { contentType: 'text/html' });
33
34 const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
35
36
37
38 console.log('HTML file has been stored to:');
39 console.log(`https://api.apify.com/v2/key-value-stores/${storeId}/records/OUTPUT`);
40});