Convert HTML to PDF in C#/Java/PHP/Ruby/Python/.NET/Perl etc

Our HTML to PDF API allows you to convert HTML to PDF files with great ease, power and performance. You can use it manually and in any programming language. You will get a lot of different settings to optimize your conversion and make sure the PDF looks exactly the way YOU want it.

Some of the use cases for our API are:

  • You want to generate PDF forms like proposals / invoices / timesheets etc.
  • You want to generate PDFs from data in a database and you know how to create regular web pages from the database.
  • You want to generate reports as PDF
  • You want to not only display a PDF, but also save it as a local file and then choose what you want to do with it.
  • You want to create PDFs from different sources and merge them into one bigger PDF New!
  • You want to create professional PDFs and use your own stationary as background New!

Here’s how easy it is: suppose you’d like to convert the web page http://www.google.com to PDF. You would just send the following HTTP request to get the PDF:

http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com

Note All the examples below show the examples based on HTTP GET, but you can also use HTTP POST to access our API!

If you want to see how you do this in C# / Java / PHP / Ruby / Python / VB.net or Perl then click here to go directly to the coding examples. You may also want to have a look at our Google code project page, where you can collaborate with others on code samples for our API.

We have established a network of GLOBAL servers, which allows you to choose a server close to you to do the conversion so the PDF is served to you with the lowest latency possible. Choose the API call of your choice for the data center closest to you.

Data centerAPI call
Europe (London, UK)
http://europe.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.co.uk
USA (Atlanta, USA)
http://usa.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.com
Asia (Tokio, Japan)
http://asia.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.google.co.jp

Basic function parameters

The following choices represent the basic functions you can execute with the API – you can either convert from a URL or raw HTML, or you can MERGE previous conversions together into one file.

html New!Raw HTML code that you’d like to have converted
We recommend you POST the data to our API whenever you want to use raw HTML code, because appending this to the query string has limitations to the length of the HTML code. Also – when you don’t POST the data you should make sure to encode the HTML properly.

ParameterDescription
urlURL of the web page that you’d like have converted
Note that you must properly escape the ampersands (&) if you want to pass additional query parameters (more info in our blog).
The URL must also include the whole domain including the ‘http://’
mergeNumber of PDFs that you want to merge together into one new PDF
When you merge, all other options (except encryption) are ignored and the previously generated PDFs are merged as-is into one new PDF
Note that you must have created these PDFs with our API or SDK in the previous two hours. After two hours we delete your PDFs from our cache and they can no longer be merged.

Layout parameters

By default the PDF will be created with the conversion settings you set in the members area. There you can set page format, margins, header & footer and other important settings.
We allow you to override most of these in each conversion. You can do so with the parameters in the tables below.

