HTML to PDF API - CLI

Learn how to convert web pages and HTML documents to PDF from the command line using the Pdfcrowd API v2.

Installation

Install the application from PyPI
pip install pdfcrowd

Learn more about other install 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.
Sign up for a Free Trial

Examples

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

Advanced Examples

html2pdf -user-name "your_username" -api-key "your_apikey" \
    -page-size "Letter" \
    -orientation "landscape" \
    -no-margins \
    "http://www.example.com" > letter_landscape.pdf
html2pdf -user-name "your_username" -api-key "your_apikey" \
    -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 "0mm" \
    -margin-bottom "0mm" \
    "http://www.example.com" > header_footer.pdf
html2pdf -user-name "your_username" -api-key "your_apikey" \
    -scale-factor 300 \
    "http://www.example.com" > zoom_300.pdf
html2pdf -user-name "your_username" -api-key "your_apikey" \
    -author "Pdfcrowd" \
    -title "Hello" \
    -subject "Demo" \
    -keywords "Pdfcrowd,demo" \
    "http://www.example.com" > with_metadata.pdf
html2pdf -user-name "your_username" -api-key "your_apikey" \
    -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 "your_username" -api-key "your_apikey" \
    -element-to-convert "#main" \
    "https://pdfcrowd.com/doc/api/" > html_part.pdf
html2pdf -user-name "your_username" -api-key "your_apikey" \
    -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 "your_username" -api-key "your_apikey" \
    -custom-javascript "libPdfcrowd.highlightHtml(false, true, true, false)" \
    "http://www.example.com" > highlight_background.pdf
html2pdf -user-name "your_username" -api-key "your_apikey" \
    -custom-javascript "libPdfcrowd.highlightHtml(true, false, true, true)" \
    "http://www.example.com" > highlight_borders.pdf

Troubleshooting

  • Check API Status Codes in case of the 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.highlightHtml(borders, backgrounds, labels, noZeroSpace) method call to visualize all HTML elements. See example.
  • Take a look at the FAQ section.

html2pdf Manual

Conversion from HTML to PDF.

usage: html2pdf [options] source

Conversion from HTML to PDF.

positional arguments:
  source                Source to be converted. It can be URL, path to a local
                        file or '-' to use stdin as an input text.

