Skip to main content
← Back to Recipes

Generate PDF report with chart

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

javascript
import PDFDocument from 'pdfkit'; import fs from 'fs'; const res = await fetch('https://www.chart-output.com/api/v1/render', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.CHART_OUTPUT_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'bar', data: { labels: ['Q1','Q2','Q3','Q4'], datasets: [{ data: [100,150,120,180] }] }, format: 'png', }), }); if (!res.ok) throw new Error(await res.text()); const chartPng = Buffer.from(await res.arrayBuffer()); 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();