PHP library for webpage or HTML to PDF conversion

We have created a PHP library that can handle the full functionality of our API. It's a class that lets you use all the options and functionality easily and has built in exception handling.

After you download the PHP library you can use our API in PHP as in the following example:

require 'pdfmyurl.php';                      // include the class

$pdf = new PDFmyURL ('yourlicensekey');      // initialize the class with your license
	
                                             // Now set some options, for example:
$pdf->SetPageSize('A4');                     // Set page format to A4
$pdf->SetPageOrientation('Portrait');        // Set page orientation to 'Portrait'
	
$pdf->CreateFromURL ('www.google.com');      // Convert the Google Homepage to PDF
$pdf->Display();                             // Display the PDF for download to the user

Below is a list of functions that you can use, as well as all the exceptions that you may encounter when you're using the library. This will help you quickly take full advantage of our webpage / HTML to PDF API.

You will use the functions of the library in the following sequence:

  1. Set options such as page size, margins, headers etc.
  2. Execute the conversion from a URL or HTML
  3. Display or save the PDF

Functions for setting options

Below is a list of all functions that you can use to set options for the conversion, the page layout and dimensions, margins, headers and footers, watermarking and much more.

The following functions allow you to customize the page dimensions and margins of your PDFs.

FunctionDescription
SetPageSizeset the page size to one of the standard page formats like A4, B0, Letter etc
SetPageOrientationset the orientation to either portrait or landscape
SetPageDimensionsset the page size to exact dimensions or force a single page PDF
SetDimensionUnitset the unit of measure for custom dimensions to mm, in or pt
SetMarginsset the margins
SetViewportset the viewport, which by default is 1024 by 768

The following functions allow you to customize the headers and footers of your PDFs.

FunctionDescription
SetHeaderDefine the header of the PDF in HTML
SetFooterDefine the footer of the PDF in HTML
SetPageOffsetDefine the offset for page numbering

The following functions allow you to show or hide specific parts of your webpage for the conversion. See the documentation on partial page conversion for more info.

FunctionDescription
ShowContentShow only content with this/these DIV id(s)
HideContentHide all content with this/these DIV id(s)

The following functions allow you to put a watermark over your PDFs or underneath your content, in which case we call it 'stationary'.

FunctionDescription
SetWatermarkLocationSet the position of the watermark
SetWatermarkTransformationSet rotation, opacity and scaling of the watermark
SetWatermarkImageDefine the URL where the watermark can be found
SetWatermarkTextDefine the text and font characteristics of the watermark
SetStationaryLocationSet the postion of the stationary background
SetStationaryTransformationSet rotation, opacity and scaling of the stationary - set scaling to 0 for FULL BACKGROUND DISPLAY
SetStationaryImagesDefine the URL of the stationary images for first page, next pages and last page

The following functions allow you to protect your PDFs with passwords and against printing, content copying and annotation.

FunctionDescription
SetEncryptionLevelSet the encryption level for the PDF
SetPasswordsSet the owner password and the 'document open' password
SetPermissionsSet the permissions on the PDF for everyone except the owner

The following functions allow you to define the way we can access your URLs in secure member areas.

FunctionDescription
SetHttpAuthenticationSet user name and password for basic HTTP Authentication
SetFormAuthenticationSet parameters for login authentication via a form
SetSessionIDSet a cookie with the JSESSIONID
SetCookieJarSet the contents of a cookie jar
SetNetscapeCookieJarSet the contents of a cookie jar in the old Netscape format

The following functions allow you to set various other options to control the conversion.

FunctionDescription
SetGrayscaleforce a PDF in grayscale
NoJavaScriptdisable JavaScript
SetJavaScriptDelayset the amount of time in msec that JavaScript waits to complete
Bookmarksadd bookmarks to the PDF based on the <h1> - <h4> tags in the webpage
NoImagesdisable images
NoBackgrounddisable the background
NoInternalLinksdisable links within the domain
NoExternalLinksdisable links to external sites
SetCssMediaTypeset CSS media type to print or screen
SetTitleset the title of the PDF
SetZoomFactorset the zoom factor of the PDF

Functions for conversion from a URL or HTML

The PHP library lets you convert a URL to PDF or raw HTML to PDF with the functions listed below.

FunctionDescription
CreateFromURLConvert a URL to PDF
CreateFromHTMLConvert HTML to PDF

Functions for saving / displaying the PDF

When the conversion is done you can choose to display the PDF to a user or save it off to a location of your choice with one of the functions below.

FunctionDescription
DisplayDisplay the PDF as attachment in the user's browser
DisplayInlineDisplay the PDF inline in the user's browser
SaveSave the PDF as a local file
StringValueReturn the PDF as a string value

Return codes and exception handling

The PHP library throws exceptions, which you can and should check to see if the execution was succesfull or not. You can do so with the following try / catch code.

Try {
   $pdf = new PDFmyURL ($license);
   $pdf->CreateFromURL('http://www.example.com');
   $pdf->Display();
} catch (Exception $error) {
   // handle the exception, for example like this:
   echo $error->getMessage();
   echo $error->getCode();
}

The following is the list of codes we use.

CodeDescription
400 Bad RequestYou didn't specify anything to convert
401 Authorization requiredYou specified an invalid license key
429 Too Many RequestsYou have overrun a usage limit for your plan
503 Service unavailableYou are sending multiple requests at the same time to the API from the same IP address
8xx cURL errorcURL is trying to connect to our server, but there's a problem on your side with the cURL function.
Please check the exact 8xx code for more information.
900 URL is invalidYou should pass a valid URL. URLs can start with http:// or https:// or have no prefix like www.google.com.
901 HTML string can not be emptyYou must pass HTML code. Therefore do not try to pass an empty string.
905 Watermark not validYou must pass a URL that points to an image if you want to use an image as watermark.
Check if your URL really points to your watermark image.
906 Watermark text can not be emptyYou must pass a real text if you want to use a text watermark.
Therefore do not try to pass an empty string.
907 Stationary not validYou must pass valid URLs that point to an image if you want to use one or more images as stationary.
Check if your URLs really point to your stationary images.
910 Filename does not existThe file you're trying to send does not exist as the filename you specified.
Please make sure to include the full path in your filename.
911 File can not be emptyThe file you're trying to send does not contain any data.
920 Not possible to save PDF fileYou're most likely out of space on your system and are not able to save the file locally.
930 cURL is not installedYou need cURL on your system to be able to work with the PHP library.
Please follow the installation instructions from PHP.net.
940 Access errorBy default we use your system's temp directory to save the PDF you receive from us.
If you web user does not have access to that directory you should either change that user's rights or use the function SetTempDirectory to change the directory we need to use.
990 We could not load this pageThere's a problem with the URL you're trying to convert.
It may not be available currently or it may contain incorrect client side scripting, flash or other things.
Detailed analysis is required of the page at hand.