Act

apify/example-puppeteer

  • Builds
  • latest 0.0.20 / 2018-04-04
  • Created 2017-10-12
  • Last modified 2018-04-04
  • grade 65

Description

Example showing how to use headless Chromium with Puppeteer to open a web page, determine its dimensions, save a screenshot and print the page to PDF. For more information about Puppeteer, please see https://github.com/GoogleChrome/puppeteer This act must use image with Puppeteer (Node.js 8 + Puppeteer on Debian).


API

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

https://api.apify.com/v2/acts/apify~example-puppeteer/runs?token=<YOUR_API_TOKEN>

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


Example input

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

{ "url": "http://wikipedia.com" }

Source code

Based on the apify/actor-node-chrome Docker image (see docs).

const Apify = require('apify');

Apify.main(async () => {
    const input = await Apify.getValue('INPUT');
    
    if (!input || !input.url) throw new Error('Invalid input, must be a JSON object with the "url" field!');
    
    console.log('Launching Puppeteer...');
    const browser = await Apify.launchPuppeteer();
    
    console.log(`Opening URL: ${input.url}`);  
    const page = await browser.newPage();
    await page.goto(input.url);
    
    // Get the "viewport" of the page, as reported by the page.
    console.log('Determining page dimensions...');
    const dimensions = await page.evaluate(() => ({
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        deviceScaleFactor: window.devicePixelRatio
    }));
    console.log(`Dimension: ${JSON.stringify(dimensions)}`);
    
    // Grab a screenshot
    console.log('Saving screenshot...');
    const screenshotBuffer = await page.screenshot({ fullPage: true });
    await Apify.setValue('screenshot.png', screenshotBuffer, { contentType: 'image/png' });
    
    console.log('Saving PDF snapshot...');
    const pdfBuffer = await page.pdf({ format: 'A4'});
    await Apify.setValue('page.pdf', pdfBuffer, { contentType: 'application/pdf' });
    
    console.log('Closing Puppeteer...');
    await browser.close();
    
    console.log('Done.');
    console.log('You can check the output in the key-value on the following URLs:');
    const storeId = process.env.APIFY_DEFAULT_KEY_VALUE_STORE_ID;
    console.log(`- https://api.apify.com/v2/key-value-stores/${storeId}/records/screenshot.png`);
    // 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(`- https://api.apify.com/v2/key-value-stores/${storeId}/records/page.pdf?disableRedirect=1`);
});