1const Apify = require('apify');
2
3Apify.main(async () => {
4 console.log('Fetching input...');
5 const input = await Apify.getValue('INPUT');
6 if (!input || typeof(input.url) !== 'string') {
7 throw new Error('Input must be an object with the "url" property');
8 }
9
10 console.log('Launching headless Chrome...');
11 const browser = await Apify.launchPuppeteer();
12 const page = await browser.newPage();
13
14 console.log(`Loading page (url: ${input.url})...`);
15 await page.goto(input.url);
16
17 if (input.sleepMillis > 0) {
18 console.log(`Sleeping ${input.sleepMillis} millis...`);
19 await new Promise((resolve) => setTimeout(resolve, input.sleepMillis));
20 }
21
22 const opts = input.pdfOptions || {};
23 delete opts.path;
24 console.log(`Printing to PDF (options: ${JSON.stringify(opts)})...`);
25 const pdfBuffer = await page.pdf(opts);
26
27 console.log(`Saving PDF (size: ${pdfBuffer.length} bytes) to output...`);
28 await Apify.setValue('OUTPUT', pdfBuffer, { contentType: 'application/pdf' });
29
30 const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
31
32
33
34 console.log('PDF file has been stored to:');
35 console.log(`https://api.apify.com/v2/key-value-stores/${storeId}/records/OUTPUT?disableRedirect=1`);
36});