← Back to Recipes

Generate PDF report with chart

Render a chart, save to temp file, then embed in a PDF with Puppeteer or pdfkit.

import { ChartOutput } from '@chart-output/node';
import PDFDocument from 'pdfkit';
import fs from 'fs';

const client = new ChartOutput({ apiKey: process.env.CHART_OUTPUT_API_KEY });
const chartPng = await client.render({
  type: 'bar',
  data: { labels: ['Q1','Q2','Q3','Q4'], datasets: [{ data: [100,150,120,180] }] },
  format: 'png',
});

const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('report.pdf'));
doc.fontSize(20).text('Monthly Report', 50, 50);
doc.image(chartPng, 50, 100, { width: 500 });
doc.end();