ParameterDescription
filenameFilename for the PDF
pagesizeSet page size like A3, A4, Letter etc. Please refer to the list of allowed page sizes
orientationSpecify portrait or landscape orientation
widthCustom width of the PDF
heightCustom height of the PDF, if you specify the width, but not the height, then we fit the whole page into a one page PDF by auto-adjusting the height of the PDF.
leftLeft margin
rightRight margin
topTop margin
bottomBottom margin
unitUnit for size & margin settings. Can be ‘mm’ (millimeters), ‘in’ (inches) or ‘pt’ (points). We’ll use mm in case you don’t specify a unit.
header_htmlHeader, declared in HTML, which you want to use for this conversion. Page numbering etc is explained in the members area.
footer_htmlFooter, declared in HTML, which you want to use for this conversion. Page numbering etc is explained in the members area.
header_urlURL of the header HTML, which you want to use for this conversion. This should be a valid URL (starting with http:// or https://) – to a location where you defined your header HTML. Page numbering etc is explained in the members area.
footer_urlURL of the footer HTML, which you want to use for this conversion. This should be a valid URL (starting with http:// or https://) to a location where you defined your footer HTML. Page numbering etc is explained in the members area.
pageoffPage offset, which is used to adjust page numbering in your header and footer. Please also have a look at your members area for more details.
cmtSet the CSS media type so you can influence better how your content is converted to PDF. We currently support the values print and screen. If you don’t specify one, we take the one you’ve set in the members area. The default there is screen.
See w3schools for more information on CSS media types.
Print will normally give you a more print-friendly layout.
popupIf you set this to y we will return a URL to the PDF on our server instead of the actual PDF.
This allows you to generate the PDF, without having to use bandwith to serve it to your visitors.
jscriptUse this parameter to render your PDF with JavaScript on or off.
jscriptmsUse this parameter to set the time in milliseconds that we take to complete JavaScript processing.

Note on measurements: in case you only specify a height then we’ll ignore it; In case you only specify a width we’ll assume you want a single page PDF. In case you specify one or more margins, but not all, we’ll assume the others to be 0.

Encryption and password protection

You can also use encryption settings to encrypt the PDF we generate and – if you want – you can protect it with a password or use rights management to control which things the user is allowed to do with the PDF. The below table lists all the options you can pass for this.

ParameterDescription
encryptionThe level of encryption for the PDF. You can choose the following values:

  • 40 for 40-bit RC4 encryption.
  • 128 for 128-bit RC4 encryption, requires Acrobat reader 5 or later
  • 128aes for 128-bit AES encryption, requires Acrobat reader 7 or later
  • 256 for 256-bit AES encryption, requires Acrobat reader X or later
ownerpassThe master password that controls all encryption & rights settings for the PDF.
userpassThe password a user needs to open the PDF.
noprintIf set, Acrobat Reader will prevent users from printing the PDF.
nocopyIf set, Acrobat Reader will prevent users from copying content from the PDF.
nomodifyIf set, Acrobat Reader will prevent users from modifying the PDF.

Watermarking and stamping your PDF

In some cases you’d like to watermark your PDF, e.g. when you want to show draft or private documents. The below table lists all the options you can pass for this.
Note that we consider stamping the same as watermarking, because if you use an opacity of 1 (fully opague) your watermark will come out like a stamp.

ParameterDescription
wmThe full URL where we can find the image you want to use as watermark. Please make sure the image is really there.
wm_xThe horizontal position where you want the watermark to start.
wm_yThe vertical position where you want the watermark to start.
wm_angleThe angle of rotation (in degrees) you want for your watermark.
wm_opacThe level of opacity you want for your watermark. This is a “real” number between 0 and 1. 0 is fully transparant and 1 is fully opague (what some call a ‘stamp’).
wm_sxThe horizontal scaling you want the watermark for your watermark. If you want it twice as wide, set this to 2.
wm_syThe vertical scaling you want for your watermark. If you want it half as high, set this to 0.5.

Stationary aka (full) page background New!

If you want your company’s PDFs to have your own stationary as background – then look no further. The API allows you to set your own stationary (or page background) for the pages of the PDF. You can use the same image for all the pages, or you can have a different image for the first page, the last page and all pages in between.
The image will be set as background, so all items you have in your HTML will appear over it. Note that non-transparent images or CSS background-color will be rendered OVER this.

As opposed to watermarks, which start (by default) in the top left corner of the CONTENT area, stationary starts at the top left corner of the full page. That’s also why we call it stationary as it resembles a real piece of paper.

ParameterDescription
bgThe full URL where we can find the image you want to use as stationary for the first page. Please make sure the image is really there.
If you don’t specify bg2 or bg3 then this image will be used for ALL your pages.
bg2The URL where we can find the image you want to use as stationary for the pages in between the first and last page.
bg3The URL where we can find the image you want to use as stationary for the last page of your PDF.
bg_xThe horizontal position where you want the stationary background to start.
bg_yThe vertical position where you want the stationary background to start.
bg_angleThe angle of rotation (in degrees) you want for your background.
bg_opacThe level of opacity you want for your background. This is a “real” number between 0 and 1. 0 is fully transparant and 1 is fully opague.
bg_sxThe horizontal scaling you want for your stationary background.
For a background that stretches the whole width, use 0. This then sets positioning and angle of rotation to 0.
bg_syThe vertical scaling you want for your stationary background.
For a background that stretches the whole height, use 0. This then sets positioning and angle of rotation to 0.

Authorization – converting content in your members area or authorization protocols

We understand that you can have content, which is only accessible to your private users, and still want to have it converted to PDF. We can surely help you with this. We first of all would recommend you to POST the data to our servers in the form of raw HTML in case you don’t want it to be accessed from outside your servers.
We can also access your content over basic HTTP authentication with the parameters below. Furthermore we can utilize any other authorization protocol to access your content as long as you give us the specifics. Please contact us for more details.

ParameterDescription
httpuserThe user name you want us to use for the authentication
httppassThe password you want us to use for the authentication.

We’ve made examples that show you how to use the HTML to PDF API for URL to PDF conversion in all major programming languages below.
If you feel we need to add examples or if you think there’s a better way of doing things, then please give us a shout out!

It is very easy to convert a URL to PDF in PHP with our API. Just get the result of our URL with the PHP function get_file_contents.

Say you want to convert http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

You can use the following code in PHP to accomplish this and save the PDF as ‘mypdf.pdf':

$apikey = 'abcde12345';
$url = 'http://www.google.com';

$result = file_get_contents("http://api.htm2pdf.co.uk/urltopdf?apikey=$apikey&url=$url");
file_put_contents('/tmp/mypdf.pdf',$result);

Additional options for the conversion

Suppose you want to set more options for the conversion, like creating a single page PDF or passing a watermark. In that case you just add the parameters that you found in the tables above.

Say you want to convert http://www.digg.com to a single page PDF with width 300mm.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&width=300&unit=mm

You can use the following code in PHP to realize this:

$apikey = 'abcde12345';
$url = 'http://www.digg.com';

$result = file_get_contents("http://api.htm2pdf.co.uk/urltopdf?apikey=$apikey&url=$url&width=300&unit=mm");
file_put_contents('/tmp/mypdf.pdf',$result);

This hopefully helps you to be on your way using the API for URL to PDF conversion in PHP.
Please let us know if you think there’s information missing or you just have a great example to share.

It is very easy to convert a URL to PDF in Java with our HTML to PDF API.

Say you want to convert http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP GET request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

You can use the following code in Java to accomplish this and get the PDF stored as ‘mypdf.pdf':

String apikey = "abcde12345";
String url = "http://www.google.com";
File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=" + apikey + "&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();

Additional options for the conversion

Suppose you want to set more options for the conversion, like creating a single page PDF or setting encryption options.
In that case you just add the parameters that you found in the tables above.

Say you want to convert http://www.digg.com to PDF and you want to set a document open password of ‘htm2pdf;.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&userpass=htm2pdf

You can use the following lines of code in Java to do this:

String apikey = "abcde12345";
String url = "http://www.digg.com";

URL u = new URL("http://api.htm2pdf.co.uk/urltopdf?apikey=" + apikey + "&url=" + url + "&userpass=htm2pdf");
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();

This hopefully helps you to be on your way using the API for URL to PDF conversion in Java.
Please let us know if you think there’s information missing or you just have a great example to share.

It is very easy to convert a URL to PDF in Ruby with our HTML to PDF API. Just get the result of our URL with the Ruby class Net::HTTP.

Say you want to convert http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

This means you would want to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

You can use the following code in Ruby to accomplish this and get the PFD saved as ‘mypdf.pdf':

require 'net/http'
apikey = 'abcde12345'
url = 'http://www.google.com'

uri = URI("http://api.htm2pdf.co.uk/urltopdf?apikey=#{apikey}&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

Additional options for the conversion

Suppose you want to set more options for the conversion, like creating a single page PDF or setting other page formatting options or something else.
In that case you just add the parameters that you found in the tables above.

Say you want to convert http://www.digg.com to a PDF that has 2 inch top and bottom margins.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you need to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&top=2&bottom=2&unit=in

You can use the following code in Ruby to realize this:

require 'net/http'
apikey = 'abcde12345'
url = 'http://www.digg.com'

uri = URI("http://api.htm2pdf.co.uk/urltopdf?apikey=#{apikey}&url=#{url}&top=2&bottom=2&unit=in")
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

This hopefully helps you to be on your way using the API for URL to PDF conversion in Ruby.
Please let us know if you think there’s information missing or you just have a great example to share.

It is very easy to convert a URL to PDF in C# with our HTML to PDF API. Just get the result of our URL with the C# WebClient method.

Say you want to convert http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

You can use the following code in C# to accomplish this and save the PDF as ‘mypdf.pdf':

string apiKey = "abcde12345";
string url = "http://www.google.com";

using (var client = new WebClient())
{
    client.QueryString.Add("apikey", apiKey);
    client.QueryString.Add("url", url);
    client.DownloadFile("http://api.htm2pdf.co.uk/urltopdf", @"c:\temp\mypdf.pdf");
}

Additional options for the conversion

Suppose you want to set more options for the conversion, like creating a single page PDF or setting encryption options.
In that case you just add the parameters that you found in the tables above.

So let’s say you would like to convert http://www.digg.com to a single PDF with a widht of 8 inches.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request followed by a redirect:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&width=8&unit=in

You can use the following code in C# to realize this:

string apiKey = "abcde12345";
string url = "http://www.digg.com";

using (var client = new WebClient())
{
    client.QueryString.Add("apikey", apiKey);
    client.QueryString.Add("url", url);
    client.QueryString.Add("width", "8");
    client.QueryString.Add("unit", "in");
    client.DownloadFile("http://api.htm2pdf.co.uk/urltopdf", @"c:\temp\mypdf.pdf");
}

This hopefully helps you to be on your way using the API for URL to PDF conversion in C#.
Please let us know if you think there’s information missing or you just have a great example to share.

It is very easy to convert a URL to PDF in VB.net with our HTML to PDF API. Just get the result of our URL with the VB.net WebClient.

Let’s say as an example you want to convert http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

You would then just send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

And you would use the following code in VB.net to accomplish this and save the file as ‘mypdf.pdf':

Dim apiKey As String = "abcde12345"
Dim url As String = "http://www.google.com"

Using client As New WebClient
    client.QueryString.Add("apikey", apiKey)
    client.QueryString.Add("url", Url)
    client.DownloadFile("http://api.htm2pdf.co.uk/urltopdf", "c:\temp\mypdf.pdf")
End Using

Additional options for the conversion

Suppose you want to set more options for the conversion, like setting a different page format or using encryption options.
In that case you just add the parameters that you found in the tables above.

Say you want to convert http://www.digg.com to PDF and encrypt the PDF with 256-bit AES and a password of ‘htm2pdf’.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request and redirect the script to the URL in the result:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&encryption=256&userpass=htm2pdf

You can use the following code in VB.net to realize this:

Dim apiKey As String = "abcde12345"
Dim url As String = "http://www.digg.com"

Using client As New WebClient
    client.QueryString.Add("apikey", apiKey)
    client.QueryString.Add("url", Url)
    client.QueryString.Add("encryption", "256")
    client.QueryString.Add("userpass", "htm2pdf")
    client.DownloadFile("http://api.htm2pdf.co.uk/urltopdf", "c:\temp\mypdf.pdf")
End Using

This hopefully helps you to be on your way using the API for URL to PDF conversion in VB.net.
Please let us know if you think there’s information missing or you just have a great example to share.

It is very easy to convert a URL to PDF in Python with our HTML to PDF API. Just get the result of our URL with the Python function urllib2.urlopen.

Suppose you’re interested in converting http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

In that case you’d need to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

You could use the following code in Python to accomplish this and get your PDF saved as ‘mypdf.pdf':

import urllib2

data = {
   'apikey': 'abcde12345',
   'url': 'http://www.google.com'
}
requesturl = 'http://api.htm2pdf.co.uk/urltopdf?apikey={apikey}&url={url}'.format(**data)
result = urllib2.urlopen(requesturl)
localFile = open('mypdf.pdf', 'w')
localFile.write(result.read())
localFile.close()

Additional options for the conversion

Suppose you want to set more options for the conversion, like setting a different page format or using watermarking options.
In that case you just add the parameters that you found in the tables above.

Say you want to convert http://www.digg.com to PDF and override all margins to 0.
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request and redirect the script to the URL in the result:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&top=0&bottom=0&left=0&right=0

You can use the following code in Python to realize this:

import urllib2

data = {
   'apikey': 'abcde12345',
   'url': 'http://www.digg.com'
}
requesturl = 'http://api.htm2pdf.co.uk/urltopdf?apikey={apikey}&url={url}&top=0&bottom=0&left=0&right=0'.format(**data)
result = urllib2.urlopen(requesturl)
localFile = open('mypdf.pdf', 'w')
localFile.write(result.read())
localFile.close()

This hopefully helps you to be on your way using the API for URL to PDF conversion in Python.
Please let us know if you think there’s information missing or you just have a great example to share.

It is very easy to convert a URL to PDF in Perl with our HTML to PDF API. You can do it with several functions in Perl, like the File::Fetch or the LWP::Simple module.

Let’s just say you’d like to convert http://www.google.com to PDF.
You’ve subscribed to our service and your API key is abcde12345.

With our API you’d need to send the following HTTP request:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.google.com

You can use the following code in Perl to accomplish this and get your PDF saved as ‘mypdf.pdf':

use File::Fetch;

my $apikey = 'abcde12345';
my $url = "http://www.google.com";

my $ff = File::Fetch->new(uri => "http://api.htm2pdf.co.uk/urltopdf?apikey=$apikey&url=$url");
my $where = $ff->fetch( to => '/tmp/mypdf.pdf');

Additional options for the conversion

Suppose you want to set more options for the conversion, like setting a different page format or using encryption options.
In that case you just add the parameters that you found in the tables above.

Say you want to convert http://www.digg.com to one long single page PDF of 20cm wide (=200mm).
You’ve subscribed to our service and your API key is abcde12345.

Therefore you want to send the following HTTP request and redirect the script to the URL in the result:

http://api.htm2pdf.co.uk/urltopdf?apikey=abcde12345&url=http://www.digg.com&width=200&unit=mm

You can use the following code in Perl to realize this:

use File::Fetch;

my $apikey = 'abcde12345';
my $url = "http://www.google.com";

my $ff = File::Fetch->new(uri => "http://api.htm2pdf.co.uk/urltopdf?apikey=$apikey&url=$url&width=200&unit=mm");
my $where = $ff->fetch( to => '/tmp/mypdf.pdf');

This hopefully helps you to be on your way using the API for URL to PDF conversion in Perl.
Please let us know if you think there’s information missing or you just have a great example to share.

You can also use the API to convert URLs manually if you want to. This really could not be easier.

Just type a URL like the following URL in your browser’s address and we’ll give you the converted file back for downloading!

http://api.htm2pdf.co.uk/urltopdf?apikey=yourapikey&url=http://www.example.com

This hopefully helps you to be on your way using the HTML to PDF API for HTML to PDF conversion.
Please let us know if you think there’s information missing or you just have a great example to share.

We have subscriptions for any number of sites and for low and high-traffic sites. If you want to try our service, Sign up!


Start right now! Sign up →