Convert HTML to PDF Instantly with the PDFshift API
PDFshift API

PDFshift API is a RESTful service that converts HTML documents into PDF files. It processes requests via a simple HTTP endpoint, accepting raw HTML, URLs, or Markdown as input. The primary value is its ability to generate high-fidelity, customizable PDFs without needing a browser or complex dependencies, streamlining document generation workflows through a single call. Its straightforward integration into any programming language or platform makes it an efficient tool for automating PDF creation.

What Exactly Does PDFshift API Do for Developers

PDFshift API eliminates the need to build complex HTML-to-PDF rendering infrastructure from scratch. Developers send a simple HTTP request containing raw HTML, URL, or Markdown content, and the API returns a perfectly formatted PDF document. It handles the heavy lifting of rendering with a headless Chrome engine, ensuring pixel-perfect results that match modern web standards. This allows developers to integrate dynamic document generation—like invoices, reports, or certificates—into their applications with just a few lines of code. The API supports custom headers, footers, margins, and page sizes, giving developers full control without managing browser instances or server-side rendering queues. It operates as a straightforward, low-latency service that turns any web content into a downloadable PDF file. Generate PDFs programmatically without library dependencies or server overhead, making the entire workflow seamless document conversion for any development stack.

Converting HTML to PDF with a Single HTTP Request

Converting HTML to PDF with a single HTTP request eliminates multi-step rendering pipelines. You submit raw HTML or a URL as POST body data, and PDFshift returns the finished document synchronously. This approach avoids client-side browser dependencies or headless Chrome orchestration. A typical sequence:

  1. You construct a JSON payload containing the HTML source and optional parameters like page size or margins.
  2. PDFshift processes the markup, applying CSS and JavaScript rendering in a server-side engine.
  3. The API streams the PDF binary directly in the HTTP response.

This one-call pattern guarantees a stateless document conversion without intermediate file storage or callbacks.

Handling Complex Layouts, CSS, and JavaScript Rendering

PDFshift API excels at handling complex layouts by processing your HTML through a full Chromium engine, ensuring pixel-perfect preservation of intricate CSS grids, flexbox structures, and media queries. JavaScript rendering is fully supported, meaning dynamic charts, interactive elements, and modern frameworks like React or Vue generate complete, accurate PDFs without manual workarounds. The API automatically resolves font loading, custom properties, and layered positioning, so even multi-column page designs or heavy SVG use outputs flawlessly. Tables with nested styles are faithfully reproduced, eliminating layout shifts and converting your web pages into reliable documents every time.

PDFshift API

Supported Output Formats and Customization Options

PDFshift API provides extensive output format flexibility, allowing developers to convert HTML, CSS, and JavaScript directly into high-fidelity PDFs without external dependencies. Beyond standard PDF generation, you can customize page size, orientation, margins, and header/footer injection. The API also supports watermarking, password protection, and resolution control for precise document rendering.

  • Convert HTML documents, URLs, or raw strings to PDF
  • Adjust page layout via A4, Letter, or custom dimensions
  • Embed custom headers, footers, and watermarks
  • Set PDF/A compliance and compression levels

How to Integrate the Conversion Tool into Your Workflow

To integrate PDFshift API into your workflow, start by grabbing your API key from the dashboard. For a quick test, send a curl request pointing to `https://api.pdfshift.io/v2/convert/` with your URL or HTML payload. In production, wrap this call in a function within your app—like a Python script using `requests.post`—and handle the binary response by writing it directly to a file. Queue conversion jobs asynchronously to avoid blocking your main process, especially for bulk document handling. Store the returned PDF in a temporary bucket or attached to a user’s request. Finally, log any HTTP status codes (like `422` for bad payloads) to catch errors early without breaking your flow.

Authentication Methods: API Keys vs. Token-Based Access

When integrating the PDFshift API into your workflow, you’ll authenticate using either an API key or token-based access. API keys are straightforward: you pass your unique key as a query parameter or header, and they never expire unless you manually revoke them. Token-based access, on the other hand, involves generating a short-lived token via an initial exchange, offering a more secure authentication method for sensitive deployments. For everyday batch processing, API keys are simpler to implement—just copy-paste and go. If your workflow involves multiple services or requires rotating credentials automatically, tokens provide extra control without hardcoding long-lived secrets.

