Convert web pages and HTML files to PDF from your terminal or shell scripts with
html2pdf. The command sends your input to PDFCrowd's servers and saves the
converted PDF locally.
Install the command-line client with pip, or see other installation options.
python -m pip install pdfcrowd-cli
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.
Convert a web page and save it as example.pdf:
html2pdf --content-viewport-width=balanced \ --output example.pdf \ https://example.com/
The balanced viewport setting fits the HTML viewport to the print area.
Upload an existing document.html file and save the PDF as document.pdf:
html2pdf --content-viewport-width=balanced \ --output document.pdf \ document.html
For HTML that references local images or stylesheets, see Include Local Assets.
Pass a web page URL, a local HTML file, or an archive as the input. Use - to read
HTML from stdin, including input piped from another program. See
Conversion Input
for the supported forms.
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
For relative resource URLs, add <base href="https://your-site.example/">
to the HTML head, replacing the address with your public website URL, or use absolute URLs.
For HTML templates and structured data, see
template rendering.
Package HTML and its local images, stylesheets, and scripts in an archive, keeping their
relative paths. This example requires the zip utility and assumes an existing
document directory containing your HTML file and any local assets.
It creates document.zip and converts it to
document.pdf:
(cd document && zip -q -r - .) > document.zip && html2pdf \ --content-viewport-width=balanced \ --output document.pdf \ document.zip
When the archive has multiple HTML files, use
--zip-main-filename to select the main document.
See the archive examples
for prepared archives and main-file selection.
Add options before the input. Command-line options override saved conversion defaults.
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/
| Purpose | Options |
|---|---|
| Page size and orientation | --page-size, --orientation |
| Margins | --margin-top, --margin-right, --margin-bottom, --margin-left, --no-margins |
| Headers and footers | --header-html, --footer-html, --header-height, --footer-height |
| Layout and scaling | --content-viewport-width, --scale-factor |
| Page styles and content | --custom-css, --custom-javascript, --use-print-media |
| Wait for dynamic content | --wait-for-element, --javascript-delay |
See the command reference for all options and Configuration for saved defaults. For more tasks, browse the CLI examples.
Conversion runs on PDFCrowd's servers. They cannot directly access your
localhost or private network, and a website may require a login.
Choose an approach that suits your page:
--http-auth,
--cookies, or
--custom-http-header, as required by the site.
These authenticate access to the source website, separately from your PDFCrowd
account.
html2pdf.
Referenced resources must also be accessible through the tunnel or another
reachable URL. Any website authentication still applies.
The examples save the result with
--output PATH.
This preserves an existing file if conversion fails. Shell redirection
(>) opens or truncates its destination before the command runs.
Use --output - to pipe the result to another command.
See the output reference
for terminal behavior and conversion information.
Check the command's exit status before using its output: 0 means success and
a nonzero status means failure. In a shell script, $? immediately after the
conversion contains its status.
Read stderr for the diagnostic message. See errors and exit codes for the meaning of the exit status and reported API errors.
Request rate and concurrency limits depend on your license. Control how quickly
your application submits conversions and how many it runs at once. A
429 response concerns request rate; a 430 response
concerns requests already in progress. The maximum upload size is 300 MB.
The CLI automatically retries a request once after an HTTP 502 or
503 response. Use --retry-count
to change the count, or set it to 0 to disable automatic retries.
Account for these retries when adding retries to a script or job runner.
PDFCrowd stops a conversion that exceeds 60 seconds of processing time.
Reason code 323 identifies this HTML conversion timeout.
Check for slow resource downloads or long-running JavaScript. Allowing your
application to wait longer does not extend the server's processing limit.
Enable --debug-log and request
--print-debug-log-url.
This example saves the converted PDF as response.body and prints the
debug-log URL to stderr:
html2pdf --print-debug-log-url \ --content-viewport-width=balanced \ --debug-log \ --output response.body \ https://example.com/
The debug log includes resource-loading details, timeouts, and browser console messages. You can also find logs in your conversion history. A local or connection failure may occur before a conversion log is available.
--wait-for-element or --javascript-delay and inspect the debug log.--content-viewport-width or enable --use-print-media when the page supplies print styles.--show-config or bypass conversion defaults with --no-config.
For help, contact support
with available diagnostics, the version from html2pdf --version,
and enough detail to reproduce the problem.