PHPackages                             anam/phantommagick - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Payment Processing](/categories/payments)
4. /
5. anam/phantommagick

ActiveLibrary[Payment Processing](/categories/payments)

anam/phantommagick
==================

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

v2.0.0(8y ago)161456.4k↓16.7%45[32 issues](https://github.com/anam-hossain/phantommagick/issues)[4 PRs](https://github.com/anam-hossain/phantommagick/pulls)2MITPHPPHP &gt;=5.4.0

Since Mar 28Pushed 2y ago12 watchersCompare

[ Source](https://github.com/anam-hossain/phantommagick)[ Packagist](https://packagist.org/packages/anam/phantommagick)[ Docs](https://github.com/anam-hossain/phantommagick)[ RSS](/packages/anam-phantommagick/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (8)Used By (2)

PhantomMagick
=============

[](#phantommagick)

### For PhantomMagick version 1, please use the [1.0.2 branch](https://github.com/anam-hossain/phantommagick/tree/1.0.2)!

[](#for-phantommagick-version-1-please-use-the-102-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.

Features
--------

[](#features)

- 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

Requirements
------------

[](#requirements)

- PHP 5.5+
- [PhantomJS](http://phantomjs.org)

Installation
------------

[](#installation)

PhantomMagick is available via Composer:

```
$ composer require anam/phantommagick
```

Dependencies
------------

[](#dependencies)

[PhantomJS](http://phantomjs.org/download.html) must be installed to use PhantomMagick.

There are few ways to install PhantomJS:

##### Install binary manually

[](#install-binary-manually)

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

.

##### Install binary through Composer

[](#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
```

Integrations
------------

[](#integrations)

##### Laravel 4 and Laravel 5 integrations

[](#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.

```
'Anam\PhantomMagick\ConverterServiceProvider'
```

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.

Usage
-----

[](#usage)

### PDF conversion

[](#pdf-conversion)

```
$conv = new \Anam\PhantomMagick\Converter();
$conv->source('http://google.com')
    ->toPdf()
    ->save('/your/destination/path/google.pdf');
```

##### Multipage PDFs

[](#multipage-pdfs)

```
use Anam\PhantomMagick\Converter;

$conv = new Converter();
$conv->addPage('Welcome to PhantomMagick')
    ->addPage('http://facebook.com')
    ->addPage('/html/file/from/local/drive/example.html')
    ->save('/your/destination/path/multipage.pdf');
```

Please note with multipage PDFs:

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

### Image conversion

[](#image-conversion)

PhantomMagick supports HTML to PNG/JPG/GIF conversion.

```
$conv = new \Anam\PhantomMagick\Converter();
$conv->source('http://google.com')
    ->toPng()
    ->save('/your/destination/path/google.png');
```

###### HTML to PNG

[](#html-to-png)

```
$conv->toPng()
```

###### HTML to JPG

[](#html-to-jpg)

```
$conv->toJpg()
```

###### HTML to GIF

[](#html-to-gif)

```
$conv->toGif()
```

### Download file

[](#download-file)

```
use Anam\PhantomMagick\Converter;

Converter::make('http://google.com')
    ->toPdf()
    ->download('google.pdf');

Converter::make('http://yahoo.com')
    ->toPng()
    ->download('yahoo.png');
```

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:

```
$conv->serve();
```

Save to cloud
-------------

[](#save-to-cloud)

PhantomMagick leverages [Flysystem](http://flysystem.thephpleague.com) to save converted files in the cloud.

PhantomMagick currently supports:

- Amazon S3
- Dropbox
- Rackspace

##### Amazon S3

[](#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')
    ->acl('public')
    ->source('http://google.com')
    ->toPdf()
    ->save('google.pdf');
```

##### Dropbox

[](#dropbox)

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();
$conv->adapter($client)
    ->source('https://google.com')
    ->toPdf()
    ->save('dropbox_example.pdf');
```

##### Rackspace

[](#rackspace)

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();
$conv->adapter($container)
    ->source('https://google.com')
    ->toPdf()
    ->save('rackspace_example.pdf');
```

### Settings

[](#settings)

#### Global options

[](#global-options)

###### Binary

[](#binary)

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.

```
$conv->setBinary('/phantomjs/binary/path/phantomjs');
```

###### Data Source

[](#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
Converter::make('/Path/to/file/example.html');
//or
$conv->source('/Path/to/file/example.html');
// or
$conv->source('http://google.com');
```

For raw HTML:

```
$conv->addPage('Raw HTML');
```

#### PDF options

[](#pdf-options)

###### Format

[](#format)

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

```
$conv->format('A4');
```

###### Margin

[](#margin)

Margin is optional and defaults to 1cm.

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

###### Orientation

[](#orientation)

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

```
$conv->portrait();
$conv->landscape();
```

###### zoomFactor

[](#zoomfactor)

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

```
array('zoomfactor' => 1)
```

###### Custom width and height

[](#custom-width-and-height)

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

```
array('width' => '900px', height => '700px')
```

##### Example

[](#example)

```
$options = [
  'format' => 'A4',
  'zoomfactor' => 1,
  'orientation' => 'portrait',
  'margin' => '1cm'
];

$conv->setPdfOptions($options);
// or
$conv->pdfOptions($options);
// or
$conv->toPdf($options);
```

#### Image options

[](#image-options)

###### Width

[](#width)

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

```
$conv->width(1280);
```

###### Height

[](#height)

Height is optional and only integers are accepted.

```
$conv->height(1280);
```

**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

[](#quality)

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

```
$conv->quality(90);
```

\#####Example

```
$options = [
  'width' => 1280,
  'quality' => 90
];

$conv->setImageOptions($options);
// or
$conv->imageOptions($options);
// or
$conv->toPng($options);
// or
$conv->toJpg($options);
// or
$conv->toGif($options);
```

Credits
-------

[](#credits)

- [Anam Hossain](https://github.com/anam-hossain)
- [All Contributors](https://github.com/anam-hossain/phantommagick/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](http://opensource.org/licenses/MIT) for more information.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity53

Moderate usage in the ecosystem

Community24

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 98.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~172 days

Recently: every ~214 days

Total

6

Last Release

3206d ago

Major Versions

1.0.2.x-dev → v2.0.02017-08-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/b25d92dbbd1d034062645eb371dcc2b04a5f686cedfde6ad7cde87f76ad9dcbf?d=identicon)[anam](/maintainers/anam)

---

Top Contributors

[![anam-hossain](https://avatars.githubusercontent.com/u/5017268?v=4)](https://github.com/anam-hossain "anam-hossain (118 commits)")[![alanhr](https://avatars.githubusercontent.com/u/12049854?v=4)](https://github.com/alanhr "alanhr (1 commits)")[![dwightwatson](https://avatars.githubusercontent.com/u/1100408?v=4)](https://github.com/dwightwatson "dwightwatson (1 commits)")

---

Tags

laravelpdfconverterinvoiceLaravel pdfphantomjshtml-to-pdfpdf converterhtml to imageHtml 2 pdfHtml 2 jpghtml to pngScreen capturePhantomimage converter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/anam-phantommagick/health.svg)

```
[![Health](https://phpackages.com/badges/anam-phantommagick/health.svg)](https://phpackages.com/packages/anam-phantommagick)
```

###  Alternatives

[laraveldaily/laravel-invoices

Missing invoices for Laravel

1.5k1.3M4](/packages/laraveldaily-laravel-invoices)[torgodly/html2media

Html2Media is a versatile Laravel package that allows users to convert HTML content into high-quality PDFs with options for either downloading or triggering a print dialog. Ideal for generating documents, invoices, and reports, this package includes configurable settings for file name, page orientation, format, margins, and scale. Html2Media also provides seamless integration with Filament actions, enabling dynamic content rendering in modals and customizable output previews. Whether you need to save a PDF or send it directly to the printer, Html2Media simplifies the process with robust, flexible features.

4532.5k1](/packages/torgodly-html2media)[cangelis/pdf

Yet another HTML to PDF Converter based on wkhtmltopdf

5363.7k1](/packages/cangelis-pdf)[wemersonjanuario/laravelpdf

Another HTML to PDF Converter for Laravel

2912.9k1](/packages/wemersonjanuario-laravelpdf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
