Community Hackathon: Charts in Email - Nov. 8th, 2019

Okay, here’s the headless browser solution with Node.js and Puppeteer.

Requrements: any environment with Node.js that can handle headless Chromium (think ~ 1GB of RAM), egress traffic, and not drain your pockets.

  1. npm install puppeteer
const url = 'https://coda.io/d/Paul-s-Plan-Demo_dqOyKWdqtC6/History_su1-W#_lufvi';
const output = `./report-${Date.now()}.pdf`;
const width = '1200px';

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  
  // You can also play around with browser window dimensions to get better results:
  // await page.setViewport({ width: 1200, height: 800 });

  // Wait for the Coda doc to finish loading
  await page.waitForSelector('*[class^=document_splash_screen]', { hidden: true });

  // Hide the sections panel (this must be done while not yet in Print mode)
  await page.waitForSelector('*[class^=left_rail_view--toggleButton]', { visible: true });
  await page.click('*[class^=left_rail_view--toggleButton]');

  // Go to print mode to hide the UI
  await page.emulateMedia('print');

  // Wait for arbitrary 2 seconds for chart animations to finish
  await page.waitFor(2000);

  // Generate the PDF
  await page.pdf({
    path: output,
    width: width
  });

  await browser.close();
})();
  1. Set up cron, API endpoint or whatever to run this script, then send the newly saved PDF to an email.

Important: needs the doc to be viewable by anonymous users, since there’s no easy way to sign in a specific user to Coda using browser automation (not worth the effort for this hackathon anyway — think handling Google sign in, 2FA etc)

Result: looking as close to Coda as possible, including nice vector charts. Not in email body though.

7 Likes