HTML to PDF Command Line Code Examples

These command line examples show URL and HTML file conversion to PDF from scripts and terminals.


Basic examples

Webpage to PDF file

html2pdf -user-name "demo" -api-key "demo" \
    -content-viewport-width "balanced" \
    "http://www.example.com" > "example.pdf"

HTML file to PDF file

html2pdf -user-name "demo" -api-key "demo" \
    -content-viewport-width "balanced" \
    "/path/to/MyLayout.html" > "MyLayout.pdf"

HTML string to PDF file

echo -n "<html><body><h1>Hello World!</h1></body></html>" | \
html2pdf -user-name "demo" -api-key "demo" \
    -content-viewport-width "balanced" - > "HelloWorld.pdf"

Advanced examples

Customize the page size and the orientation

html2pdf -user-name "demo" -api-key "demo" \
    -page-size "Letter" \
    -orientation "landscape" \
    -no-margins \
    "http://www.example.com" > "letter_landscape.pdf"

Put the source URL in the header and the page number in the footer

html2pdf -user-name "demo" -api-key "demo" \
    -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"

Create fillable PDF form

echo -n "<html><body>Enter name:<input type=text></body></html>" | \
html2pdf -user-name "demo" -api-key "demo" \
    -enable-pdf-forms - > "form.pdf"

Zoom the HTML document

html2pdf -user-name "demo" -api-key "demo" \
    -scale-factor 300 \
    "http://www.example.com" > "zoom_300.pdf"

Set PDF metadata

html2pdf -user-name "demo" -api-key "demo" \
    -author "Pdfcrowd" \
    -title "Hello World" \
    -subject "Demo" \
    -keywords "Pdfcrowd,demo" \
    "http://www.example.com" > "with_metadata.pdf"

Create a Powerpoint like presentation from an HTML document

html2pdf -user-name "demo" -api-key "demo" \
    -page-layout "single-page" \
    -page-mode "full-screen" \
    -initial-zoom-type "fit-page" \
    -orientation "landscape" \
    -no-margins \
    "https://pdfcrowd.com/api/" > "slide_show.pdf"

Convert an HTML document section

html2pdf -user-name "demo" -api-key "demo" \
    -element-to-convert "#main" \
    "https://pdfcrowd.com/api/" > "html_part.pdf"

Inject an HTML code

html2pdf -user-name "demo" -api-key "demo" \
    -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"

Convert a responsive web page as it appears on a large device

html2pdf -user-name "demo" -api-key "demo" \
    -content-viewport-width "large" \
    -no-margins \
    "https://getbootstrap.com/" > "bootstrap.pdf"

Renderer debugging - highlight HTML elements

html2pdf -user-name "demo" -api-key "demo" \
    -custom-javascript "libPdfcrowd.highlightHtmlElements({backgroundColor: 'rgba(255, 191, 0, 0.1)', borderColor:null})" \
    "http://www.example.com" > "highlight_background.pdf"

Renderer debugging - borders with spacing around HTML elements

html2pdf -user-name "demo" -api-key "demo" \
    -custom-javascript "libPdfcrowd.highlightHtmlElements({borderColor: 'orange', backgroundColor: null, padding: '4px', margin: '4px'})" \
    "http://www.example.com" > "highlight_borders.pdf"

Template rendering examples

Create PDF from JSON data

echo -n "Hello {{ name }} from {{ product }}" | \
html2pdf -user-name "demo" -api-key "demo" \
    -data-string "$(echo -e "{
            \"name\": \"World\",
            \"product\": \"Pdfcrowd API\"
        }")" - > "output.pdf"

Create PDF from XML data

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

Create PDF from YAML data

echo -n "Hello {{ name }} from {{ product }}" | \
html2pdf -user-name "demo" -api-key "demo" \
    -data-string "$(echo -e "name: World
product: Pdfcrowd API")" - > "output.pdf"

Create PDF from CSV data

echo -n "Hello {{ name }} from {{ product }}" | \
html2pdf -user-name "demo" -api-key "demo" \
    -data-string "$(echo -e "name,product
World,Pdfcrowd API")" - > "output.pdf"