optional arguments:
  -user-name USER_NAME  Your user name at pdfcrowd.com.
  -api-key API_KEY      Your API key at pdfcrowd.com.
  -page-size PAGE_SIZE  Set the output page size. Allowed values are A2, A3,
                        A4, A5, A6, Letter.
  -page-width PAGE_WIDTH
                        Set the output page width. The safe maximum is 200in
                        otherwise some PDF viewers may be unable to open the
                        PDF. Can be specified in inches (in), millimeters
                        (mm), centimeters (cm), or points (pt).
  -page-height PAGE_HEIGHT
                        Set the output page height. Use -1 for a single page
                        PDF. The safe maximum is 200in otherwise some PDF
                        viewers may be unable to open the PDF. Can be -1 or
                        specified in inches (in), millimeters (mm),
                        centimeters (cm), or points (pt).
  -page-dimensions PAGE_DIMENSIONS
                        Set the output page dimensions. PAGE_DIMENSIONS must
                        contain 2 values separated by a semicolon. Set the
                        output page width. The safe maximum is 200in otherwise
                        some PDF viewers may be unable to open the PDF. Can be
                        specified in inches (in), millimeters (mm),
                        centimeters (cm), or points (pt). Set the output page
                        height. Use -1 for a single page PDF. The safe maximum
                        is 200in otherwise some PDF viewers may be unable to
                        open the PDF. Can be -1 or specified in inches (in),
                        millimeters (mm), centimeters (cm), or points (pt).
  -orientation ORIENTATION
                        Set the output page orientation. Allowed values are
                        landscape, portrait.
  -margin-top MARGIN_TOP
                        Set the output page top margin. Can be specified in
                        inches (in), millimeters (mm), centimeters (cm), or
                        points (pt).
  -margin-right MARGIN_RIGHT
                        Set the output page right margin. Can be specified in
                        inches (in), millimeters (mm), centimeters (cm), or
                        points (pt).
  -margin-bottom MARGIN_BOTTOM
                        Set the output page bottom margin. Can be specified in
                        inches (in), millimeters (mm), centimeters (cm), or
                        points (pt).
  -margin-left MARGIN_LEFT
                        Set the output page left margin. Can be specified in
                        inches (in), millimeters (mm), centimeters (cm), or
                        points (pt).
  -no-margins           Disable margins.
  -page-margins PAGE_MARGINS
                        Set the output page margins. PAGE_MARGINS must contain
                        4 values separated by a semicolon. Set the output page
                        top margin. Set the output page right margin. Set the
                        output page bottom margin. Set the output page left
                        margin. All values can be specified in inches (in),
                        millimeters (mm), centimeters (cm), or points (pt).
  -header-url HEADER_URL
                        Load an HTML code from the specified URL and use it as
                        the page header. The following classes can be used in
                        the HTML. The content of the respective elements will
                        be expanded as follows: pdfcrowd-page-count - the
                        total page count of printed pages pdfcrowd-page-number
                        - the current page number pdfcrowd-source-url - the
                        source URL of a converted document The following
                        attributes can be used: data-pdfcrowd-number-format -
                        specifies the type of the used numerals Arabic
                        numerals are used by default. Roman numerals can be
                        generated by the roman and roman-lowercase values
                        Example: <span class='pdfcrowd-page-number' data-
                        pdfcrowd-number-format='roman'></span> data-pdfcrowd-
                        placement - specifies where to place the source URL,
                        allowed values: The URL is inserted to the content
                        Example: <span class='pdfcrowd-source-url'></span>
                        will produce <span>http://example.com</span> href -
                        the URL is set to the href attribute Example: <a class
                        ='pdfcrowd-source-url' data-pdfcrowd-
                        placement='href'>Link to source</a> will produce <a
                        href='http://example.com'>Link to source</a> href-and-
                        content - the URL is set to the href attribute and to
                        the content Example: <a class='pdfcrowd-source-url'
                        data-pdfcrowd-placement='href-and-content'></a> will
                        produce <a
                        href='http://example.com'>http://example.com</a> The
                        supported protocols are http:// and https://.
  -header-html HEADER_HTML
                        Use the specified HTML code as the page header. The
                        following classes can be used in the HTML. The content
                        of the respective elements will be expanded as
                        follows: pdfcrowd-page-count - the total page count of
                        printed pages pdfcrowd-page-number - the current page
                        number pdfcrowd-source-url - the source URL of a
                        converted document The following attributes can be
                        used: data-pdfcrowd-number-format - specifies the type
                        of the used numerals Arabic numerals are used by
                        default. Roman numerals can be generated by the roman
                        and roman-lowercase values Example: <span class
                        ='pdfcrowd-page-number' data-pdfcrowd-number-
                        format='roman'></span> data-pdfcrowd-placement -
                        specifies where to place the source URL, allowed
                        values: The URL is inserted to the content Example:
                        <span class='pdfcrowd-source-url'></span> will produce
                        <span>http://example.com</span> href - the URL is set
                        to the href attribute Example: <a class='pdfcrowd-
                        source-url' data-pdfcrowd-placement='href'>Link to
                        source</a> will produce <a
                        href='http://example.com'>Link to source</a> href-and-
                        content - the URL is set to the href attribute and to
                        the content Example: <a class='pdfcrowd-source-url'
                        data-pdfcrowd-placement='href-and-content'></a> will
                        produce <a
                        href='http://example.com'>http://example.com</a> The
                        string must not be empty.
  -header-height HEADER_HEIGHT
                        Set the header height. Can be specified in inches
                        (in), millimeters (mm), centimeters (cm), or points
                        (pt).
  -footer-url FOOTER_URL
                        Load an HTML code from the specified URL and use it as
                        the page footer. The following classes can be used in
                        the HTML. The content of the respective elements will
                        be expanded as follows: pdfcrowd-page-count - the
                        total page count of printed pages pdfcrowd-page-number
                        - the current page number pdfcrowd-source-url - the
                        source URL of a converted document The following
                        attributes can be used: data-pdfcrowd-number-format -
                        specifies the type of the used numerals Arabic
                        numerals are used by default. Roman numerals can be
                        generated by the roman and roman-lowercase values
                        Example: <span class='pdfcrowd-page-number' data-
                        pdfcrowd-number-format='roman'></span> data-pdfcrowd-
                        placement - specifies where to place the source URL,
                        allowed values: The URL is inserted to the content
                        Example: <span class='pdfcrowd-source-url'></span>
                        will produce <span>http://example.com</span> href -
                        the URL is set to the href attribute Example: <a class
                        ='pdfcrowd-source-url' data-pdfcrowd-
                        placement='href'>Link to source</a> will produce <a
                        href='http://example.com'>Link to source</a> href-and-
                        content - the URL is set to the href attribute and to
                        the content Example: <a class='pdfcrowd-source-url'
                        data-pdfcrowd-placement='href-and-content'></a> will
                        produce <a
                        href='http://example.com'>http://example.com</a> The
                        supported protocols are http:// and https://.
  -footer-html FOOTER_HTML
                        Use the specified HTML as the page footer. The
                        following classes can be used in the HTML. The content
                        of the respective elements will be expanded as
                        follows: pdfcrowd-page-count - the total page count of
                        printed pages pdfcrowd-page-number - the current page
                        number pdfcrowd-source-url - the source URL of a
                        converted document The following attributes can be
                        used: data-pdfcrowd-number-format - specifies the type
                        of the used numerals Arabic numerals are used by
                        default. Roman numerals can be generated by the roman
                        and roman-lowercase values Example: <span class
                        ='pdfcrowd-page-number' data-pdfcrowd-number-
                        format='roman'></span> data-pdfcrowd-placement -
                        specifies where to place the source URL, allowed
                        values: The URL is inserted to the content Example:
                        <span class='pdfcrowd-source-url'></span> will produce
                        <span>http://example.com</span> href - the URL is set
                        to the href attribute Example: <a class='pdfcrowd-
                        source-url' data-pdfcrowd-placement='href'>Link to
                        source</a> will produce <a
                        href='http://example.com'>Link to source</a> href-and-
                        content - the URL is set to the href attribute and to
                        the content Example: <a class='pdfcrowd-source-url'
                        data-pdfcrowd-placement='href-and-content'></a> will
                        produce <a
                        href='http://example.com'>http://example.com</a> The
                        string must not be empty.
  -footer-height FOOTER_HEIGHT
                        Set the footer height. Can be specified in inches
                        (in), millimeters (mm), centimeters (cm), or points
                        (pt).
  -print-page-range PRINT_PAGE_RANGE
                        Set the page range to print. A comma seperated list of
                        page numbers or ranges.
  -page-background-color PAGE_BACKGROUND_COLOR
                        The page background color in RGB or RGBA hexadecimal
                        format. The color fills the entire page regardless of
                        the margins. The value must be in RRGGBB or RRGGBBAA
                        hexadecimal format.
  -page-watermark PAGE_WATERMARK
                        Apply the first page of the watermark PDF to every
                        page of the output PDF. The file path to a local
                        watermark PDF file. The file must exist and not be
                        empty.
  -multipage-watermark MULTIPAGE_WATERMARK
                        Apply each page of the specified watermark PDF to the
                        corresponding page of the output PDF. The file path to
                        a local watermark PDF file. The file must exist and
                        not be empty.
  -page-background PAGE_BACKGROUND
                        Apply the first page of the specified PDF to the
                        background of every page of the output PDF. The file
                        path to a local background PDF file. The file must
                        exist and not be empty.
  -multipage-background MULTIPAGE_BACKGROUND
                        Apply each page of the specified PDF to the background
                        of the corresponding page of the output PDF. The file
                        path to a local background PDF file. The file must
                        exist and not be empty.
  -exclude-header-on-pages EXCLUDE_HEADER_ON_PAGES
                        The page header is not printed on the specified pages.
                        List of physical page numbers. Negative numbers count
                        backwards from the last page: -1 is the last page, -2
                        is the last but one page, and so on. A comma seperated
                        list of page numbers.
  -exclude-footer-on-pages EXCLUDE_FOOTER_ON_PAGES
                        The page footer is not printed on the specified pages.
                        List of physical page numbers. Negative numbers count
                        backwards from the last page: -1 is the last page, -2
                        is the last but one page, and so on. A comma seperated
                        list of page numbers.
  -page-numbering-offset PAGE_NUMBERING_OFFSET
                        Set an offset between physical and logical page
                        numbers. Integer specifying page offset.
  -no-background        Do not print the background graphics.
  -disable-javascript   Do not execute JavaScript.
  -disable-image-loading
                        Do not load images.
  -disable-remote-fonts
                        Disable loading fonts from remote sources.
  -block-ads            Try to block ads. Enabling this option can produce
                        smaller output and speed up the conversion.
  -default-encoding DEFAULT_ENCODING
                        Set the default HTML content text encoding. The text
                        encoding of the HTML content.
  -http-auth HTTP_AUTH  Set credentials to access HTTP base authentication
                        protected websites. HTTP_AUTH must contain 2 values
                        separated by a semicolon. Set the HTTP authentication
                        user name. Set the HTTP authentication password.
  -use-print-media      Use the print version of the page if available (@media
                        print).
  -no-xpdfcrowd-header  Do not send the X-Pdfcrowd HTTP header in Pdfcrowd
                        HTTP requests.
  -cookies COOKIES      Set cookies that are sent in Pdfcrowd HTTP requests.
                        The cookie string.
  -verify-ssl-certificates
                        Do not allow insecure HTTPS connections.
  -fail-on-main-url-error
                        Abort the conversion if the main URL HTTP status code
                        is greater than or equal to 400.
  -fail-on-any-url-error
                        Abort the conversion if any of the sub-request HTTP
                        status code is greater than or equal to 400 or if some
                        sub-requests are still pending. See details in a debug
                        log.
  -custom-javascript CUSTOM_JAVASCRIPT
                        Run a custom JavaScript after the document is loaded.
                        The script is intended for post-load DOM manipulation
                        (add/remove elements, update CSS, ...). String
                        containing a JavaScript code. The string must not be
                        empty.
  -custom-http-header CUSTOM_HTTP_HEADER
                        Set a custom HTTP header that is sent in Pdfcrowd HTTP
                        requests. A string containing the header name and
                        value separated by a colon.
  -javascript-delay JAVASCRIPT_DELAY
                        Wait the specified number of milliseconds to finish
                        all JavaScript after the document is loaded. The
                        maximum value is determined by your API license. The
                        number of milliseconds to wait. Must be a positive
                        integer number or 0.
  -element-to-convert ELEMENT_TO_CONVERT
                        Convert only the specified element from the main
                        document and its children. The element is specified by
                        one or more CSS selectors. If the element is not
                        found, the conversion fails. If multiple elements are
                        found, the first one is used. One or more CSS
                        selectors separated by commas. The string must not be
                        empty.
  -element-to-convert-mode ELEMENT_TO_CONVERT_MODE
                        Specify the DOM handling when only a part of the
                        document is converted. Allowed values are cut-out,
                        remove-siblings, hide-siblings.
  -wait-for-element WAIT_FOR_ELEMENT
                        Wait for the specified element in a source document.
                        The element is specified by one or more CSS selectors.
                        The element is searched for in the main document and
                        all iframes. If the element is not found, the
                        conversion fails. Your API license defines the maximum
                        wait time by "Max Delay" parameter. One or more CSS
                        selectors separated by commas. The string must not be
                        empty.
  -viewport VIEWPORT    Set the viewport size. The viewport is the user's
                        visible area of the page. VIEWPORT must contain 2
                        values separated by a semicolon. Set the viewport
                        width in pixels. The viewport is the user's visible
                        area of the page. The value must be in a range
                        96-7680. Set the viewport height in pixels. The
                        viewport is the user's visible area of the page. Must
                        be a positive integer number.
  -rendering-mode RENDERING_MODE
                        Sets the rendering mode. The rendering mode. Allowed
                        values are default, viewport.
  -scale-factor SCALE_FACTOR
                        Set the scaling factor (zoom) for the main page area.
                        The scale factor. The value must be in a range 10-500.
  -header-footer-scale-factor HEADER_FOOTER_SCALE_FACTOR
                        Set the scaling factor (zoom) for the header and
                        footer. The scale factor. The value must be in a range
                        10-500.
  -disable-smart-shrinking
                        Disable the intelligent shrinking strategy that tries
                        to optimally fit the HTML contents to a PDF page.
  -linearize            Create linearized PDF. This is also known as Fast Web
                        View.
  -encrypt              Encrypt the PDF. This prevents search engines from
                        indexing the contents.
  -user-password USER_PASSWORD
                        Protect the PDF with a user password. When a PDF has a
                        user password, it must be supplied in order to view
                        the document and to perform operations allowed by the
                        access permissions. The user password.
  -owner-password OWNER_PASSWORD
                        Protect the PDF with an owner password. Supplying an
                        owner password grants unlimited access to the PDF
                        including changing the passwords and access
                        permissions. The owner password.
  -no-print             Disallow printing of the output PDF.
  -no-modify            Disallow modification of the ouput PDF.
  -no-copy              Disallow text and graphics extraction from the output
                        PDF.
  -title TITLE          Set the title of the PDF. The title.
  -subject SUBJECT      Set the subject of the PDF. The subject.
  -author AUTHOR        Set the author of the PDF. The author.
  -keywords KEYWORDS    Associate keywords with the document. The string with
                        the keywords.
  -page-layout PAGE_LAYOUT
                        Specify the page layout to be used when the document
                        is opened. Allowed values are single-page, one-column,
                        two-column-left, two-column-right.
  -page-mode PAGE_MODE  Specify how the document should be displayed when
                        opened. Allowed values are full-screen, thumbnails,
                        outlines.
  -initial-zoom-type INITIAL_ZOOM_TYPE
                        Specify how the page should be displayed when opened.
                        Allowed values are fit-width, fit-height, fit-page.
  -initial-page INITIAL_PAGE
                        Display the specified page when the document is
                        opened. Must be a positive integer number.
  -initial-zoom INITIAL_ZOOM
                        Specify the initial page zoom in percents when the
                        document is opened. Must be a positive integer number.
  -hide-toolbar         Specify whether to hide the viewer application's tool
                        bars when the document is active.
  -hide-menubar         Specify whether to hide the viewer application's menu
                        bar when the document is active.
  -hide-window-ui       Specify whether to hide user interface elements in the
                        document's window (such as scroll bars and navigation
                        controls), leaving only the document's contents
                        displayed.
  -fit-window           Specify whether to resize the document's window to fit
                        the size of the first displayed page.
  -center-window        Specify whether to position the document's window in
                        the center of the screen.
  -display-title        Specify whether the window's title bar should display
                        the document title. If false , the title bar should
                        instead display the name of the PDF file containing
                        the document.
  -right-to-left        Set the predominant reading order for text to right-
                        to-left. This option has no direct effect on the
                        document's contents or page numbering but can be used
                        to determine the relative positioning of pages when
                        displayed side by side or printed n-up
  -debug-log            Turn on the debug logging. Details about the
                        conversion are stored in the debug log.
  -tag TAG              Tag the conversion with a custom value. The tag is
                        used in conversion statistics. A value longer than 32
                        characters is cut off. A string with the custom tag.
  -http-proxy HTTP_PROXY
                        A proxy server used by Pdfcrowd conversion process for
                        accessing the source URLs with HTTP scheme. It can
                        help to circumvent regional restrictions or provide
                        limited access to your intranet. The value must have
                        format DOMAIN_OR_IP_ADDRESS:PORT.
  -https-proxy HTTPS_PROXY
                        A proxy server used by Pdfcrowd conversion process for
                        accessing the source URLs with HTTPS scheme. It can
                        help to circumvent regional restrictions or provide
                        limited access to your intranet. The value must have
                        format DOMAIN_OR_IP_ADDRESS:PORT.
  -client-certificate CLIENT_CERTIFICATE
                        A client certificate to authenticate Pdfcrowd
                        converter on your web server. The certificate is used
                        for two-way SSL/TLS authentication and adds extra
                        security. The file must be in PKCS12 format. The file
                        must exist and not be empty.
  -client-certificate-password CLIENT_CERTIFICATE_PASSWORD
                        A password for PKCS12 file with a client certificate
                        if it's needed.
  -use-http             Specifies if the client communicates over HTTP or
                        HTTPS with Pdfcrowd API.
  -user-agent USER_AGENT
                        Set a custom user agent HTTP header. It can be usefull
                        if you are behind some proxy or firewall. The user
                        agent string.
  -proxy PROXY          Specifies an HTTP proxy that the API client library
                        will use to connect to the internet. PROXY must
                        contain 4 values separated by a semicolon. The proxy
                        hostname. The proxy port. The username. The password.
  -retry-count RETRY_COUNT
                        Specifies the number of retries when the 502 HTTP
                        status code is received. The 502 status code indicates
                        a temporary network issue. This feature can be
                        disabled by setting to 0. Number of retries wanted.

produced by: www.pdfcrowd.com