HTML to PDF Command Line Code Examples

Use these command-line examples for common HTML to PDF tasks. See the CLI guide for installation, input and output handling, and diagnostics.

You can run the examples below without account setup. The CLI uses demo credentials unless you have configured your own API credentials. Update the input URLs and filenames as needed.

Commands use POSIX-shell syntax, as in Bash or Zsh, and save results in the current directory.

Basic examples

These commands save the PDF with --output. See Handle the Result for output options and failure handling.

Convert a URL

html2pdf --content-viewport-width=balanced \
    --output example.pdf \
    https://example.com/

Convert an HTML file

Upload an existing document.html file. For local images and stylesheets, see Local assets.

html2pdf --content-viewport-width=balanced \
    --output document.pdf \
    document.html

Read HTML from stdin

Read HTML from an existing document.html file through stdin. Use the same - input when piping HTML from another program.

html2pdf --content-viewport-width=balanced \
    --output html.pdf \
    - < document.html

Convert inline HTML

Send HTML through stdin and save the PDF as report.pdf.

html2pdf \
    --content-viewport-width=balanced \
    --output report.pdf \
    - <<'HTML'
<!doctype html>
<html><body><h1>Monthly report</h1></body></html>
HTML

Local assets

Include HTML and its local images, stylesheets, and scripts in a ZIP archive, keeping their relative paths.

Convert a prepared archive

Upload an existing document.zip. Select the main HTML file explicitly when the archive contains multiple documents.

Detect the main HTML file

html2pdf --content-viewport-width=balanced \
    --output output.pdf \
    document.zip

Select index.html

html2pdf --content-viewport-width=balanced \
    --zip-main-filename=index.html \
    --output document.pdf \
    document.zip

Convert a local directory

Use the zip utility to create document.zip from the existing document directory, then convert it to PDF.

(cd document && zip -q -r - .) > document.zip &&
html2pdf \
    --content-viewport-width=balanced \
    --output document.pdf \
    document.zip

Page rendering

Set page size and margins

Use landscape Letter pages with half-inch margins.

html2pdf --content-viewport-width=balanced \
    --margin-bottom=0.5in \
    --margin-left=0.5in \
    --margin-right=0.5in \
    --margin-top=0.5in \
    --orientation=landscape \
    --page-size=Letter \
    --output page-layout.pdf \
    https://example.com/

Add a header and page numbers

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

html2pdf --footer-height=10mm \
    '--footer-html=
    <center>
        Page <span class="pdfcrowd-page-number"></span>
        of <span class="pdfcrowd-page-count"></span>
    </center>' \
    --header-height=15mm \
    '--header-html=
    <a class="pdfcrowd-source-url"
        data-pdfcrowd-placement="href-and-content"></a>' \
    --margin-bottom=0 \
    --margin-top=0 \
    --output header_footer.pdf \
    https://example.com/

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

Scale page content

Scale the HTML content to 300%.

html2pdf --scale-factor=300 \
    --output zoom_300.pdf \
    https://example.com/

Choose a responsive layout

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

Large viewport

html2pdf --content-viewport-width=large \
    --no-margins \
    --output bootstrap.pdf \
    https://example.com/

Small viewport

html2pdf --content-viewport-width=small \
    --output viewport_small.pdf \
    https://example.com/

Hide or restyle page elements

Replace the selectors with elements from your page.

html2pdf --content-viewport-width=balanced \
    '--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;
    }' \
    --output custom_css.pdf \
    https://example.com/

Modify the page with JavaScript

Adjust the selectors and class names for your page.

Remove widgets and expand sections

html2pdf --content-viewport-width=balanced \
    '--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'"'"');
    });' \
    --output custom_js.pdf \
    https://example.com/

Add content to the page

html2pdf '--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);' \
    --output html_inject.pdf \
    https://example.com/

Convert a specific element

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

html2pdf '--element-to-convert=#main' \
    --output html_part.pdf \
    document.html

Wait for a page element

Wait for #content-loaded to appear in an existing document.html file. Choose an element that appears when your content is ready.

html2pdf --content-viewport-width=balanced \
    '--wait-for-element=#content-loaded' \
    --output wait_element.pdf \
    document.html

Allow extra time for JavaScript

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

html2pdf --content-viewport-width=balanced \
    --javascript-delay=2000 \
    --output with_delay.pdf \
    https://example.com/

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

html2pdf --content-viewport-width=balanced \
    --use-print-media \
    --output print_media.pdf \
    https://example.com/

Inspect element boundaries

Highlight element boxes to investigate spacing and layout.

Highlight backgrounds

html2pdf '--custom-javascript=
    libPdfcrowd.highlightHtmlElements({
        backgroundColor: '"'"'rgba(255, 191, 0, 0.1)'"'"',
        borderColor: null
    });' \
    --output highlight_background.pdf \
    https://example.com/

Add borders and spacing

html2pdf '--custom-javascript=
    libPdfcrowd.highlightHtmlElements({
        borderColor: '"'"'orange'"'"',
        backgroundColor: null,
        padding: '"'"'4px'"'"',
        margin: '"'"'4px'"'"'
    });' \
    --output highlight_borders.pdf \
    https://example.com/

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

Render a template with JSON data

Upload existing template.html and data.json files. See the template rendering guide for template syntax, data mapping, and other supported data formats.

html2pdf --content-viewport-width=balanced \
    --data-format=json \
    --data-file=data.json \
    --output output.pdf \
    template.html

PDF features

Create a fillable PDF

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

html2pdf --content-viewport-width=balanced \
    --enable-pdf-forms \
    --output form.pdf \
    form.html

See fillable PDF forms for supported controls and attributes.

Set PDF metadata

html2pdf --author=PDFCrowd \
    --keywords=PDFCrowd,demo \
    --subject=Demo \
    '--title=Hello World' \
    --output with_metadata.pdf \
    https://example.com/

Set a presentation view

html2pdf --initial-zoom-type=fit-page \
    --no-margins \
    --orientation=landscape \
    --page-layout=single-page \
    --page-mode=full-screen \
    --output slide_show.pdf \
    https://example.com/

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