HTML To PDF API

SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications. The API is easy to use and the integration takes only a few lines of code.

This page refers to v2 of SelectPdf Online HTML to PDF REST API. To read about v1 of the API, please use the following link: HTML To PDF REST API – v1.

GET A DEMO LICENSE KEY NOW

Features

  • Create PDF from any web page or html string.
  • Full html5/css3/javascript support.
  • Set PDF options such as page size and orientation, margins, security, web page settings.
  • Set PDF viewer options and PDF document information.
  • Create custom headers and footers for the pdf document.
  • Hide web page elements during the conversion.
  • Automatically generate bookmarks during the html to pdf conversion.
  • Support for partial page conversion.
  • Easy integration, no third party libraries needed.
  • Works in all programming languages.
  • No installation required.

Usage

It’s very easy to use SelectPdf Html to Pdf API. All you need is a license key and with SelectPdf online API you will be able to convert any url or html raw code to pdf in an instant.
If you want to see how to do this in PHP, Java, Ruby, Python, Perl, Node.JS, C# or VB.net, click here to go directly to the coding examples.

Here a basic usage example using a GET request:

http://selectpdf.com/api2/convert/?key=your_license_key_here&url=http://www.testurl.com

Here a basic usage example using a POST request:

Method: POST
API endpoint: http://selectpdf.com/api2/convert/
Content Type: application/json
Post Data: {key: 'your_license_key_here', url: 'http://www.testurl.com'}

The request needs the following components:

  1. end point – this should be http://selectpdf.com/api2/convert/.
  2. key – the license key required to use the API. Request a Demo License Key.
  3. url or html – the url or raw html string that will be converted to PDF.

Both GET and POST requests over HTTP or HTTPS can be used with the current version of SelectPdf online REST API. It’s important to remember the following things:

  1. Send all parameters via query string if GET is used or via request body if POST is used.
  2. Parameters url, html and base_url parameters need to be URL encoded if GET is used.
  3. For POST requests, set the Content-Type header to application/x-www-form-urlencoded or application/json, depending on how you send the request parameters.
  4. Use any of the parameters below to customize the pdf conversion. If a parameter is missing, the default value is used by the converter.

Options

Mandatory Parameters

SelectPdf Html To Pdf REST API has only 2 mandatory parameters. The rest of the parameters are optional. When they are missing, the default value is used.

Parameter Description
key Your API license key
url The url of the web page that is converted to pdf
— OR —
html The raw html that is converted to pdf
(base_url – an optional base url parameter can be used together with html to resolve relative paths from the html string)

In addition to the key, url, html and base_url, the following parameters can be customized:

Page Setup

Use the following parameters to customize the size, orientation and margins of the pdf pages from the generated PDF document.

Parameter Description
page_size Specifies the page size of the generated pdf document. The default value is A4. All possible values are: A1, A2, A3, A4, A5, Letter, HalfLetter, Ledger, Legal.
page_orientation Specifies the page orientation of the generated pdf document. The default value is Portrait. All possible values are: Portrait, Landscape.
margin_top, margin_right, margin_bottom, margin_left You can specify values for all 4 margins of the generated pdf document. The margins are specified in points. 1 point is 1 / 72 inch. By default all margins are 5pt.

Header and Footer

Use the following parameters to customize the header, footer and page numbers for the PDF document.

Parameter Description
show_header Controls if a custom header is displayed in the generated pdf document. All possible values are: True, False. The default value is False.
header_height The height of the pdf document header. This height is specified in points. 1 point is 1/72 inch. The default value is 50.
header_url The url of the web page that is converted and rendered in the pdf document header.
header_html The raw html that is converted and rendered in the pdf document header.
header_base_url An optional base url parameter can be used together with header_html to resolve relative paths from the html string.
header_display_on_first_page Controls the visibility of the header on the first page of the generated pdf document. All possible values are: True, False. The default value is True.
header_display_on_odd_pages Controls the visibility of the header on the odd numbered pages of the generated pdf document. All possible values are: True, False. The default value is True.
header_display_on_even_pages Controls the visibility of the header on the even numbered pages of the generated pdf document. All possible values are: True, False. The default value is True.
   
