Actor

yinka/PDF-Generator

  • Builds
  • latest 0.4.3 / 2018-02-26
  • Created 2018-02-12
  • Last modified 2018-09-18
  • grade 6

Description

A fork of Jancurn's url-to-pdf act with added name input & and delay until network is idle. Opens a web page in headless Chrome using Puppeteer and prints it to PDF. The input is a JSON object such as: { "url": "https://wiki.org", "pdfOptions": { "format": "a4", "printBackground": true }, "name": "Wiki.pdf" } The "pdfOptions" object is passed to Puppeteer's page.pdf() function - see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions Output is a PDF file.


API

To run the actor, send a HTTP POST request to:

https://api.apify.com/v2/acts/yinka~PDF-Generator/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the actor. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{
  "url": "https://wiki.org",
  "pdfOptions": {
    "format": "a4",
    "printBackground": true
  },
  "name": "Wiki.pdf"
}

Source code

Based on the apify/actor-node-puppeteer:beta Docker image (see docs).

const Apify = require('apify');

Apify.main(async () => {
    console.log('Fetching input...');
    const input = await Apify.getValue('INPUT');
    if (!input || typeof(input.url) !== 'string') {
        throw new Error('Input must be an object with the "url" property');
    }
    
    console.log('Launching headless Chrome...');
    const browser = await Apify.launchPuppeteer();
    const page = await browser.newPage();
    await page.emulateMedia('screen');
    
    console.log(`Loading page (url: ${input.url})...`);
    await page.goto(input.url, {waitUntil: 'load'});
    
    if (input.sleepMillis > 0) {
        console.log(`Sleeping ${input.sleepMillis} millis...`);
        await new Promise((resolve) => setTimeout(resolve, input.sleepMillis));
    }
    
    const opts = input.pdfOptions || {};
    delete opts.path; // Don't store to file
    console.log(`Printing to PDF (options: ${JSON.stringify(opts)})...`);
    const pdfBuffer = await page.pdf(opts);
    
    console.log(`Saving PDF (size: ${pdfBuffer.length} bytes) to output...`);
    await Apify.setValue(input.name, pdfBuffer, { contentType: 'application/pdf' });
    
    const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
    
    // NOTE: Adding disableRedirect=1 param, because for some reason Chrome doesn't allow pasting URLs to PDF
    // that redirect into the browser address bar (yeah, wtf...)
    console.log('PDF file has been stored to:');
    console.log(`https://api.apify.com/v2/key-value-stores/${storeId}/records/${input.name}?disableRedirect=1`);
});