HTML to PDF HTTP API Code Examples

Complete HTTP requests for common conversion tasks. See the HTTP API guide for authentication, request formats, and response handling.

Replace the example URLs and file paths with your own.

cURL example options

Use a Bash-compatible shell.

Basic options

  • -u — Supply your username and API key.
  • -F — Send a form field.
  • --form-string — Send a literal form value.
  • -o — Save the response to a file.
  • -f — Report HTTP errors as command failures.
  • -s -S — Hide the progress meter while keeping error messages.

File input

  • file=@document.html — Upload a local file.
  • text=<- — Read HTML from standard input. The examples use < document.html to supply the file's contents.

Diagnostics

  • --fail-with-body — Use instead of -f to retain the error response body.
  • -D — Save response headers to a file.
  • -w — Print response information; the diagnostic example prints the HTTP status.

Basic examples

Convert a URL

curl -f -s -S \
  -u 'demo:demo' \
  -o example.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=balanced \
  https://api.pdfcrowd.com/convert/24.04/

Convert an HTML file

Upload an existing document.html file.

curl -f -s -S \
  -u 'demo:demo' \
  -o document.pdf \
  -F file=@document.html \
  -F content_viewport_width=balanced \
  https://api.pdfcrowd.com/convert/24.04/

For local images and stylesheets, include the HTML and its assets in an archive.

Send HTML content

Send HTML in the text field. This command reads it from an existing document.html file.

curl -f -s -S \
  -u 'demo:demo' \
  -o html.pdf \
  -F 'text=<-' \
  -F content_viewport_width=balanced \
  https://api.pdfcrowd.com/convert/24.04/ < document.html

In your application, pass the HTML produced by your template renderer as the value of text.

Page rendering

Set page size and margins

curl -f -s -S \
  -u 'demo:demo' \
  -o page-layout.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=balanced \
  -F margin_bottom=0.5in \
  -F margin_left=0.5in \
  -F margin_right=0.5in \
  -F margin_top=0.5in \
  -F orientation=landscape \
  -F page_size=Letter \
  https://api.pdfcrowd.com/convert/24.04/

For edge-to-edge output, use no_margins instead of the margin settings.

Add a header and page numbers

Show the source URL in the header and “Page N of M” in the footer.

curl -f -s -S \
  -u 'demo:demo' \
  -o header_footer.pdf \
  -F url=https://example.com/ \
  -F footer_height=10mm \
  --form-string 'footer_html=<center>Page <span class='"'"'pdfcrowd-page-number'"'"'></span> of <span class='"'"'pdfcrowd-page-count'"'"'></span></center>' \
  -F header_height=15mm \
  --form-string 'header_html=<a class='"'"'pdfcrowd-source-url'"'"' data-pdfcrowd-placement='"'"'href-and-content'"'"'></a>' \
  -F margin_bottom=0 \
  -F margin_top=0 \
  https://api.pdfcrowd.com/convert/24.04/

See the headers and footers guide for more layouts and placeholders.

Scale page content

Scale the HTML content to 300%.

curl -f -s -S \
  -u 'demo:demo' \
  -o zoom_300.pdf \
  -F url=https://example.com/ \
  -F scale_factor=300 \
  https://api.pdfcrowd.com/convert/24.04/

Choose a responsive layout

The browser viewport controls responsive breakpoints independently of the PDF paper size.

Large viewport:

curl -f -s -S \
  -u 'demo:demo' \
  -o bootstrap.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=large \
  -F no_margins=true \
  https://api.pdfcrowd.com/convert/24.04/

Small viewport:

curl -f -s -S \
  -u 'demo:demo' \
  -o viewport_small.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=small \
  https://api.pdfcrowd.com/convert/24.04/

Hide or restyle page elements

Replace the selectors with elements from your page.

curl -f -s -S \
  -u 'demo:demo' \
  -o custom_css.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=balanced \
  --form-string 'custom_css=
        /* Hide navigation, ads, and sidebar */
        nav, .advertisement, .sidebar, footer {
            display: none !important;
        }
        /* Adjust layout for print */
        .content {
            width: 100% !important;
            max-width: none !important;
        }
    ' \
  https://api.pdfcrowd.com/convert/24.04/

Modify the page with JavaScript

This example removes widgets and expands sections by changing their CSS classes. Adjust the selectors and class names for your page.

Remove widgets and expand sections:

curl -f -s -S \
  -u 'demo:demo' \
  -o custom_js.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=balanced \
  --form-string 'custom_javascript=
        // Remove dynamic content before conversion
        document.querySelectorAll('"'"'.video-player, .chat-widget'"'"').forEach(el => {
            el.remove();
        });
        // Expand collapsed sections
        document.querySelectorAll('"'"'.collapsed'"'"').forEach(el => {
            el.classList.remove('"'"'collapsed'"'"');
        });
    ' \
  https://api.pdfcrowd.com/convert/24.04/

Add content to the page:

curl -f -s -S \
  -u 'demo:demo' \
  -o html_inject.pdf \
  -F url=https://example.com/ \
  --form-string 'custom_javascript=el=document.createElement('"'"'h2'"'"'); el.textContent='"'"'Hello from PDFCrowd API'"'"'; el.style.color='"'"'red'"'"'; el_before=document.getElementsByTagName('"'"'h1'"'"')[0]; el_before.parentNode.insertBefore(el, el_before.nextSibling)' \
  https://api.pdfcrowd.com/convert/24.04/

