Actor picture

HTML to PDF Converter

jancurn/url-to-pdf

Loads a web page in headless Chrome using Puppeteer and prints it to PDF. The input is a JSON object and output is a PDF file.

No credit card required

Author's avatarJan Čurn
  • Modified
  • Users213
  • Runs113,463
Actor picture

HTML to PDF Converter

Based on the apify/actor-node-puppeteer 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();
    
    console.log(`Loading page (url: ${input.url})...`);
    await page.goto(input.url);
    
    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('OUTPUT', 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/OUTPUT?disableRedirect=1`);
});