Why Convert URLs to PDF Programmatically?

Automating URL-to-PDF conversion is essential for modern applications that need to archive web content, generate reports from dashboards, create invoices from hosted templates, or produce documentation from live pages.

📊

Report Generation

Automatically capture live dashboards, analytics pages, and data visualizations as shareable PDF reports. Schedule nightly exports of business intelligence dashboards without manual screenshots.

💾

Web Archival

Preserve web content in a stable, offline format. Legal compliance, regulatory archiving, and content backup all benefit from automated URL-to-PDF conversion that captures pages exactly as rendered.

🧾

Invoice & Receipt PDFs

Host your invoice template as a web page and convert it to PDF on demand. This approach lets you use HTML and CSS for layout while delivering professional PDF documents to your customers.

🔄

CI/CD Integration

Integrate PDF generation into your build pipeline. Generate documentation PDFs from your hosted docs site, create release notes, or produce compliance artifacts automatically on every deploy.

Step-by-Step Tutorial

Follow these five steps to convert any URL to PDF programmatically using PDFSpark's free API. Each step includes working code examples you can copy and run immediately.

Choose the Right Endpoint

PDFSpark provides two endpoints for PDF generation. For converting live web pages, you will use the URL-to-PDF endpoint. If you have raw HTML content instead, the HTML-to-PDF endpoint is a better fit.

Available Endpoints
# Convert a live webpage to PDF
POST https://pdfspark.dev/api/v1/pdf/from-url

# Convert raw HTML string to PDF
POST https://pdfspark.dev/api/v1/pdf/from-html

The /api/v1/pdf/from-url endpoint accepts a JSON body with a url field pointing to any publicly accessible web page. PDFSpark navigates to that URL using a real Chromium browser, fully renders the page including JavaScript and CSS, and returns a PDF binary in the response body.

Basic URL to PDF with cURL

The fastest way to test URL-to-PDF conversion is with a single cURL command. This sends a POST request with the target URL and saves the resulting PDF to your local filesystem. No authentication headers, no API keys — just the URL you want to capture.

cURL
curl -X POST "https://pdfspark.dev/api/v1/pdf/from-url" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "options": {
      "format": "A4",
      "printBackground": true
    }
  }' \
  -o webpage.pdf

The response is a raw PDF binary with Content-Type: application/pdf. The -o flag tells cURL to save the output directly to a file. You can open webpage.pdf in any PDF viewer and see an exact replica of the web page as it would appear in Chrome's print dialog.

JavaScript / Node.js Integration

For server-side applications running Node.js, you can use the built-in fetch API (available in Node 18+) to call PDFSpark and save the resulting PDF. This approach works in Express route handlers, serverless functions, scheduled jobs, and any other Node.js environment.

JavaScript (Node.js)
import { writeFile } from 'node:fs/promises';

async function convertUrlToPdf(targetUrl, outputPath) {
  const response = await fetch(
    'https://pdfspark.dev/api/v1/pdf/from-url',
    {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        url: targetUrl,
        options: {
          format: 'A4',
          printBackground: true,
          margin: {
            top: '20mm',
            bottom: '20mm',
            left: '15mm',
            right: '15mm'
          }
        }
      })
    }
  );

  if (!response.ok) {
    throw new Error(`PDF generation failed: ${response.status}`);
  }

  const pdfBuffer = Buffer.from(
    await response.arrayBuffer()
  );
  await writeFile(outputPath, pdfBuffer);
  console.log(`PDF saved to ${outputPath}`);
}

// Usage
await convertUrlToPdf(
  'https://example.com',
  'output.pdf'
);

For browser-based JavaScript (client-side), you can use the same fetch call and trigger a download using a Blob URL. This lets users click a button to generate and download a PDF of any URL directly in the browser.

Python Integration

Python developers can use the requests library to convert URLs to PDF. This pattern works in Django views, Flask routes, FastAPI endpoints, Celery tasks, and standalone scripts. The key is to stream the response to avoid loading the entire PDF into memory at once.

Python
import requests

def convert_url_to_pdf(target_url: str, output_path: str) -> None:
    """Convert a URL to PDF using PDFSpark API."""
    response = requests.post(
        "https://pdfspark.dev/api/v1/pdf/from-url",
        json={
            "url": target_url,
            "options": {
                "format": "A4",
                "printBackground": True,
                "margin": {
                    "top": "20mm",
                    "bottom": "20mm",
                    "left": "15mm",
                    "right": "15mm"
                }
            }
        },
        timeout=30
    )
    response.raise_for_status()

    with open(output_path, "wb") as f:
        f.write(response.content)

    print(f"PDF saved to {output_path}")

