HTML to Image API - HTTP Documentation

Overview

The Pdfcrowd API is HTTP-based, the communication is made through normal HTTP requests. You can call the API by sending an HTTP request to the API server address with options passed as POST data.

The POST request's content type must be multipart/form-data if the request includes any local files. Otherwise it can be application/x-www-form-urlencoded too.

You can also check out our API client libraries if you want to implement the API in your favorite programming language.

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 sign up for the Pdfcrowd API here.

The authentication method for user credentials is HTTP Basic Access Authentication. You provide your credentials every time you make a request.

Server Address

The server address is https://api.pdfcrowd.com/convert/

Both HTTP and HTTPS protocols are supported.

Examples

Convert a web page to a PNG file
curl -f -u "your_username:your_apikey" \
    -o example.png  \
    -F "output_format=png"  \
    -F "url=http://www.example.com" \
    https://api.pdfcrowd.com/convert/
Convert a local HTML file to a PNG file
curl -f -u "your_username:your_apikey" \
    -o MyLayout.png  \
    -F "output_format=png"  \
    -F "file=@/path/to/MyLayout.html" \
    https://api.pdfcrowd.com/convert/
Convert a string containing HTML to a PNG file
curl -f -u "your_username:your_apikey" \
    -o HelloWorld.png  \
    -F "output_format=png"  \
    -F "text= <html><body><h1>Hello World!</h1></body></html>" \
    https://api.pdfcrowd.com/convert/

# or use custom HTML producer
html_producer | curl -u "your_username:your_apikey" \
    -o HelloWorld.png  \
    -F "output_format=png"  \
    -F "text=<-" \
    https://api.pdfcrowd.com/convert/

Tips & Tricks

The API lets you convert a web page, a local HTML file, or a string containing HTML.

The best way to start with the API is to choose one of the examples and once you get it working, you can:

You can also use these HTML related features:

  • You can use the following classes in your HTML code which hide/remove elements from the output:
    • pdfcrowd-remove - sets display:none on the element
    • pdfcrowd-hide - sets visibility:hidden on the element
  • You can switch to the print version of the page (if it exists) with use_print_media.
  • You can force a page break with
    <div style="page-break-before:always"></div>
  • You can avoid a page break inside an element with the following CSS
    img { page-break-inside:avoid }
  • You can use custom_javascript to alter the HTML contents with a custom JavaScript.

HTML to Image API Reference

Conversion Format

Parameter Description Default
output_format
The format of the output file.
Allowed values:
  • png
  • jpg
  • gif
  • tiff
  • bmp
  • ico
  • ppm
  • pgm
  • pbm
  • pnm
  • psb
  • pct
  • ras
  • tga
  • sgi
  • sun
  • webp

 

Conversion Input

Parameter Description Default
url
The address of the web page to convert.
The supported protocols are http:// and https://.
file
The path to a local file to convert.
The file can be either a single file or an archive (.tar.gz, .tar.bz2, or .zip).
If the HTML document refers to local external assets (images, style sheets, javascript), zip the document together with the assets.
The file must exist and not be empty.
The file name must have a valid extension.
text
The string content to convert.
The string must not be empty.

 

Response

Parameter Description Default
output_name
The file name of the created file (max 180 chars). If not specified then the name is auto-generated.
content_disposition
The value of the Content-Disposition HTTP header sent in the response.
Allowed values:
  • attachment
    Forces the browser to pop up a Save As dialog.
  • inline
    The browser will open the result file in the browser window.
attachment

 

General Options

Parameter Description Default
no_background
Do not print the background graphics.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
disable_javascript
Do not execute JavaScript.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
disable_image_loading
Do not load images.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
disable_remote_fonts
Disable loading fonts from remote sources.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
block_ads
Try to block ads. Enabling this option can produce smaller output and speed up the conversion.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
default_encoding
Set the default HTML content text encoding.
auto detect
http_auth_user_name
Set the HTTP authentication user name.
http_auth_password
Set the HTTP authentication password.
use_print_media
Use the print version of the page if available (@media print).
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
no_xpdfcrowd_header
Do not send the X-Pdfcrowd HTTP header in Pdfcrowd HTTP requests.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
cookies
Set cookies that are sent in Pdfcrowd HTTP requests.
Examples:
  • session=6d7184b3bf35;token=2710
verify_ssl_certificates
Do not allow insecure HTTPS connections.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
fail_on_main_url_error
Abort the conversion if the main URL HTTP status code is greater than or equal to 400.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
fail_on_any_url_error
Abort the conversion if any of the sub-request HTTP status code is greater than or equal to 400.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false
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, ...).
The string must not be empty.
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.
Examples:
  • X-My-Client-ID:k2017-12345
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.
Must be a positive integer number or 0.
200
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.
The string must not be empty.
Examples:
  • The first element with the id main-content is converted.
    #main-content
  • The first element with the class name main-content is converted.
    .main-content
  • The first element with the tag name table is converted.
    table
  • The first element with the tag name table or with the id main-content is converted.
    table, #main-content
  • The first element <p class="article"> within <div class="user-panel main"> is converted.
    div.user-panel.main p.article
element_to_convert_mode
Specify the DOM handling when only a part of the document is converted.
Allowed values:
  • cut-out
    The element and its children are cut out of the document.
  • remove-siblings
    All element's siblings are removed.
  • hide-siblings
    All element's sibilings are hidden.
cut-out
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.
The string must not be empty.
Examples:
  • Wait until an element with the id main-content is found.
    #main-content
  • Wait until an element with the class name main-content is found.
    .main-content
  • Wait until an element with the tag name table is found.
    table
  • Wait until an element with the tag name table or with the id main-content is found.
    table, #main-content
  • Wait until <p class="article"> is found within <div class="user-panel main">.
    div.user-panel.main p.article

 

Image Output

Parameter Description Default
screenshot_width
Set the output image width in pixels.
The value must be in a range 96-7680.
1024
screenshot_height
Set the output image height in pixels. If it's not specified, actual document height is used.
Must be a positive integer number.

 

Miscellaneous

Parameter Description Default
debug_log
Turn on the debug logging. Details about the conversion are stored in the debug log. The URL of the log is returned in the X-Pdfcrowd-Debug-Log response header.
Allowed values:
  • true, 1 or on
  • false, 0 or off
false

 

Response Headers

HTTP response can contain the following headers.
You can find details about each conversion in your conversion log.

Name Description
X-Pdfcrowd-Debug-Log URL to the debug log
X-Pdfcrowd-Remaining-Credits the number of available conversion credits in your account
X-Pdfcrowd-Consumed-Credits the number of credits consumed by the conversion
X-Pdfcrowd-Job-Id the unique ID of the conversion
X-Pdfcrowd-Pages the total number of pages in the output document
X-Pdfcrowd-Output-Size the size of the output in bytes

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.