PhantomMagick provides a simple API to ease the process of converting HTML to PDF or images

v2.0.0 2017-08-07 09:06 UTC

This package is not auto-updated.

Last update: 2022-11-26 07:31:18 UTC


For PhantomMagick version 1, please use the 1.0.2 branch!

PhantomMagick provides a simple API to ease the process of converting HTML to PDF or images. It's especially handy for things like generating invoices or capturing screenshots of websites. It's framework agnostic but it does provide a facade for simple use in Laravel 4/5.


  • Convert HTML to a PDF
  • Convert HTML to an image (PNG, JPG or GIF)
  • Support multipage PDFs
  • Capture a web page as a screenshot
  • Save PDF or image to local disk or to the cloud (S3, Dropbox or Rackspace)
  • Framework agnostic, with optional Laravel integration



PhantomMagick is available via Composer:

$ composer require anam/phantommagick


PhantomJS must be installed to use PhantomMagick.

There are few ways to install PhantomJS:

Install binary manually

You can download the official PhantomJS binary from the following link:

Install binary through Composer

Simply pull in the anam/phantomjs-linux-x86-binary package to get the up-to-date PhantomJS binary for 64-bit Linux systems.

composer require anam/phantomjs-linux-x86-binary


Laravel 4 and Laravel 5 integrations

Although PhantomMagick is framework agnostic, it does support Laravel out of the box and comes with a Service provider and Facade for easy integration.

After you have installed the PhantomMagick, open the config/app.php file which is included with Laravel and add the following lines.

In the $providers array add the following service provider.


Add the facade of this package to the $aliases array.

'Converter' => 'Anam\PhantomMagick\Facades\Converter'

You can now use this facade in place of instantiating the converter yourself in the following examples.


PDF conversion

$conv = new \Anam\PhantomMagick\Converter();
Multipage PDFs
use Anam\PhantomMagick\Converter;

$conv = new Converter();
$conv->addPage('<html><body><h1>Welcome to PhantomMagick</h1></body></html>')

Please note with multipage PDFs:

  • Only absolute paths are supported, so avoid relative paths
  • Inline styles or inline style stylesheets are recommended

Image conversion

PhantomMagick supports HTML to PNG/JPG/GIF conversion.

$conv = new \Anam\PhantomMagick\Converter();

Download file

use Anam\PhantomMagick\Converter;



To display in the browser instead of forcing the file to be download, you can pass a second parameter to the method.

$conv->download('google.pdf', true);

or just simply call:


Save to cloud

PhantomMagick leverages Flysystem to save converted files in the cloud.

PhantomMagick currently supports:

  • Amazon S3
  • Dropbox
  • Rackspace
Amazon S3

First install the required S3 dependencies through Composer.

composer require aws/aws-sdk-php
composer require league/flysystem-aws-s3-v3
use Anam\PhantomMagick\Converter;
use Aws\S3\S3Client;

$client = S3Client::factory([
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET',
    'region' => 'your-region',
    'version' => 'latest',

$conv = new Converter();
$conv->adapter($client, 'bucket-name', 'optional/path/prefix')

First install the required Dropbox dependencies through Composer.

composer require dropox/dropbox-sdk
composer require flysystem-dropbox
use Anam\PhantomMagick\Converter;
use Dropbox\Client;

$client = new Client('DROPBOX_TOKEN', 'DROPBOX_APP');

$conv = new Converter();

First install the required Rackspace dependencies through Composer.

composer require rackspace/php-opencloud
composer require league/flysystem-rackspace
use Anam\PhantomMagick\Converter;
use OpenCloud\OpenStack;
use OpenCloud\Rackspace;

$client = new OpenStack(Rackspace::US_IDENTITY_ENDPOINT, array(
    'username' => 'RACKSPACE_USERNAME',
    'password' => 'RACKSPACE_PASSWORD'

$store = $client->objectStoreService('cloudFiles', 'SYD');
$container = $store->getContainer('phantom-magick');

$conv = new Converter();


Global options


You can set the path of the phantomjs binary if you've installed it yourself manually, or the phantomjs command is not available in your shell. If you installed it through Composer (with the anam/phantomjs-linux-x86-binary package) PhantomMagick will be smart enough to find the file automatically.

Data Source

PhantomMagick only supports HTML and data can be provided via an URL or from the local disk. If you need to use raw HTML data, you can use multipage PDF conversion. However raw data does have some limitations; it does not support relative paths and it only supports inline styles and internal CSS.

new Converter('/Path/to/file/example.html');
// or
// or

For raw HTML:

$conv->addPage('<html><body><h1>Raw HTML</h1></body></html>');

PDF options


Format is optional. Supported formats are: 'A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid'.


Margin is optional and defaults to 1cm.

array('margin' => '1cm')

Orientation ('portrait', 'landscape') is optional and defaults to 'portrait'.


zoomFactor is optional and defaults to 1 (where 1 is 100% zoom).

array('zoomfactor' => 1)
Custom width and height

Custom dimension is optional. Supported formats are cm, px and in.

array('width' => '900px', height => '700px')
$options = [
  'format' => 'A4',
  'zoomfactor' => 1,
  'orientation' => 'portrait',
  'margin' => '1cm'

// or
// or

Image options


Width is optional and defaults to 1280px (720p) and only intergers are accepted.


Height is optional and only integers are accepted.


Note: If only width is given full webpage will be rendered. However, if both width and height is given the image will be clipped to the given width and height.


Quality is optional and defaults to 80. The quality must be between 1-100.



$options = [
  'width' => 1280,
  'quality' => 90

// or
// or
// or
// or



The MIT License (MIT). Please see LICENSE for more information.