Making Your First API Call with cURL or Python

Begin by constructing a cURL command with your PDFshift API key in the -u flag, targeting the conversion endpoint with a JSON payload specifying the source URL or file path. For Python, use the requests library to POST a similar payload to https://api.pdfshift.io/v2/convert/, passing credentials via HTTPBasicAuth. The response returns the converted document binary. Validate success by checking the HTTP 200 status code. This two-method approach ensures rapid API integration for any environment.

For your first API call, use cURL with basic auth or Python’s requests POST—both directly return a PDF binary upon a 200 response.

PDFshift API

Error Handling and Response Codes You Should Know

When integrating PDFshift, monitor the HTTP response codes to diagnose failures efficiently. A `200` indicates success; `400` signals a malformed request (e.g., invalid URL). `401` means authentication is missing or incorrect, while `429` is a rate-limit hit. `500` points to a server-side issue—retry with exponential backoff. Idempotency keys help avoid duplicate charges on timeouts. For automated workflows, always log the response body’s `error` field. Q: Should I retry all 4xx errors? No—only retry `429` and `500`; other 4xx errors require fixing the request payload.

Key Features That Set This Service Apart from Alternatives

PDFshift stands out because it converts documents directly from a URL or raw HTML, eliminating the need for file uploads or local storage. This saves time and reduces infrastructure complexity. Unlike many alternatives, it returns the PDF as raw bytes in the API response, not a temporary download link, which simplifies integration and guarantees security. **The single most practical differentiator is its predictable pricing model:** you pay per page, not per document or API call. Q: What happens if my HTML has external images? A: PDFshift automatically fetches and embeds them, so your PDF looks exactly like the webpage without manual asset handling.

Header, Footer, and Page Margin Control for Branded Documents

PDFshift delivers precise header, footer, and margin control for branded documents by accepting explicit dimension values in millimeters or inches, eliminating guesswork. You define top, bottom, left, and right margins independently to ensure logo placement or legal disclaimers never clip. Headers and footers support custom HTML content, so a branded letterhead can include dynamic page numbers alongside a company logo. Q: Can I set different margins for the first page versus subsequent pages? A: Yes, through separate CSS rules applied via the margin_top parameter, allowing a title page with no header and wider margins while maintaining strict branding on following pages.

Setting Custom Paper Sizes, Orientation, and Margins

PDFshift allows precise control over page geometry by accepting explicit parameters for custom paper sizes, orientation, and margins. Users define dimensions in millimeters or inches, override default letter size, and set portrait or landscape mode via API fields like paper_size and orientation. Margins are adjustable independently for each side, enabling fine-tuned layouts for invoices, reports, or labels. This granularity eliminates post-processing, as the output matches exact print or display requirements without manual adjustment. The service validates all values before rendering, preventing errors from unsupported dimensions.

PDFshift enables exact page layout control through configurable paper dimensions, orientation, and margin parameters delivered directly via API.

Password Protection, Watermarking, and File Compression

PDFshift API provides granular control over document security and output management through three distinct features. Robust access control and content integrity are achieved via password protection, which allows setting separate user and owner passwords for viewing and editing restrictions. Watermarking enables the dynamic overlay of text or images across every page, crucial for branding or confidentiality marking. File compression reduces payload size through configurable image quality and resource optimization, balancing fidelity against bandwidth. These mechanisms operate independently, allowing a single API call to combine a password, a diagonal “Confidential” watermark, and medium-level compression for efficient, secure distribution.

Getting the Best Performance and Reliability from the API

To achieve optimal performance and reliability with the PDFshift API, always implement exponential backoff in your retry logic to handle transient network issues gracefully. Send requests with unique filenames in the payload to avoid processing conflicts, and use synchronous calls for critical documents to confirm immediate status. Leverage the API’s caching feature by setting a high `ttl` value for frequently converted templates, drastically reducing redundant processing. For high-volume workflows, enable connection keep-alive to minimize handshake overhead. Monitor response headers for specific error codes (like 429 for rate limits) and adjust your request rate dynamically. Finally, validate your HTML input before submission to prevent silent failures, ensuring your conversions remain fast and dependable under load.

Rate Limits, Retry Strategies, and Batch Processing Tips

