PDF
Generating PDF Reports with Screenshots
April 18, 2025
8 min read
PDF reports remain an essential business tool for sharing information in a professional, portable format. By combining our Screenshot API with PDF generation libraries, you can create dynamic reports that include website screenshots, analytics data, and custom branding.
Why Include Screenshots in Reports?
- Provide visual context for web analytics data
- Document website changes over time
- Capture competitor websites for market analysis
- Include visual evidence in compliance reports
- Create visual archives of web content
PDF Generation Options
Several libraries make it easy to generate PDFs programmatically:
Using PDFKit (Node.js)
javascript
01const PDFDocument = require('pdfkit');02const fs = require('fs');03const axios = require('axios');0405async function generateReport(url, apiKey) {06 // Get screenshot from API07 const response = await axios.post('https://api.screenshotapi.com/capture', {08 url,09 fullPage: true,10 format: 'png'11 }, {12 headers: {13 'Authorization': `Bearer ${apiKey}`14 }15 });1617 const screenshotUrl = response.data.screenshotUrl;18 const imageResponse = await axios.get(screenshotUrl, { responseType: 'arraybuffer' });19 const imageBuffer = Buffer.from(imageResponse.data);2021 // Create PDF22 const doc = new PDFDocument({ margin: 50 });23 doc.pipe(fs.createWriteStream('website-report.pdf'));2425 // Add title26 doc.fontSize(25).text('Website Screenshot Report', { align: 'center' });27 doc.moveDown();2829 // Add URL and date30 doc.fontSize(14).text(`URL: ${url}`);31 doc.fontSize(12).text(`Generated: ${new Date().toLocaleString()}`);32 doc.moveDown();3334 // Add screenshot35 const imgWidth = doc.page.width - 100;36 doc.image(imageBuffer, 50, doc.y, { width: imgWidth });3738 doc.end();39 return 'website-report.pdf';40}
Using jsPDF (Browser-based)
javascript
01import { jsPDF } from 'jspdf';0203async function generateReport(url, apiKey) {04 // Get screenshot from API05 const response = await fetch('https://api.screenshotapi.com/capture', {06 method: 'POST',07 headers: {08 'Content-Type': 'application/json',09 'Authorization': `Bearer ${apiKey}`10 },11 body: JSON.stringify({12 url,13 fullPage: false,14 width: 1280,15 height: 80016 })17 });1819 const data = await response.json();2021 // Load the image22 return new Promise((resolve) => {23 const img = new Image();24 img.crossOrigin = 'Anonymous';25 img.onload = () => {26 // Create PDF27 const doc = new jsPDF({28 orientation: 'landscape',29 unit: 'px',30 format: [1000, 700]31 });3233 // Add title34 doc.setFontSize(24);35 doc.text('Website Screenshot Report', 500, 30, { align: 'center' });3637 // Add URL and date38 doc.setFontSize(12);39 doc.text(`URL: ${url}`, 50, 60);40 doc.text(`Generated: ${new Date().toLocaleString()}`, 50, 80);4142 // Add screenshot (resized to fit)43 const imgWidth = 900;44 const imgHeight = (img.height * imgWidth) / img.width;45 doc.addImage(img, 'PNG', 50, 100, imgWidth, imgHeight);4647 // Save PDF48 doc.save('website-report.pdf');49 resolve('website-report.pdf');50 };51 img.src = data.screenshotUrl;52 });53}
Creating Multi-page Reports
For more comprehensive reports, you might want to include multiple screenshots:
javascript
01async function generateComparisonReport(urls, apiKey) {02 const doc = new PDFDocument({ margin: 50 });03 doc.pipe(fs.createWriteStream('comparison-report.pdf'));0405 // Add title06 doc.fontSize(25).text('Website Comparison Report', { align: 'center' });07 doc.moveDown();0809 // Process each URL10 for (const url of urls) {11 // Get screenshot12 const response = await axios.post('https://api.screenshotapi.com/capture', {13 url,14 fullPage: false,15 width: 1280,16 height: 80017 }, {18 headers: {19 'Authorization': `Bearer ${apiKey}`20 }21 });2223 const screenshotUrl = response.data.screenshotUrl;24 const imageResponse = await axios.get(screenshotUrl, { responseType: 'arraybuffer' });25 const imageBuffer = Buffer.from(imageResponse.data);2627 // Add a new page for each screenshot (except the first one)28 if (url !== urls[0]) {29 doc.addPage();30 }3132 // Add URL33 doc.fontSize(14).text(`URL: ${url}`);34 doc.moveDown();3536 // Add screenshot37 const imgWidth = doc.page.width - 100;38 doc.image(imageBuffer, 50, doc.y, { width: imgWidth });39 }4041 doc.end();42 return 'comparison-report.pdf';43}
Adding Analytics Data
Enhance your reports by combining screenshots with analytics data:
javascript
01async function generateAnalyticsReport(url, analyticsData, apiKey) {02 // Similar setup to previous examples...0304 // Add analytics charts and data05 doc.fontSize(18).text('Performance Metrics', 50, 500);06 doc.fontSize(12).text(`Page Load Time: ${analyticsData.loadTime}ms`, 50, 530);07 doc.fontSize(12).text(`First Contentful Paint: ${analyticsData.fcp}ms`, 50, 550);08 doc.fontSize(12).text(`Largest Contentful Paint: ${analyticsData.lcp}ms`, 50, 570);0910 // Add more data and visualizations as needed...1112 doc.end();13}
By combining our Screenshot API with these PDF generation techniques, you can create professional, data-rich reports that provide both visual and analytical insights into web content.
ReportsIntegration
Ready to Get Started?
Get your API key now and start capturing screenshots in minutes.