HTML to PDF in Command Line

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

Samples

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

To get your personal API credentials, you can start a free API trial or buy the API license.

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 you can choose from the examples below and adapt the code to your needs. You can also interactively explore the API in the API Playground.

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/api/" > "slide_show.pdf"
html2pdf -user-name "demo" -api-key "ce544b6ea52a5621fb9d55f8b542d14d" \
    -element-to-convert "#main" \
    "https://pdfcrowd.com/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:

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.