Posted by & filed under HTML to PDF SDK.

This post is intended to be a ‘Quick Start’ for people who want to use our HTML to PDF SDK and are looking for some examples to start with. In our documentation you will also find quite some examples and when you download the library you will notice that it contains a file with example code as well. So hopefully this post is not superfluous.

Our SDK is currently only available in PHP. It consists of a development library file, that you need to include in each PHP file that you want to use our conversion service with. We also recommend that you use a “Try / Catch” block so you can properly catch exceptions, should they occur. It will be very useful in case things go wrong.

Basic usage

The basic invocation of the HTML to PDF SDK is as follows. You first include the SDK library file (currently version 1.4) and then you use a Try / Catch block over the invocation of the service.

require 'htm2pdfapi.php';

Try {
 $pdf = new Htm2PdfApi ($userid, $apikey);
 ....
} catch Exception($error) {
 // do something with $error->getMessage() and/or $error->getCode()
}

Of course you should replace $userid and $apikey with the User ID and API key you receive from us when you register.

So far that’s quite simple, right? Now let’s look at some real examples.

Converting a URL to PDF

Let’s take an example where we would like to convert a URL (say http://www.bbc.co.uk) to PDF and let’s add in some custom settings like page size, margins and orientation.

You’d get something like this:

require 'htm2pdfapi.php';

Try {
 $pdf = new Htm2PdfApi ($userid, $apikey);    // initialize the API

 $pdf->SetPageSize('Letter');                 // this PDF will have page size 'Letter'
 $pdf->SetPageOrientation('Portrait');        // we set the orientation to 'Portrait'
 $pdf->SetMargins(5,5,5,5,'mm');              // we want 5mm margins all around

 $pdf->CreateFromURL('http://www.bbc.co.uk'); // now execute the conversion

 $pdf->Display();                             // display the PDF as attachment
} catch Exception($error) {
 // do something with $error->getMessage() and/or $error->getCode()
}

Converting raw HTML to PDF

Now you could also just convert raw HTML code to a PDF with the SDK. This is one of the four conversion methods (the others are converting a URL to PDF, converting a file to PDF and converting a custom form to PDF).

Let’s add in some custom watermarking settings as well on top of regular formatting options like page size, margins and orientation.

You’d get something like this:

require 'htm2pdfapi.php';

Try {
 $pdf = new Htm2PdfApi ($userid, $apikey);    // initialize the API

 $pdf->SetPageSize('A4');                     // this PDF will have page size 'A4'
 $pdf->SetPageOrientation('Landscape');       // we set the orientation to 'Landscape'
 $pdf->SetMargins(1,1,1,1,'in');              // we want 1 inch margins all around

 $pdf->SetWatermarkLocation(100,100);         // Let's put a watermark at (100,100)
                                              // It will say 'DRAFT' in red Tahoma
 $pdf->SetWatermarkText('DRAFT','Tahoma','#FF0000', 64);

 $pdf->CreateFromHTML('<html>...</html>');    // now execute the conversion

 $pdf->DisplayInline();                       // display the PDF inline in the browser
} catch Exception($error) {
 // do something with $error->getMessage() and/or $error->getCode()
}

Now these are just the most basic of examples that we just showed. The SDK can do much more, so if you’re really ready to get down with it you should absolutely check out the full documentation. And don’t forget – just sign up and get all the benefits now!

Leave a Reply

You must be logged in to post a comment.