PDFshift enforces rate limits, typically measured in requests per minute, to ensure API stability. Implement exponential backoff in your retry strategy, pausing progressively longer (e.g., 1, 2, 4 seconds) between attempts on HTTP 429 or 5xx errors, with a maximum retry count of three. For batch processing, submit multiple documents sequentially but not concurrently to avoid triggering limits; use a queue to manage throughput. Batching large files requires adjusted timeouts as processing scales linearly with page count. A strategic retry and batching approach minimizes latency and wasted requests.

PDFshift API

Rate Limits: respect per-minute caps. Retry Strategy: use exponential backoff with a cap. Batch Tips: process sequentially with queue management.

Optimizing HTML Payloads to Avoid Timeouts and Errors

To prevent timeouts and errors with the PDFshift API, focus on optimizing HTML html to pdf payloads. Minimize payload size by removing unused CSS, whitespace, and external resource calls. Use inline CSS and base64-encode only essential images to reduce server load. Set a low complexity limit on scripts and avoid heavy JavaScript execution, as the API processes HTML synchronously.

  • Compress HTML by stripping comments and redundant tags.
  • Replace external fonts with system fonts to prevent fetch delays.
  • Use lazy-loading placeholders for non-critical data.

Using Asynchronous Endpoints for Large or Long-Running Jobs

For large or long-running jobs, PDFshift provides asynchronous endpoints that decouple the request from the response, preventing client-side timeouts. When you submit a conversion via these endpoints, the API immediately returns a job ID instead of the final PDF. Your application must then poll a separate status endpoint using that ID to check for completion. Once the job status indicates asynchronous job completion, you can retrieve the final output. This approach is essential for handling complex documents exceeding standard size limits or requiring extended processing time, as it ensures reliable delivery without blocking your application’s execution flow.

Using asynchronous endpoints lets you submit large or long-running PDF conversions, receive a job ID immediately, and retrieve the result only after polling confirms the job is complete, ensuring reliability without timeouts.

Common Use Cases and Practical Tips for New Users

Common use cases for new users include instantly converting HTML invoices or landing pages to PDF, merging multiple documents into a single file, and extracting text for archival purposes. For a smooth start, always test your API key with a simple HTML string before scaling to production loads. Use the `async` parameter for larger files to avoid timeouts, and stick to base64-encoded payloads for reliability. Remember to set a descriptive `file_name` to keep your outputs organized. If you encounter errors, double-check your URL encoding and ensure your payload is valid JSON. Start with the free trial quota to validate workflows before committing.

Generating Invoices, Reports, or Receipts on the Fly

PDFshift API

For generating invoices, reports, or receipts on the fly, PDFshift API lets you process raw HTML or Markdown into polished PDFs in seconds. Just design your template with dynamic placeholders for totals or dates, then send a POST request with that HTML—no file storage needed. You can automate receipt generation by hooking it into your checkout flow, instantly creating a clean download link for customers. For reports, pass JSON data to a pre-styled template and PDFshift handles the rendering. The table below shows typical response times:

Invoice (2 pages) ~1.2 seconds
Receipt (1 page) ~0.8 seconds
Report (10 pages) ~2.5 seconds

Handling Dynamic Content from Webhooks or User Input

When consuming webhook payloads or user input, construct your HTML template by sanitizing and interpolating dynamic values directly into the source string passed to the PDFshift API. Ensure you encode special characters to prevent rendering failures, and use server-side string concatenation to build the full HTML before submission. Because the API returns a PDF binary, you can dynamically define filenames from the same input for immediate download. This approach allows real-time PDF generation from variable data without pre-stored templates, handling any JSON or form fields your endpoint receives.

Debugging Failed Conversions Without Wasting API Calls

When a PDFshift conversion fails, avoid blindly retrying the request and burning API calls. Instead, first inspect the error response from the API, which includes a specific error code and message. Use this to diagnose the root cause—such as an invalid source URL, unsupported file format, or authentication issue. Only resubmit the request after correcting the identified problem. For a structured approach to debugging failed conversions without wasting API calls, follow this sequence:

  1. Check the HTTP status code (e.g., 400 for bad request, 401 for unauthorized).
  2. Parse the JSON response for the error_message field to pinpoint the issue.
  3. Validate your source document or configuration based on that error, then retry only once.