HTML to PDF / HTTP API Guide

Overview

This page serves as a guide for using the PDFCrowd API to convert web pages and HTML content to PDF. You can call the API by sending an HTTP request to the API server address with options passed as POST data.

Quick Start

Below are examples to help you quickly get started with the API. Explore our additional examples for more insights.

Authentication

To access the API, you will need to use your PDFCrowd username and API key. For initial testing, you may use the following demo credentials without registering:

  • Username: demo
  • API key: ce544b6ea52a5621fb9d55f8b542d14d

To obtain your personal API credentials, start a free trial or purchase the API license.

The authentication method is HTTP Basic authentication.

Server Address

The server address: https://api.pdfcrowd.com/convert/24.04/

The content type for POST requests must be `multipart/form-data` if the request uploads any local files. Otherwise, `application/x-www-form-urlencoded` can also be used.

Show older API versions.

Customization

The table below highlights the most common customizations you might find useful. Refer to the Option Reference for a detailed description of all available options. For an interactive experience, explore these options in the API Playground.

For additional customization options and Troubleshooting, please visit the FAQ section of our website where you can find answers and help related to frequent queries and common issues.

Page Size Adjust the page size using page_size. To create a single-page PDF with page height automatically adjusted to the HTML content, set -1 to page_height.
Page Orientation Change the page orientation to landscape using orientation.
Page Margins Adjust the page margins with margin_top, margin_bottom, margin_left, and margin_right. To eliminate all margins use no_margins.
Headers and Footers Add custom headers and footers using header_html and footer_html. To adjust the height of headers or footers, use footer_height and header_height respectively.
For detailed guidance on implementing these features, refer to this tutorial.
HTML Content Fitting Set the viewport width for formatting the HTML content using content_viewport_width. Use content_fit_mode to control how the content is fitted into the PDF page.
You can also adjust the zoom level of HTML content by using scale_factor, which allows you to scale the content up or down.
Per-page Settings Use conversion_config to specify the PDF page size, orientation, margins, and the presence and appearance of headers and footers on a per-page basis.
Hide or Remove Contents Use the following classes in your HTML code to hide or remove elements from the output:
  • `pdfcrowd-remove`: This class applies `display:none!important` to the element, effectively removing it from the layout.
  • `pdfcrowd-hide`: This class applies `visibility:hidden!important` to the element, making it invisible but still occupying space in the layout.
For additional methods and detailed explanation, refer to this FAQ article.
Custom CSS Styling To customize CSS styling specifically for the conversion, use custom_css to inject additional styles.

Alternatively, you can directly incorporate conversion-specific styling into your main stylesheet. Just prefix your CSS selectors with `.pdfcrowd-body` to ensure the styles apply only during the conversion process. For example:
.pdfcrowd-body h1 { font-size: 48px; }
.pdfcrowd-body footer { display: none; }
Avoid Page Break To prevent page breaks within specific elements, use the `page-break-inside:avoid;` CSS property. Apply this property to elements where you do not want a page break, such as tables, table cells, and images. Here is an example:
th, td, img { page-break-inside:avoid }
Force Page Break You can force a page break in your document by incorporating a specific style in an HTML div tag. Insert the following code where you want the page break to occur:
<div style="page-break-before:always"></div>
Use `@media print` Activate the print version of a webpage (if available) using use_print_media. This function instructs the API to apply the CSS rules defined within the `@media print` stylesheet, ensuring the output mirrors the print-optimized version of the webpage.
Inject Custom JavaScript Use custom_javascript or on_load_javascript to modify HTML content using custom JavaScript scripts. These scripts run when the page loads, allowing you to dynamically alter elements, styles, or behavior. In addition to the standard browser JavaScript APIs, your scripts can leverage helper functions provided by our JavaScript library.
Add PDF Signature Enable the creation of PDFs with a digital signature field. This feature allows the PDF to be digitally signed using applications such as Adobe Acrobat or Preview. For more detailed instructions refer to our Create Digital Signature in PDF guide.
Fillable PDF Form Create fillable PDFs equipped with interactive fields and buttons. This functionality allows users to enter data directly into the PDF, making it ideal for applications such as forms and surveys. For detailed instructions on how to implement this feature, please refer to our Create Fillable PDF Form guide.
HTML Templates Add data to your HTML templates for dynamic generation tailored to specific content needs, such as reports, invoices, and personalized documents. For details, refer to HTML Template to PDF.

Response Headers

The HTTP response contains the following headers.

Name Description
x-pdfcrowd-debug-log URL of the debug log
x-pdfcrowd-remaining-credits The number of conversion credits remaining in your account
x-pdfcrowd-consumed-credits The number of credits used for the conversion
x-pdfcrowd-job-id The unique identifier for the conversion
x-pdfcrowd-pages Total number of pages in the output document
x-pdfcrowd-total-pages Total number of pages in the original document, including pages excluded by the print_page_range option
x-pdfcrowd-output-size Size of the output data in bytes

Troubleshooting

  • If you are receiving an error, refer to the API Status Codes for more information.
  • Use debug_log to obtain detailed information about the conversion process, including load errors, load times, browser console output, etc.
  • Consult the FAQ for answers to common questions.
  • Contact us if you need assistance or if there is a feature you are missing.

POST Parameter Reference

Refer to the HTML to PDF POST Reference for a description of all options.