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. You should have your first example working within 5 minutes!
You can download the library from our website at https://pdfmyurl.com/pdfmyurl.zip or install it via composer.
If you're using composer / Packagist in your project then you can install our PHP library as follows.
$ composer require pdfmyurl/pdfmyurl
The install will include an example.php file with a basic example as well as the library itself. Since namespacing is a bit different when you use composer, a basic URL to PDF conversion example would be as follows.
<?php require 'vendor/autoload.php'; use pdfmyurl\pdfmyurl\PDFmyURL; // first fill in the license that you received upon sign-up $pdf = new PDFmyURL ('yourlicensekey'); // now set the options, so for example when we want to have a page in A4 in orientation portrait // you can also set these in our members area with a Wysiwig form by the way $pdf->SetPageSize('A4'); $pdf->SetPageOrientation('Portrait'); // then do the conversion - this is how you convert Google to PDF and display the PDF to the user $pdf->CreateFromURL ('www.google.com'); $pdf->Display(); // if you'd like to convert raw HTML instead you would have done this // $pdf->CreateFromHTML('<html><body>Hello World!</body></html>'); // $pdf->Display();
If you don't use composer, you can simply download the PHP library and include it in your project. You can then use our API in PHP as in the following example:
<?php require 'pdfmyurl.php'; $pdf = new PDFmyURL ('yourlicensekey'); // initialize the class with your license $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:
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.
|SetPageSize||$pagesize||set the page size to one of the standard page formats like A4, B0, Letter etc|
|SetPageOrientation||$orientation||set the orientation to either portrait or landscape|
|SetPageDimensions||$width, $height||set the page size to exact dimensions or force a single page PDF|
|SetDimensionUnit||$unit||set the unit of measure for custom dimensions to mm, cm, in or px|
|SetMargins||$top,bottom,$left,$right||set the margins|
|SetViewport||$width, $height||set the viewport, which by default is 1366 by 768|
Below are some examples that show how to use these functions.
require 'pdfmyurl.php'; // include the class $pdf = new PDFmyURL ('yourlicensekey'); // initialize the class with your license $pdf->SetPageSize('A4'); // Set page format to A4 $pdf->SetPageOrientation('Portrait'); // Set page orientation to 'Portrait' $pdf->SetDimensionUnit('mm'); // Set unit of measurement to millimeter $pdf->SetPageDimensions(100, 200); // Set the page to be 100mm wide and 200mm high $pdf->SetPageDimensions(300, 0); // Set the page to be 300mm wide and force a single long page $pdf->SetMargins(10,10,5,5); // Set the top & bottom margin to 10mm and the left & right margin to 5mm $pdf->SetViewport(800,600); // Use a viewport of 800*600 for rendering