HTML to PDF API - coding examples

We've made basic examples that show you how to use the PDFmyURL API in all major programming languages. These examples are specifically intended to get you coding in a few minutes without the need to install anything.

If you would rather install a library to code with, we offer a professional PHP library and a .NET component. We can offer libraries in other languages on request.

Basic Example

Our API is very easy to use. It takes a license and either a URL or raw HTML as input and returns a PDF.
You just send a request similar to this, which we will be using in all our examples and which converts http://www.example.com to PDF:

http://pdfmyurl.com/api?license=yourlicensekey&url=http://www.example.com

You can use a lot of additional parameters in the API call, which give you access to all the features for conversion. These are all specified in the documentation.

NB: You can use both GET as well as POST requests as long as you properly URL encode the data that you pass.

You can implement the PDFmyURL API with the file_get_contents function in PHP. Sending the example request and storing the PDF in a local file thus becomes the following code.

$license = 'yourlicensekey';
$url = urlencode('http://www.example.com');

$result = file_get_contents("http://pdfmyurl.com/api?license=$license&url=$url");
file_put_contents('/tmp/mypdf.pdf',$result);

Of course you can use many options, by referring to the documentation. Also we offer a complete PHP library based on our API.

You can use the following code in Java to send the basic example and get the PDF stored as 'mypdf.pdf':

String license = "yourlicensekey";
String url = "http://www.example.com";
File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("http://pdfmyurl.com/api?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();

Of course you can use many options, by referring to the documentation.

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

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

uri = URI("http://pdfmyurl.com/api?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

Of course you can use many options, by referring to the documentation.

Converting a URL to PDF in C# is easy with the PDFmyURL API. Just use the C# WebClient method as in the example below. Of course you can also use our professional .NET component.

string license = "yourlicensekey";
string url = "http://www.example.com";

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

Of course you can use many options, by referring to the documentation.

It is easy to convert a URL into PDF in VB.net with our API. Just use the VB.net WebClient. Of course you can also use our custom made .NET component.

Dim license As String = "yourlicensekey"
Dim url As String = "http://www.example.com"

Using client As New WebClient
    client.QueryString.Add("license", license)
    client.QueryString.Add("url", Url)
    client.DownloadFile("http://pdfmyurl.com/api", "c:\temp\mypdf.pdf")
End Using

Of course you can use many options, by referring to the documentation.

You can use the Python function urllib2.urlopen to make use of our API and convert URLs or HTML to PDF.

import urllib2

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

Of course you can use many options, by referring to the documentation.

You can use several modules in Perl to use our API, such as the File::Fetch or the LWP::Simple module.

use File::Fetch;

my $license = 'yourlicensekey';
my $url = "http://www.example.com";

my $ff = File::Fetch->new(uri => "http://pdfmyurl.com/api?license=$license&url=$url");
my $where = $ff->fetch( to => '/tmp/mypdf.pdf');

Of course you can use many options, by referring to the documentation.

The API can also be used in any other programming language or from the command line. If you'd like to see examples in other languages please contact us about this.