API

To generate PDF files from your website, a so called REST API is available. By creating a link to htmltopdf.info with the correct parameters, you can easily turn your web page or a web page from another site into a PDF download. The simpliest way to do so is adding a link to http://FreeHTMLtoPDF.com/?convert on your web page. Clicking on that link will offer that page as a PDF download.

<a href="http://FreeHTMLtoPDF.com/?convert">Download as PDF</a>

 A more 'full-featured' example:

<a href="http://FreeHTMLtoPDF.com/?convert=http%3A%2F%2Fwww.google.com&size=US_Letter&orientation=landscape&framesize=800&language=de">Download as PDF</a>

The base URL of the REST API is the URL of this web site: http://FreeHTMLtoPDF.com. The parameters that can be used in this API are described in the table below. All parameters must be url-encoded and can be sent via POST or GET.

 

Please consider to make a donation for this FREE service:

PDF BUTTON

The PDF button is a button with an animated progress bar. To convert a web page, just add the following code to that page:

<a href="//FreeHTMLtoPDF.com/?convert&iconsize=32"><img src="//FreeHTMLtoPDF.com/images/apiicon_32.png" /></a><script type="text/javascript" src="//FreeHTMLtoPDF.com/scripts/api.js" />

Sample (click button to check out the progress bar):

 The parameters that can be used are listed below. For instance, when you want your own download icon change the code to:

<a href="//FreeHTMLtoPDF.com/?convert&customicon=http%3A%2F%2Fwww.myhomepage.com%2Fmydownloadicon.png"><img src="//FreeHTMLtoPDF.com/images/apiicon_32.png" /></a><script type="text/javascript" src="//FreeHTMLtoPDF.com/scripts/api.js" />

Note that all parameters must be url-encoded. There are several online tools to url-encode text if you cannot do it programmatically: http://tiny.cc/urlencode

ParamDefault valueDescription
convertURL from referrerThis is the url to convert. When left empty and html is also empty, the referrer is used.
iconsize32Optional. The size of the pdf button. The following sizes are supported:
  • 16
  • 20
  • 24
  • 32
  • 40
  • 48
  • 64
  • 96
  • 128
  • 256
customiconOptional. The URL of the icon to use as PDF button, instead of the default icon.
titleDownload as PDFOptional. The title of the PDF button. This text is visible when you hover the PDF button.
buttontextOptional. When this parameter is specified, the PDF button will show the given text next to the icon. Example of a PDF button with button text:
localhtml0Optional. Possible values are 0 or 1. When this value is 1, the PDF button can be used on non-public pages. This means that the local html is sent to be converted instead of the url.
enablejsOptional. Possible values are 0 or 1. When this value is 1, javascript on the page will be executed, otherwise it won't.
Default value when localhtml is 0: 1
Default value when localhtml is 1: 0
formdata0Optional. Possible values are 0 or 1. When this value is 1, the values of all form elements as they were at the time the PDF button was clicked will be visible in the PDF document.
widthOptional. The width of the PDF document in inches (in), millimeters (mm) or points (pt). Cannot be combined with size. Example: 210mm
heightOptional. The height of the PDF document in inches (in), millimeters (mm) or points (pt). Cannot be combined with size. Required when width is supplied. Example: 10.5in
sizeautoOptional. Size of the pages within the PDF document. Can be one of the following:
  • A3
  • A4
  • A5
  • Letter
  • Legal
  • Ledger
  • auto
When the value is auto, either A4 or Letter will be used, depending on the country the API call originates from.
Cannot be combined with width or height.
framewidthautoOptional. The size of the virtual browser frame the website is loaded in. When the value is auto, it first searches for the meta tag viewport and when that tag cannot be found, it uses 1366 as the frame width.
orientationportraitOptional. Either "landscape" or "portrait".
languageAccept-Language
header of caller
Optional. The content of the Accept-Language HTTP header used to request the url from convert. By default, the Accept-Language HTTP header of the call to the API will be used.

SIMPLE BUTTON

If your web page editor does not allow you to add javascript, a simpler button (or text link) can be used. This button will not show a progress bar. The following example shows how to add a button that converts the page the button is located on. Just paste it into your HTML editor and check it out.

 <a href="//FreeHTMLtoPDF.com/?convert"><img src="//FreeHTMLtoPDF.com/images/apiicon_32.png" /></a>