show_footer Controls if a custom footer is displayed in the generated pdf document. All possible values are: True, False. The default value is False.
footer_height The height of the pdf document footer. This height is specified in points. 1 point is 1/72 inch. The default value is 50.
footer_url The url of the web page that is converted and rendered in the pdf document footer.
footer_html The raw html that is converted and rendered in the pdf document footer.
footer_base_url An optional base url parameter can be used together with footer_html to resolve relative paths from the html string.
footer_display_on_first_page Controls the visibility of the footer on the first page of the generated pdf document. All possible values are: True, False. The default value is True.
footer_display_on_odd_pages Controls the visibility of the footer on the odd numbered pages of the generated pdf document. All possible values are: True, False. The default value is True.
footer_display_on_even_pages Controls the visibility of the footer on the even numbered pages of the generated pdf document. All possible values are: True, False. The default value is True.
   
page_numbers If this feature is enabled, in the footer of each pdf page, the current page number will be displayed. The default value is True. All possible values are: True, False.
page_numbers_first Controls the page number for the first page being rendered. The default value is 1.
page_numbers_offset Controls the total number of pages offset in the generated pdf document. The default value is 0.
page_numbers_template The text that is used to display the page numbers. It can contain the placeholder {page_number} for the current page number and {total_pages} for the total number of pages. The default value is “Page: {page_number} of {total_pages}”.
page_numbers_font_name The font used to display the page numbers text. The default value is “Helvetica”.
page_numbers_font_size The size of the font used to display the page numbers. The default value is 10 (points).
page_numbers_alignment The alignment of the page numbers text. All possible values are 1 – align left, 2 – align center, 3 – align right. The default values is 3 – align right.
page_numbers_color Specifies the color of the page numbers text in RGB html format. The default value is #333333.
page_numbers_pos_y Specifies the position in points on the vertical where the page numbers text is displayed in the footer. The default value is 10 (points).

Pdf Options

Use the following parameters to control the PDF document information, security and the look when the document is opened in a client PDF viewer.

Parameter Description
doc_title Specifies the pdf document title.
doc_subject Specifies the subject of the pdf document.
doc_keywords Specifies the pdf document keywords.
doc_author Specifies the name of the pdf document author.
doc_add_creation_date If this is set, the date and time when the pdf document was created is saved in pdf document information. All possible values are: True, False. The default value is False.
   
viewer_page_layout Sets the page layout to be used when the document is opened. All possible values:
  0 – Single Page (displays one page at a time),
  1 – One Column (displays the pages in one column),
  2 – Two Column Left (displays the pages in two columns, with odd-numbered pages on the left),
  3 – Two Column Right (displays the pages in two columns, with odd-numbered pages on the right).
The default value is 1 – One Column.
viewer_page_mode Sets the document page mode when the pdf document is opened in a pdf viewer. All possible values:
  0 – Use None (neither document outline / bookmarks nor thumbnail images are visible)
  1 – Use Outlines (document outline / bookmarks are visible)
  2 – Use Thumbs (page thumbnail images are visible)
  3 – Full Screen (full-screen mode, with no menu bar, window controls or any other window visible)
  4 – Use OC (optional content group panel is visible)
  5 – Use Attachments (document attachments are visible)
The default value is 0 – Use None.
viewer_center_window Sets a flag specifying whether to position the document’s window in the center of the screen. All possible values are: True, False. The default value is False.
viewer_display_doc_title Sets a flag specifying whether the window’s title bar should display the document title taken from document information. All possible values are: True, False. The default value is False.
viewer_fit_window Sets a flag specifying whether to resize the document’s window to fit the size of the first displayed page. All possible values are: True, False. The default value is False.
viewer_hide_menu_bar Sets a flag specifying whether to hide the pdf viewer application’s menu bar when the document is active. All possible values are: True, False. The default value is False.
viewer_hide_toolbar Sets a flag specifying whether to hide the pdf viewer application’s tool bars when the document is active. All possible values are: True, False. The default value is False.
viewer_hide_window_ui Sets a flag specifying 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. All possible values are: True, False. The default value is False.
   
