## Why Automate Report Generation? Every business generates reports: sales summaries, analytics dashboards, client deliverables, compliance documents. Doing this manually means hours of copy-pasting data into templates, adjusting formatting, and exporting PDFs. With the ToolCenter HTML-to-PDF API, you can turn any HTML template into a polished PDF in a single API call. Feed it data, get a report. ## The Architecture The workflow is straightforward: 1. **Fetch your data** — Database query, API call, or spreadsheet 2. **Render HTML template** — Inject data into an HTML template 3. **Convert to PDF** — Send the HTML to ToolCenter 4. **Deliver** — Email, upload to S3, or serve to users ## Step 1: Design Your Report Template Create a professional HTML report template: ```html
Monthly Sales Report
{{reportDate}}

{{reportTitle}}

{{totalRevenue}}
Total Revenue
{{totalOrders}}
Orders
{{avgOrderValue}}
Avg Order Value

Top Products

{{#products}} {{/products}}
ProductUnits SoldRevenue
{{name}}{{units}}{{revenue}}
``` ## Step 2: Populate the Template with Data Use a template engine to inject your data: ```javascript const Handlebars = require('handlebars'); const fs = require('fs'); function renderReport(data) { const templateSrc = fs.readFileSync('./templates/report.html', 'utf-8'); const template = Handlebars.compile(templateSrc); return template({ reportTitle: `Sales Report — ${data.month} ${data.year}`, reportDate: new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }), totalRevenue: `$${data.totalRevenue.toLocaleString()}`, totalOrders: data.totalOrders.toLocaleString(), avgOrderValue: `$${(data.totalRevenue / data.totalOrders).toFixed(2)}`, products: data.topProducts, generatedAt: new Date().toISOString(), }); } ``` ## Step 3: Convert to PDF with ToolCenter ```javascript const axios = require('axios'); async function generatePDF(html) { const response = await axios.post( 'https://api.toolcenter.dev/v1/pdf', { html: html, format: 'A4', margin: { top: '20mm', bottom: '20mm', left: '15mm', right: '15mm' }, printBackground: true, preferCSSPageSize: true, }, { headers: { 'Authorization': 'Bearer YOUR_API_KEY' }, responseType: 'arraybuffer', } ); return Buffer.from(response.data); } ``` ## Step 4: Put It All Together ```javascript async function createMonthlyReport(month, year) { // 1. Fetch data from your database const data = await fetchSalesData(month, year); // 2. Render HTML const html = renderReport(data); // 3. Convert to PDF const pdf = await generatePDF(html); // 4. Save or send const filename = `sales-report-${year}-${month}.pdf`; fs.writeFileSync(`./reports/${filename}`, pdf); // Optional: send via email await sendEmail({ to: '[email protected]', subject: `Sales Report — ${month}/${year}`, attachments: [{ filename, content: pdf }], }); return filename; } ``` ## Python Implementation ```python import requests from jinja2 import Template from datetime import datetime def generate_report(data): # Load and render template with open('templates/report.html') as f: template = Template(f.read()) html = template.render( report_title=f"Sales Report — {data['month']}", report_date=datetime.now().strftime('%B %d, %Y'), total_revenue=f"${data['total_revenue']:,.2f}", total_orders=f"{data['total_orders']:,}", products=data['top_products'], ) # Convert to PDF response = requests.post( 'https://api.toolcenter.dev/v1/pdf', json={ 'html': html, 'format': 'A4', 'margin': {'top': '20mm', 'bottom': '20mm', 'left': '15mm', 'right': '15mm'}, 'printBackground': True, }, headers={'Authorization': 'Bearer YOUR_API_KEY'}, ) return response.content ``` ## Scheduling Reports ### With Cron (Node.js) ```javascript const cron = require('node-cron'); // Generate monthly report on the 1st at 9 AM cron.schedule('0 9 1 * *', async () => { const now = new Date(); const lastMonth = now.getMonth(); // 0-indexed, so this is last month const year = now.getFullYear(); await createMonthlyReport(lastMonth, year); console.log('Monthly report generated and sent!'); }); ``` ### With AWS Lambda ```javascript exports.handler = async (event) => { const { month, year } = event; const filename = await createMonthlyReport(month, year); return { statusCode: 200, body: `Report generated: ${filename}` }; }; ``` ## Advanced Features ### Adding Charts Include chart images in your report using a charting library: ```javascript const { ChartJSNodeCanvas } = require('chartjs-node-canvas'); async function renderChart(data) { const canvas = new ChartJSNodeCanvas({ width: 600, height: 300 }); const image = await canvas.renderToDataURL({ type: 'bar', data: { labels: data.map(d => d.month), datasets: [{ label: 'Revenue', data: data.map(d => d.revenue) }], }, }); return image; // data:image/png;base64,... } ``` Embed it in your HTML template: ```html ``` ### Multi-Page Reports For long reports, use CSS page breaks: ```css .page-break { page-break-after: always; } ``` ### Headers and Footers ```javascript const response = await axios.post('https://api.toolcenter.dev/v1/pdf', { html: html, format: 'A4', headerTemplate: '
Acme Corp — Confidential
', footerTemplate: '
Page of
', displayHeaderFooter: true, }); ``` ## Conclusion Automated report generation eliminates hours of manual work. Design your templates once in HTML/CSS, populate them with live data, and let the ToolCenter PDF API handle the conversion. Whether it's daily dashboards, weekly summaries, or monthly analytics, the pipeline stays the same: data in, PDF out.