Convert HTML to PDF
without effort

Our API makes PDFs from webpages or HTML. Fast, easy and reliable!
Try it with any URL below or scroll down to the code examples.

Create perfect PDFs from webpages in your application

How to use our HTML2PDF API

Below you'll find basic examples for all major programming languages. With just a few lines of code you can convert any URL or HTML to PDF. You can find all options and examples in our API documentation and the description of the click to PDF links.


// the following code converts the URL https://example.com to PDF in PHP

$url = urlencode("https://example.com");
$license = "yourlicensekey";
                              	
// now we call the HTML2PDF API
$pdf = file_get_contents("https://api.html2pdf.co.uk/?license={$license}&url={$url}";

// the PDF is stored in the parameter $pdf now

Note - we also have a HTML2PDF PHP library available!


// the following code converts the URL https://example.com to PDF in C#/ .NET

string license = "yourlicensekey";
string url = System.Net.WebUtility.UrlEncode("https://example.com");

using (var client = new WebClient())
{
    client.QueryString.Add("license", license);
    client.QueryString.Add("url", url);
    client.DownloadFile("https://api.html2pdf.co.uk/", @"c:\temp\mypdf.pdf");
}

// the following code converts the URL https://example.com to PDF in Java

String license = "yourlicensekey";
String url = URLEncoder.encode("https://example.com",java.nio.charset.StandardCharsets.UTF_8.toString() );
File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("https://api.html2pdf.co.uk/?license=" + license + "&url=" + url);
URLConnection uc = u.openConnection();
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outs));

byte[] b = new byte[8 * 1024];
int read = 0;
while ((read = is.read(b)) > -1) {
	bout.write(b, 0, read);
}
bout.flush();
bout.close();
is.close();

// the following code converts the URL https://example.com to PDF in Python

import urllib2

data = {
   'license': 'yourlicensekey',
   'url': 'https://example.com'
}
requesturl = 'https://api.html2pdf.co.uk/?license={license}&url={url}'.format(**data)
result = urllib2.urlopen(requesturl)
localFile = open('mypdf.pdf', 'w')
localFile.write(result.read())
localFile.close()

# the following code converts the URL https://example.com to PDF in Ruby

require 'net/http'
license = 'yourlicensekey'
url = 'https://example.com'

uri = URI("https://api.html2pdf.co.uk/?license=#{license}&url=#{url}")
Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri.request_uri

  http.request request do |response|
    open 'mypdf.pdf', 'w' do |io|
      response.read_body do |chunk|
        io.write chunk
      end
    end
  end
end

# the following code converts the URL https://example.com to PDF in Perl

use File::Fetch;
use URI::Escape;

my $license = "yourlicensekey";
my $url = uri_escape("https://example.com");

my $ff = File::Fetch->new(uri => "https://api.html2pdf.co.uk/?license=$license&url=$url");
my $where = $ff->fetch( to => '/tmp');

# The following cURL command converts the URL https://example.com to a PDF with the name result.pdf

curl 	-d "license=yourlicensekey" \
	--data-urlencode "url=https://example.com" \
	-o result.pdf \
	-H "Content-Type: application/x-www-form-urlencoded" \
	-X POST https://api.html2pdf.co.uk/

# If you want to use HTML instead, then you can use the following cURL command to convert a file html.html with HTML to PDF

curl 	-d "license=yourlicensekey" \
	--data-urlencode html@html.html \
	-o result.pdf \
	-H "Content-Type: application/x-www-form-urlencoded" \
	-X POST https://api.html2pdf.co.uk/

# The following Wget command converts the URL https://example.com to a PDF with the name result.pdf

wget -O result.pdf --post-data="license=yourlicensekey&url=https://example.com" https://api.html2pdf.co.uk/

<!-- If you want to add a Click to PDF link to your pages, then just add the following link -->

<a href="javascript:location.href='https://api.html2pdf.co.uk/?url='+escape(location.href)">Click to PDF</a>

<!-- We also have a custom function that uses JavaScript or jQuery to send the contents of a webpage to us for conversion -->

<a href="#;" onclick="HTML2PDF()">Click to PDF</a>

<script>
function HTML2PDF () {
	// Replace this with your HTML2PDF license
	var license = 'yourlicensekey';
	
	// First we take the HTML of the page
	var html = '', node = document.firstChild;
	while (node) {
		switch (node.nodeType) {
			case Node.ELEMENT_NODE:
				html += node.outerHTML;
				break;
			case Node.TEXT_NODE:
				html += node.nodeValue;
				break;
			case Node.CDATA_SECTION_NODE:
				html += '<![CDATA[' + node.nodeValue + ']]>';
				break;
			case Node.COMMENT_NODE:
				html += '<!--' + node.nodeValue + '-->';
				break;
			case Node.DOCUMENT_TYPE_NODE:
				// (X)HTML documents are identified by public identifiers
				html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
				break;
		}
		node = node.nextSibling;
	}	    	

	// We pass the html and our license 
	// You don't need to pass any other parameters if your defaults in our members area are already good
	var data = { html: html, license:license};
	var serialized = Object.keys(data).map(function(k) { 
		return encodeURIComponent(k) + '=' + encodeURIComponent(data[k])
		}).join('&')
				
	// You can insert an "in progress" message here
	
	// We now prepare the API call
	xhttp = new XMLHttpRequest();
	xhttp.onreadystatechange = function() {
		var a;
		if (xhttp.readyState === 4 && xhttp.status === 200) {
			// The PDF is now generated
			// You can remove the "in progress" message here
		
			// Now we show the PDF to the user
			var filename = "example.pdf";
			if (window.navigator && window.navigator.msSaveOrOpenBlob) { 
				window.navigator.msSaveOrOpenBlob(xhttp.response, filename);
			} else { 
				a = document.createElement('a');
				a.href = window.URL.createObjectURL(xhttp.response);
				a.download = filename;
				a.style.display = 'none';
				document.body.appendChild(a);
				a.click();
			}
		}
	};
	
	// This is the actual call to our API
	xhttp.open("POST", "https://api.html2pdf.co.uk/", true);
	xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xhttp.responseType = 'blob';
	xhttp.send(serialized);
}
</script>

What will you get?

The easiest service to use and the privacy of your data guaranteed!

Easy Integration

You can set all your conversion options in a WYSIWIG members area and use our API with just a few lines of code.

Many Layout Options

Custom page sizes, header, footer and many more easy to configure settings at your disposal.

High Quality Output

PDFs that look EXACTLY like what you see on the screen and that can be used in professional settings.

Great Support

We love what we do and have been doing it since 2008. We know what you need and are happy to help!

Long History

We have been around for a long time and thousands of customers trust and have trusted us. You can too!

Stability

99.9% Uptime and the latest technology in use. You can let us worry about delivering and maintaining!

What do our customers say?

We are running HTML2PDF since 2008 and hundreds of companies have trusted our service in the past.

" We're running this without fail for years now. Excellent! "

- Julien LaCoste

" Thanks for all your help. I appreciate how kind your support is!"

- SamHomeGauge

" Thanks for the great solution and the speedy support. You're a lifesaver!"

- MinBillTrust

Get a Free Trial

If you want to try our API first, then please feel free to get a free trial!