HTML to PDF in Command Line

Overview

This page describes how to convert web pages and HTML documents to PDF from the command line using the Pdfcrowd API.

Getting Started

Enter a web page or upload an HTML file to get a PDF preview and integration code that you can copy and paste to your application.

Or choose from the examples below and adapt the code to your needs.

You can also interactively explore the API in our API Playground.

Installation

Install the application from PyPI
pip install pdfcrowd

We also offer other installation options.

Authentication

Authentication is needed in order to use the Pdfcrowd API. The credentials used for accessing the API are your Pdfcrowd username and the API key.

You can try out the API without registration using the following demo credentials:
  • Username: demo
  • API key: ce544b6ea52a5621fb9d55f8b542d14d

Or you can start a free trial or purchase the API license to get your personal API credentials.

Examples

html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    "http://www.example.com" > "example.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    "/path/to/MyLayout.html" > "MyLayout.pdf"
echo -n "<html><body><h1>Hello World!</h1></body></html>" | \
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" - > "HelloWorld.pdf"

Advanced Examples

html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -page-size "Letter" \
    -orientation "landscape" \
    -no-margins \
    "http://www.example.com" > "letter_landscape.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -header-height "15mm" \
    -footer-height "10mm" \
    -header-html "<a class='pdfcrowd-source-url' data-pdfcrowd-placement='href-and-content'></a>" \
    -footer-html "<center><span class='pdfcrowd-page-number'></span></center>" \
    -margin-top "0" \
    -margin-bottom "0" \
    "http://www.example.com" > "header_footer.pdf"
echo -n "<html><body>Enter name:<input type=text></body></html>" | \
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -enable-pdf-forms - > "form.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -scale-factor 300 \
    "http://www.example.com" > "zoom_300.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -author "Pdfcrowd" \
    -title "Hello World" \
    -subject "Demo" \
    -keywords "Pdfcrowd,demo" \
    "http://www.example.com" > "with_metadata.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -page-layout "single-page" \
    -page-mode "full-screen" \
    -initial-zoom-type "fit-page" \
    -orientation "landscape" \
    -no-margins \
    "https://pdfcrowd.com/doc/api/" > "slide_show.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -element-to-convert "#main" \
    "https://pdfcrowd.com/doc/api/" > "html_part.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -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)" \
    "http://www.example.com" > "html_inject.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -viewport-width 992 \
    -rendering-mode "viewport" \
    -smart-scaling-mode "viewport-fit" \
    -no-margins \
    "https://getbootstrap.com/" > "bootstrap.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -custom-javascript "libPdfcrowd.highlightHtmlElements({backgroundColor: 'rgba(255, 191, 0, 0.1)', borderColor:null})" \
    "http://www.example.com" > "highlight_background.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -custom-javascript "libPdfcrowd.highlightHtmlElements({borderColor: 'orange', backgroundColor: null, padding: '4px', margin: '4px'})" \
    "http://www.example.com" > "highlight_borders.pdf"

Integration Examples

The API can be easily integrated into your environment. You can have our interactive API Playground autogenerate the integration code for you:

Template Rendering

The API enables rendering of HTML templates. The template syntax is based on the Jinja template rendering engine.

The most common constructs are:

  • Data rendering: Invoice: {{ invoice.number }}
  • For loop: {% for invoice in invoices %} ... {% endfor %}
  • If statement: {% if invoice.total > 100 %} ... {% endif %}
  • Data filter: {{ invoice.to.first_name|capitalize }}

The supported input data formats are JSON, XML, CSV and YAML. The data can be uploaded from a file or from a string variable.

Supported template filters: capitalize, center, default, escape, first, forceescape, format, indent, join, last, length, list, lower, replace, reverse, safe, slice, sort, string, striptags, title, trim, truncate, unique, upper, wordcount, wordwrap.

Template Rendering Examples

echo -n "Hello {{ name }} from {{ product }}" | \
html2pdf -user-name "{{ user_name }}" -api-key "{{ api_key }}" \
    -data-string "$(echo -e "{
            \"name\": \"World\",
            \"product\": \"Pdfcrowd API\"
        }")" - > "output.pdf"
echo -n "Hello {{ data.name }} from {{ data.product }}" | \
html2pdf -user-name "{{ user_name }}" -api-key "{{ api_key }}" \
    -data-string "$(echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
        <data>
          <name>World</name>
          <product>Pdfcrowd API</product>
        </data>")" - > "output.pdf"
echo -n "Hello {{ name }} from {{ product }}" | \
html2pdf -user-name "{{ user_name }}" -api-key "{{ api_key }}" \
    -data-string "$(echo -e "name: World
product: Pdfcrowd API")" - > "output.pdf"
echo -n "Hello {{ name }} from {{ product }}" | \
html2pdf -user-name "{{ user_name }}" -api-key "{{ api_key }}" \
    -data-string "$(echo -e "name,product
World,Pdfcrowd API")" - > "output.pdf"

Troubleshooting

  • List of API Status Codes if an error code is returned.
  • You can use -debug-log to get detailed info about the conversion, such as conversion errors, time, console output.
  • You can use our JavaScript library to resolve rendering problems, such as missing content or blank pages.
    Just use -custom-javascript with libPdfcrowd.highlightHtmlElements method call to visualize all HTML elements. See the backgrounds example , borders example and helper JavaScript library documentation.
  • Take a look at the FAQ section.
  • Are you missing a feature? Contact us, we look forward to your suggestions.

User Manual

Refer for details to the User Manual.