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. The generated PDFs are perfect. That makes SelectPdf API the best html to pdf online service that can be used.

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.
The free trial key for the online API is valid for 7 days and it includes 200 conversions.

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, take a look at the code snippets below or click here for more complex coding examples.

require(“SelectPdf.Api.php”);
 
$client = new SelectPdf\Api\HtmlToPdfClient($apiKey);
$client->setPageSize(SelectPdf\Api\PageSize::A4);
$client->convertUrlToFile($url, $localFile);
 
// Code snippet uses SelectPdf API Client library for PHP

import selectpdf
 
client = selectpdf.HtmlToPdfClient(apiKey)
client.setPageSize(selectpdf.PageSize.A4)
client.convertUrlToFile(url, localFile)
 
# Code snippet uses SelectPdf API Client library for Python

using SelectPdf.Api;
 
HtmlToPdfClient client = new HtmlToPdfClient(apiKey);
client.setPageSize(PageSize.A4);
client.convertUrlToFile(url, localFile);
 
// Code snippet uses SelectPdf API Client library for .NET

package com.selectpdf;
 
HtmlToPdfClient client = new HtmlToPdfClient(apiKey);
client.setPageSize(ApiEnums.PageSize.A4);
client.convertUrlToFile(url, localFile);
 
// Code snippet uses SelectPdf API Client library for Java

var selectpdf = require(‘selectpdf’);
 
var client = new selectpdf.HtmlToPdfClient(apiKey);
client.setPageSize(‘A4’);
 
client.convertUrlToFile(url, localFile,
    function(err, fileName) {
        if (err) return console.log(“An error occurred: ” + err);
    }
);
 
// Code snippet uses SelectPdf API Client library for Node.js

require ‘selectpdf’
 
client = SelectPdf::HtmlToPdfClient.new(api_key)
client.page_size = SelectPdf::PageSize::A4
client.convert_url_to_file(url, local_file)
 
# Code snippet uses SelectPdf API Client library for Ruby

use SelectPdf;
 
my $client = new SelectPdf::HtmlToPdfClient($apiKey);
$client->setPageSize(“A4”)
$client->convertUrlToFile($url, $local_file);
 
# Code snippet uses SelectPdf API Client library for Perl

SelectPdf API can be used with raw HTTP calls. Here is a basic usage example using a GET request:

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

Here is a basic usage example using a POST request:

Method: POST
API endpoint: https://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 https://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.
  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: Custom, A0, A1, A2, A3, A4, A5, A6, A7, A8, Letter, HalfLetter, Ledger, Legal. If the value is Custom, specify the size of the PDF page using page_width and page_height parameters.
page_width, page_height Custom page size parameters specified in points. 1 point is 1 / 72 inch. These are taken into consideration only if page_size parameter is set to Custom.
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.
header_web_page_width The width used by the converter’s internal browser window during the conversion of the header content. The default value is 1024px.
header_web_page_height The height used by the converter’s internal browser window during the conversion of the header content. The default value is 0px and it means that the page height is automatically calculated by the converter.
   
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.
footer_web_page_width The width used by the converter’s internal browser window during the conversion of the footer content. The default value is 1024px.
footer_web_page_height The height used by the converter’s internal browser window during the conversion of the footer content. The default value is 0px and it means that the page height is automatically calculated by the converter.
   
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.
page_breaks_enhanced_algorithm Sets a flag indicating if an enhanced custom page breaks algorithm is used. The enhanced algorithm is a little bit slower but it will prevent the appearance of hidden text in the PDF when custom page breaks are used. All possible values are: True, False. The default value is False.
single_page_pdf Sets a flag indicating the converter to resize the PDF page to fit all the content in a single page. All possible values are: True, False. The default value is False.

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.
engine Sets the rendering engine used for the HTML to PDF conversion. Default value is WebKit. All possible values are: WebKit, Restricted, Blink.
cookies_string Sets the HTTP cookies for the web page being converted. Cookies string is the querystring encoded representation of the cookies name/values.
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.
keep_images_together Avoid breaking images between PDF pages. All possible values are: True, False. The default value is False.
scale_images Sets a flag indicating if the images from the page are scaled during the conversion process. All possible values are: True, False. The default value is False.
startup_mode Set converter startup mode. The default value is Automatic and the conversion is started immediately.
By default this is set to Automatic and the conversion is started as soon as the page loads (and conversion delay set with min_load_time elapses). If set to Manual, the conversion is started only by a javascript call to SelectPdf.startConversion() from within the web page.
All possible values are: Automatic, Manual. The default value is Automatic.

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.

Remarks

  • Concurrency: depending on payment plan, the following number of concurrent requests can be sent to the API:
      Free Trial – 1 request
      Entry Level – 2 requests
      Standard Level – 4 requests
      Advanced Level – 8 requests
      Premium Level – 8 requests
      Ultra Level – 16 requests
      Dedicated Level – 16 requests.

    If more requests are sent, they are either queued or rejected with a Too Many Requests error.

  • Each 50 pages from the generated PDF document is counted as 1 conversion credit.

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
Html To Pdf Conversion in Javascript
Html To Pdf Conversion in VB.NET
Html To Pdf Conversion in C#

New: Client libraries are now available for the following languages:  Python  PHP  .NET  Java  Node.js  Ruby  Perl.

HTML to PDF API Usage Tracking

The subscription status and usage can be tracked using a separate API call.

Here is a basic usage tracking example using a GET request:

https://selectpdf.com/api2/usage/?key=your_license_key_here&get_history=True

Here is a basic usage tracking example using a POST request:

Method: POST
API endpoint: https://selectpdf.com/api2/usage/
Content Type: application/json
Post Data: {key: 'your_license_key_here', get_history: 'True'}

The request needs the following components:

  1. end point – this should be https://selectpdf.com/api2/usage/.
  2. key – the license key required to use the API. Request a Demo License Key.
  3. get_history – (optional) get conversion history for the past months. All possible values are: True, False. The default value is False.

The result looks like this:

Please note that the default response format is JSON. You can change that by setting the Accept header to something else (text/xml for example will return the result in XML format).

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. 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.

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.
The free trial key for the online API is valid for 7 days and it includes 200 conversions.

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. Try now for free the best API to convert HTML to PDF.

SelectPdf also offers the following REST APIs: PDF to TEXT REST API and PDF Merge REST API.