The following icons will always remain available for these buttons:

  • //FreeHTMLtoPDF.com/images/apiicon_16.png
  • //FreeHTMLtoPDF.com/images/apiicon_20.png
  • //FreeHTMLtoPDF.com/images/apiicon_24.png
  • //FreeHTMLtoPDF.com/images/apiicon_32.png
  • //FreeHTMLtoPDF.com/images/apiicon_40.png
  • //FreeHTMLtoPDF.com/images/apiicon_48.png
  • //FreeHTMLtoPDF.com/images/apiicon_64.png
  • //FreeHTMLtoPDF.com/images/apiicon_96.png
  • //FreeHTMLtoPDF.com/images/apiicon_128.png
  • //FreeHTMLtoPDF.com/images/apiicon_256.png

 

The following parameters can be used with this type of button:

ParamDefault valueDescription
convertURL from referrerThis is the url to convert. When left empty and html is also empty, the referrer is used.
enablejs1Optional. Possible values are 0 or 1. When this value is 1, javascript on the page will be executed, otherwise it won't.
widthOptional. The width of the PDF document in inches (in), millimeters (mm) or points (pt). Cannot be combined with size. Example: 210mm
heightOptional. The height of the PDF document in inches (in), millimeters (mm) or points (pt). Cannot be combined with size. Required when width is supplied. Example: 10.5in
sizeautoOptional. Size of the pages within the PDF document. Can be one of the following:
  • A3
  • A4
  • A5
  • Letter
  • Legal
  • Ledger
  • auto
When the value is auto, either A4 or Letter will be used, depending on the country the API call originates from.
Cannot be combined with width or height.
framewidthautoOptional. The size of the virtual browser frame the website is loaded in. When the value is auto, it first searches for the meta tag viewport and when that tag cannot be found, it uses 1366 as the frame width.
orientationportraitOptional. Either "landscape" or "portrait".
languageAccept-Language
header of caller
Optional. The content of the Accept-Language HTTP header used to request the url from convert. By default, the Accept-Language HTTP header of the call to the API will be used.

PROGRAMMATICALLY

The FreeHTMLtoPDF API can also be called programmatically. For instance from your PHP or ASP.NET code. This offers the following features and advantages:

  • Provide the PDF download from your program
  • Save the PDF in a database
  • Add the PDF as attachment to an email
  • Convert pieces of a web page instead of the full page by using the html and baseurl parameters

This is an example of converting your own html code to PDF and then providing it to the visitor as a PDF download using PHP:

 <?php

	$url = 'http://freehtmltopdf.com';
	$data = array(  'convert' => '', 
			'html' => '<html><head><title>My Title</title><link rel="stylesheet" type="text/css" href="relativepath.css"></head><body><h1>Web Page</h1><p>This is my content.</p></body></html>',
			'baseurl' => 'http://www.myhost.com');

	// use key 'http' even if you send the request to https://...
	$options = array(
		'http' => array(
			'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
			'method'  => 'POST',
			'content' => http_build_query($data),
		),
	);
	$context  = stream_context_create($options);
	$result = file_get_contents($url, false, $context);

	// set the pdf data as download content:
	header('Content-type: application/pdf');
	header('Content-Disposition: attachment; filename="webpage.pdf"');
	echo($result);
	
?>

The parameters you can use with this part of the API are as follows:

ParamDefault valueDescription
convertThis is the url to convert. When left empty, html and baseurl must be filled.
htmlOptional. HTML code to convert. Cannot be used when convert is filled with a URL. When using this parameter, the POST request method is recommended.
baseurlOptional. Can be supplied to resolve the relative links to images, stylesheets etc. in the html parameter.
enablejs1Optional. Possible values are 0 or 1. When this value is 1, javascript on the page will be executed, otherwise it won't.
widthOptional. The width of the PDF document in inches (in), millimeters (mm) or points (pt). Cannot be combined with size. Example: 210mm
heightOptional. The height of the PDF document in inches (in), millimeters (mm) or points (pt). Cannot be combined with size. Required when width is supplied. Example: 10.5in
sizeautoOptional. Size of the pages within the PDF document. Can be one of the following:
  • A3
  • A4
  • A5
  • Letter
  • Legal
  • Ledger
  • auto
When the value is auto, either A4 or Letter will be used, depending on the country the API call originates from.
Cannot be combined with width or height.
framewidthautoOptional. The size of the virtual browser frame the website is loaded in. When the value is auto, it first searches for the meta tag viewport and when that tag cannot be found, it uses 1366 as the frame width.
orientationportraitOptional. Either "landscape" or "portrait".
languageAccept-Language
header of caller
Optional. The content of the Accept-Language HTTP header used to request the url from convert. By default, the Accept-Language HTTP header of the call to the API will be used.