Use Rotativa.io to create PDF files in PHP

date Jun 15, 2016

You can now use Rotativa.io from PHP. We have developed a PHP helper class that you can download from here.

This helper class is going to take advantage of the Rotativa.io system and provide a way to create PDF files from you PHP app with an API call without the need to use absolute URL in web page components, such as images, css files, fonts, javascript files.

Using Rotativa.io you can achieve quicker PDF creation times since web page assets can be transferred to the API with a single call.

This PHP helper class is still in early stages of development but we are going to work to quickly put it on par with the features available with the .Net library.

Usage is very simple:

1: Instantiate a Rotativa.io object. You’ll need to sign up at Rotativa.io. After signing in you can get your ApiKey and choose for one of our 4 endpoints (Us West, Us East, Europe, Asia).

$rhq = new Htm2PdfApi(< YOUR ENDPOINT >, < YOUR APIKEY >);

2: Set options if needed

// use this if you want to set the file name the user will download
$rhq->SetFilename('my-report.pdf');     

// values can be 'Landscape' or 'Portrait' (default is 'Portrait')
$rhq->SetPageOrientation('Landscape');  

// values can be A4, B0, Letter etc
$rhq->SetPageSize(...);                 

// set the page width
$rhq->SetPageWidth(...);                

// set the page height
$rhq->SetPageHeight(...);               

// set the margins
$rhq->SetPageMargins(....);             

// set other wkhtmltopdf switches 
// docs at http://wkhtmltopdf.org/usage/wkhtmltopdf.txt
$rhq->SetCustomSwithes(...);            

3: Load and/or build your html (no need to use absolute URL for images, css, fonts, etc …)

$html = '<html><body><h1>Report</h1><p><img src="/img/logo.png" /></p>...';

// or

$html = file_get_contents('../pdf/report1.html');

// or build with your favorite template engine.

4: Get the user to download the PDF from a private and secure cloud URL

$rhq->DisplayPDF($html);

If you need different functionality or need to report a bug, please write an email to support@rotativahq.com

Any feedback is appreciated.