How SnapAPI Converts Web Pages to PDF
SnapAPI loads the target URL in a full Chromium browser, waits for JavaScript execution and all resources to finish loading, then uses Chromium print-to-PDF to generate a PDF that matches the page print stylesheet. The Chromium engine supports the complete CSS print media specification, producing PDFs with accurate typography, color, image rendering, and layout at the configured paper dimensions. Unlike server-side HTML-to-PDF libraries using outdated browser engines, SnapAPI handles modern CSS including Grid, Flexbox, CSS Custom Properties, and web fonts from services like Google Fonts. The resulting PDF faithfully represents the page as designed, without the layout breakage that affects older PDF tools when rendering modern CSS. Authentication-protected pages can be captured by passing the required cookies or headers in the API request, allowing internal dashboard and report pages to be converted to PDF with the same API call.
PDF Endpoint Parameters
The SnapAPI PDF endpoint accepts parameters controlling paper size, margins, orientation, and rendering options. The format parameter accepts A4, Letter, A5, Legal, and Tabloid. The margin_top, margin_bottom, margin_left, and margin_right parameters set page margins in millimeters. The landscape parameter set to true produces landscape PDFs. The print_background parameter set to true preserves background colors and images, essential for branded reports, certificates, and marketing collateral where the design depends on colored backgrounds. The delay parameter waits a specified number of milliseconds after page load before generating the PDF, ensuring JavaScript-rendered content and lazy-loaded images appear in the output. The delay value should be set to match the actual rendering time of your target page: most pages need zero to one thousand milliseconds, while complex dashboards may need two to three thousand.
import requests
API_KEY = "your_api_key"
def webpage_to_pdf(url, output_path, **options):
params = {
"url": url,
"format": options.get("format", "A4"),
"margin_top": options.get("margin_top", "10mm"),
"margin_bottom": options.get("margin_bottom", "10mm"),
"margin_left": options.get("margin_left", "15mm"),
"margin_right": options.get("margin_right", "15mm"),
"print_background": "true" if options.get("print_background") else "false",
"landscape": "true" if options.get("landscape") else "false",
}
if options.get("delay"):
params["delay"] = options["delay"]
resp = requests.get(
"https://api.snapapi.pics/pdf",
params=params,
headers={"Authorization": f"Bearer {API_KEY}"},
timeout=60,
)
resp.raise_for_status()
with open(output_path, "wb") as f:
f.write(resp.content)
return len(resp.content)
size = webpage_to_pdf(
"https://example.com/report",
"report.pdf",
format="A4",
print_background=True,
delay=2000,
)
print(f"PDF size: {size:,} bytes")
Optimizing Pages for PDF Output
Web pages designed for browser viewing often need CSS print media adjustments to produce clean PDF output. Add a @media print stylesheet that hides navigation bars, sidebars, cookie banners, social sharing buttons, and live chat widgets that should not appear in the PDF. Set page-break-inside: avoid on tables, figures, and code blocks to prevent awkward mid-element page breaks. Set page-break-before: always on section headings to start each major section on a new page in long documents. Use the @page CSS rule to define consistent page margins and footer content. Test your print stylesheet by using Chrome File > Print > Save as PDF before deploying, since Chrome print preview closely matches SnapAPI Chromium output and reveals layout issues at actual paper dimensions before you run API calls.
Web Page to PDF Use Cases
Report generation is the highest-volume use case: analytics dashboards, financial reports, and business intelligence summaries are rendered as web pages and converted to PDF for distribution to stakeholders. Invoice generation converts billing pages to PDF for email delivery to customers, providing a professional document format without requiring a separate PDF generation library in your stack. Legal document archiving converts court filings, regulatory pages, and compliance documents to PDF for offline storage and case documentation. Content export in note-taking and knowledge management SaaS products lets users export content to PDF by rendering the content page and converting it to a download. Marketing collateral generation converts HTML email templates and landing pages to PDF for print materials and media kits. Screenshot-plus-PDF workflows use a single SnapAPI key for both image and PDF output of the same URL, eliminating a second service dependency.
Get Started with the Web Page to PDF API
Register at snapapi.pics/register for a free account with two hundred PDF generation requests per month at no cost. No credit card is required to start. The PDF endpoint is available on all plan tiers including the free tier. The SnapAPI documentation at snapapi.pics/docs covers all PDF endpoint parameters with code examples in JavaScript, Python, Go, PHP, Swift, and Kotlin. For applications that need both screenshots and PDFs from the same URL, SnapAPI handles both with a single API key and a single line change in the endpoint path, reducing integration and billing complexity compared to using separate screenshot and PDF services.