Actor picture

PDF Generator


A fork of the url-to-pdf actor with added name input and delay until the network is idle. Opens a web page in headless Chrome using Puppeteer and prints the output to a PDF file.

No credit card required

Author's avatarYinka Olukorode
  • Modified
  • Users30
  • Runs4,648

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(, 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:');