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. You can also use our API on the command line with for example cURL or Wget.

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:

https://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("https://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 = URLEncoder.encode("http://www.example.com",java.nio.charset.StandardCharsets.UTF_8.toString() );
File outs = new File("C:\\temp\mypdf.pdf");

URL u = new URL("https://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("https://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 = System.Net.WebUtility.UrlEncode("http://www.example.com");

using (var client = new WebClient())
{
    client.QueryString.Add("license", license);
    client.QueryString.Add("url", url);
    client.DownloadFile("https://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 = System.Net.WebUtility.UrlEncode("http://www.example.com")

Using client As New WebClient
    client.QueryString.Add("license", license)
    client.QueryString.Add("url", Url)
    client.DownloadFile("https://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 = 'https://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.
The following example takes a webpage and saves it as PDF in the /tmp directory under the filename stored in $where.

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

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

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

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

When you use JavaScript you should realize that your license key will be visible to everyone. In the members area we have a setting that enables you to use the API with the domain instead so that this doesn't happen. This setting is activated by default when you sign up and we'll assume that you've set the domain in the below example.

The below example opens a PDF created from a URL in a new tab. We also have another more detailed example that shows how you can use JavaScript to convert pages in protected members areas or on your intranet.

function pdfmyurl (url, savepdf) {
       var self = this;
       
       self.save = savepdf;
       self.req = new XMLHttpRequest();
 
       // you can add other parameters here - otherwise the defaults from the members area are used
       var data = "url=" + encodeURIComponent(url);
  
       self.req.onload = function(event) {
            self.reader = new FileReader();
              
            self.reader.addEventListener("loadend", function() {
                  window.open(self.reader.result, "_blank");
                  return self.reader.result;
            });
              
            self.reader.readAsDataURL(self.req.response);
       };
  
       self.req.open("POST", "https://pdfmyurl.com/api", true);
       self.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
       self.req.responseType = "blob";
       self.req.send(data);
}

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.