# Usage
convert_url_to_pdf(
    "https://example.com",
    "output.pdf"
)

For production Python applications, consider adding retry logic with exponential backoff. The requests library raises an exception on non-2xx status codes when you call raise_for_status(), making error handling straightforward.

Advanced Options — Customize Your PDFs

PDFSpark supports a rich set of options to control the output PDF. You can set page format, margins, headers, footers, media type emulation, and more. All options are passed in the options object of your request body.

Option Type Description
format string Paper size: "A4", "Letter", "Legal", "A3", etc.
landscape boolean Set to true for landscape orientation. Default is portrait.
margin object Page margins with top, bottom, left, right values (e.g., "20mm").
printBackground boolean Include CSS background colors and images. Default is false.
emulateMediaType string Force "screen" or "print" CSS media type for rendering.
headerTemplate string HTML template for page header. Supports pageNumber, totalPages classes.
footerTemplate string HTML template for page footer. Same special classes as header.
delay number Wait time in milliseconds after page load before capturing (for JS-heavy pages).
Advanced cURL Example
curl -X POST "https://pdfspark.dev/api/v1/pdf/from-url" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/dashboard",
    "options": {
      "format": "A4",
      "landscape": true,
      "printBackground": true,
      "emulateMediaType": "screen",
      "margin": {
        "top": "25mm",
        "bottom": "25mm",
        "left": "10mm",
        "right": "10mm"
      },
      "headerTemplate": "<div style=\"font-size:8px;width:100%;text-align:center;\">Confidential Report</div>",
      "footerTemplate": "<div style=\"font-size:8px;width:100%;text-align:center;\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>",
      "delay": 2000
    }
  }' \
  -o report.pdf

The delay parameter is particularly useful for pages that load data asynchronously. If your target URL fetches content via JavaScript after the initial page load, set a delay of 1000–3000 milliseconds to ensure all content is rendered before the PDF is captured. For static pages, you can omit this option entirely.

URL to PDF FAQ

Answers to the most common questions about converting URLs to PDF programmatically with PDFSpark.

Is the PDFSpark API really free to use?

Yes. PDFSpark is completely free with no API keys, no signup, and no monthly quotas. The only limitation is rate limiting at 20 requests per minute per IP address to ensure fair usage for everyone. It is part of the SoftVoyagers open ecosystem and will remain free forever.

Can I convert pages that require JavaScript to render?

Absolutely. PDFSpark uses a full Chromium browser engine under the hood, which means it executes all JavaScript on the page just like a real browser would. Single-page applications (React, Vue, Angular), dynamically loaded charts, and interactive dashboards are all fully supported. Use the delay option to give asynchronous content extra time to load before the PDF snapshot is taken.

What happens if the target URL is behind authentication?

PDFSpark can only access publicly reachable URLs. If your target page requires login credentials, you have two alternatives: either use the /api/v1/pdf/from-html endpoint to send the pre-rendered HTML directly, or set up a publicly accessible (but obscured) version of the page. For security, PDFSpark includes SSRF protection that blocks requests to private IP ranges and internal networks.

What page sizes and formats are supported?

PDFSpark supports all standard paper sizes including A0 through A6, Letter, Legal, Tabloid, and Ledger. You can also specify custom dimensions using the width and height fields in your options object. Both portrait and landscape orientations are available for every page size.

How does URL-to-PDF differ from HTML-to-PDF?

The /api/v1/pdf/from-url endpoint navigates to a live URL with a real browser, loads all external resources (stylesheets, images, scripts), executes JavaScript, and then captures the fully rendered page. The /api/v1/pdf/from-html endpoint takes a raw HTML string that you provide directly in the request body. Use URL-to-PDF when you want to capture existing web pages; use HTML-to-PDF when you are generating content dynamically in your application.

Can I add custom headers and footers to the generated PDF?

Yes. Use the headerTemplate and footerTemplate options to inject HTML that appears on every page of the PDF. These templates support special CSS classes: pageNumber for the current page, totalPages for the total page count, date for the current date, title for the document title, and url for the page URL.

Are there alternatives to PDFSpark for this task?

Other tools for programmatic URL-to-PDF conversion include Puppeteer (self-hosted), Playwright (self-hosted), and various paid APIs. PDFSpark differentiates itself by being completely free, requiring zero infrastructure setup, and needing no API key. Unlike wkhtmltopdf, PDFSpark uses a modern Chromium engine that fully supports CSS3, Flexbox, Grid, and ES2024 JavaScript.

Start Converting URLs to PDF Today

One API call. No signup. No API key. Free forever.

Try PDFSpark Free →
Part of the SoftVoyagers Ecosystem