user_password Specifies a password needed to be able to view the generated pdf document.
owner_password Specifies a password needed to be able to view or modify the generated pdf document.
   
pdf_name Specifies the name of the pdf document that will be created. The default value is Document.pdf.
background_color Specifies the background color of the pdf page in RGB html format. The default is #FFFFFF.
draw_html_background Sets a flag indicating if the web page background is rendered in pdf. All possible values are: True, False. The default value is True.

Web Page Options

Use the following parameters to control how the converted web page is loaded.

Parameter Description
web_page_width The width used by the converter’s internal browser window. The default value is 1024px.
web_page_height The height used by the converter’s internal browser window. The default value is 0px and it means that the page height is automatically calculated by the converter.
min_load_time Introduces a delay (in seconds) before the actual conversion to allow the web page to fully load. The default value is 1 second. Use a larger value if the web page has content that takes time to render when it is displayed in the browser.
max_load_time It’s the maximum amount of time (in seconds) that the convert will wait for the page to load. A timeout error is displayed when this time elapses. The default value is 30 seconds. Use a larger value (up to 120 seconds allowed) for pages that take a long time to load.
use_css_print Specifies if the CSS Print media type is used instead of the Screen media type. The default value is False. All possible values are: True, False.
disable_javascript Do not run JavaScript in web pages. All possible values are: True, False. The default value is False.
disable_internal_links Do not create internal links in the PDF. All possible values are: True, False. The default value is False.
disable_external_links Do not create external hyperlinks in the PDF. All possible values are: True, False. The default value is False.

Advanced Options

SelectPdf offers some advanced features for the Online HTML to PDF REST API. With SelectPdf you can automatically generate bookmarks in your pdf document based on some html elements from the converted webpage. You can also exclude certain page elements from the conversion or you can even convert a specific section of the page instead of the full web page content.

Parameter Description
pdf_bookmarks_selectors Generate automatic bookmarks in pdf. The elements that will be bookmarked are defined using CSS selectors. For example, the selector for all the H1 elements is “H1”, the selector for all the elements with the CSS class name ‘myclass’ is “*.myclass” and the selector for the elements with the id ‘myid’ is “*#myid”. Read more about CSS selectors here.
pdf_hide_elements Exclude page elements from the conversion. The elements that will be excluded are defined using CSS selectors. For example, the selector for all the H1 elements is “H1”, the selector for all the elements with the CSS class name ‘myclass’ is “*.myclass” and the selector for the elements with the id ‘myid’ is “*#myid”. Read more about CSS selectors here.
pdf_show_only_element_id Convert only a specific section of the web page to pdf. The section that will be converted to pdf is specified by the html element ID. The element can be anything (image, table, table row, div, text, etc).

Return Codes

Our API returns HTTP response codes, which you can check to see if the conversion was successful or not. Here is the list of HTTP codes used by SelectPdf REST API:

Code Description
200 OK The API call succeeded. The pdf document is returned.
400 Bad Request Url or html string not specified. The body of the response contains an explanation in plain text.
401 Authorization Required License key not specified or invalid. The body of the response contains an explanation in plain text.
499 Custom Conversion error. The body of the response contains an explanation in plain text.

Code Examples

Click your favorite language to see examples of how to create PDF from a web page or an HTML string using GET or POST requests.

Html To Pdf Conversion in PHP
Html To Pdf Conversion in Java
Html To Pdf Conversion in Ruby
Html To Pdf Conversion in Python
Html To Pdf Conversion in Perl

Test SelectPdf HTML To PDF online RESTful API Now

Request a Demo License Key for SelectPdf REST API right now. Feel free to ask any questions if needed.

SelectPdf for Cloud’s platform independent Html to Pdf API is a true REST API that can be used with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more. You can use it with any language or platform that supports REST. Almost all platforms and languages support REST and provide native REST clients to work with REST APIs. You do not need to worry about language or platform limitations. You can use it with any platform – web, desktop, mobile, and cloud.