Convert a specific element

Convert only the element matching #main in an existing document.html file. Adjust the filename and selector for your document.

curl -f -s -S \
  -u 'demo:demo' \
  -o html_part.pdf \
  -F file=@document.html \
  -F 'element_to_convert=#main' \
  https://api.pdfcrowd.com/convert/24.04/

Wait for a page element

Wait for #content-loaded to appear in an existing document.html file before converting it.

# Wait for a specific element to appear
curl -f -s -S \
  -u 'demo:demo' \
  -o wait_element.pdf \
  -F file=@document.html \
  -F content_viewport_width=balanced \
  -F 'wait_for_element=#content-loaded' \
  https://api.pdfcrowd.com/convert/24.04/

Choose an element that appears when your content is ready.

Allow extra time for JavaScript

# Wait for a specified time before conversion
curl -f -s -S \
  -u 'demo:demo' \
  -o with_delay.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=balanced \
  -F javascript_delay=2000 \
  https://api.pdfcrowd.com/convert/24.04/

This adds a delay of 2000 milliseconds. When the page has a reliable readiness marker, use Wait for a page element.

Apply the page’s @media print rules. The page must provide print styles for this to change its appearance.

curl -f -s -S \
  -u 'demo:demo' \
  -o print_media.pdf \
  -F url=https://example.com/ \
  -F content_viewport_width=balanced \
  -F use_print_media=true \
  https://api.pdfcrowd.com/convert/24.04/

Inspect element boundaries

Highlight element boxes to investigate spacing and layout.

Highlight backgrounds:

curl -f -s -S \
  -u 'demo:demo' \
  -o highlight_background.pdf \
  -F url=https://example.com/ \
  -F 'custom_javascript=libPdfcrowd.highlightHtmlElements({backgroundColor: '"'"'rgba(255, 191, 0, 0.1)'"'"', borderColor:null})' \
  https://api.pdfcrowd.com/convert/24.04/

Add borders and spacing:

curl -f -s -S \
  -u 'demo:demo' \
  -o highlight_borders.pdf \
  -F url=https://example.com/ \
  -F 'custom_javascript=libPdfcrowd.highlightHtmlElements({borderColor: '"'"'orange'"'"', backgroundColor: null, padding: '"'"'4px'"'"', margin: '"'"'4px'"'"'})' \
  https://api.pdfcrowd.com/convert/24.04/

HTML and CSS

Add these fragments to HTML you control.

Remove or hide elements

<nav class="pdfcrowd-remove">This won't appear in the PDF</nav>
<div class="pdfcrowd-hide">This is invisible but takes up space</div>

pdfcrowd-remove removes the element from the layout. pdfcrowd-hide preserves its space.

Control page breaks

<!-- Force a page break before the following section -->
<div style="page-break-before: always"></div>

<!-- Avoid breaking tables across pages -->
<style>
  table, tr, td { page-break-inside: avoid; }
</style>

Content taller than a page still needs to break.

Apply styles only during conversion

<style>
  /* These styles only apply during PDF conversion */
  .pdfcrowd-body h1 { font-size: 48px; }
  .pdfcrowd-body footer { display: none; }
  .pdfcrowd-body .main-content { width: 100%; }
</style>

The .pdfcrowd-body prefix limits these rules to conversion; it does not change the page in a visitor’s browser.

Templates

Upload an HTML template whose variables match your data. See HTML Template to PDF for template syntax and data mapping.

Render a template with a data file

This request uploads existing template.html and data.json files.

curl -f -s -S \
  -u 'demo:demo' \
  -o output.pdf \
  -F file=@template.html \
  -F data_file=@data.json \
  -F content_viewport_width=balanced \
  -F data_format=json \
  https://api.pdfcrowd.com/convert/24.04/

Advanced examples

Create a fillable PDF

Use an existing form.html file containing HTML form controls.

curl -f -s -S \
  -u 'demo:demo' \
  -o form.pdf \
  -F file=@form.html \
  -F content_viewport_width=balanced \
  -F enable_pdf_forms=true \
  https://api.pdfcrowd.com/convert/24.04/

See fillable PDF forms for supported controls and attributes.

Set PDF metadata

curl -f -s -S \
  -u 'demo:demo' \
  -o with_metadata.pdf \
  -F url=https://example.com/ \
  -F author=PDFCrowd \
  -F keywords=PDFCrowd,demo \
  -F subject=Demo \
  -F 'title=Hello World' \
  https://api.pdfcrowd.com/convert/24.04/

Set a presentation view

curl -f -s -S \
  -u 'demo:demo' \
  -o slide_show.pdf \
  -F url=https://example.com/ \
  -F initial_zoom_type=fit-page \
  -F no_margins=true \
  -F orientation=landscape \
  -F page_layout=single-page \
  -F page_mode=full-screen \
  https://api.pdfcrowd.com/convert/24.04/

These settings request a full-screen initial view with landscape pages. The PDF viewer controls whether it honors those preferences.