Repository: anam-hossain/phantommagick Branch: master Commit: 7da1cb921987 Files: 18 Total size: 49.4 KB Directory structure: gitextract_exfyuc5j/ ├── .gitignore ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src/ │ ├── Adapter.php │ ├── Converter.php │ ├── ConverterServiceProvider.php │ ├── Exception/ │ │ ├── FileFormatNotSupportedException.php │ │ └── FileNotFoundException.php │ ├── Facades/ │ │ └── Converter.php │ ├── Runner.php │ ├── Str.php │ └── scripts/ │ └── phantom_magick.js └── tests/ ├── AdapterTest.php ├── ConverterTest.php ├── PrivateAndProtectedMethodsAccessibleTrait.php ├── RunnerTest.php └── test_page.html ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .editorconfig .env.php /build /vendor /bin ================================================ FILE: README.md ================================================ # PhantomMagick ### For PhantomMagick version 1, please use the [1.0.2 branch](https://github.com/anam-hossain/phantommagick/tree/1.0.2)! 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 - 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 - PHP 5.5+ - [PhantomJS](http://phantomjs.org) ## Installation PhantomMagick is available via Composer: ```bash $ composer require anam/phantommagick ``` ## Dependencies [PhantomJS](http://phantomjs.org/download.html) 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: [http://phantomjs.org/download.html](http://phantomjs.org/download.html). ##### 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. ```bash composer require anam/phantomjs-linux-x86-binary ``` ## 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. ```php 'Anam\PhantomMagick\ConverterServiceProvider' ``` Add the facade of this package to the `$aliases` array. ```php 'Converter' => 'Anam\PhantomMagick\Facades\Converter' ``` You can now use this facade in place of instantiating the converter yourself in the following examples. ## Usage ### PDF conversion ```php $conv = new \Anam\PhantomMagick\Converter(); $conv->source('http://google.com') ->toPdf() ->save('/your/destination/path/google.pdf'); ``` ##### Multipage PDFs ```php 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 PhantomMagick supports HTML to PNG/JPG/GIF conversion. ```php $conv = new \Anam\PhantomMagick\Converter(); $conv->source('http://google.com') ->toPng() ->save('/your/destination/path/google.png'); ``` ###### HTML to PNG ```php $conv->toPng() ``` ###### HTML to JPG ```php $conv->toJpg() ``` ###### HTML to GIF ```php $conv->toGif() ``` ### Download file ```php 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. ```php $conv->download('google.pdf', true); ``` or just simply call: ```php $conv->serve(); ``` ## 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 First install the required S3 dependencies through Composer. ```bash composer require aws/aws-sdk-php composer require league/flysystem-aws-s3-v3 ``` ```php 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 First install the required Dropbox dependencies through Composer. ```bash composer require dropox/dropbox-sdk composer require flysystem-dropbox ``` ```php 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 First install the required Rackspace dependencies through Composer. ```bash composer require rackspace/php-opencloud composer require league/flysystem-rackspace ``` ```php 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 #### Global options ###### 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. ```php $conv->setBinary('/phantomjs/binary/path/phantomjs'); ``` ###### 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. ```php 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: ```php $conv->addPage('

Raw HTML

'); ``` #### PDF options ###### Format Format is optional. Supported formats are: 'A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid'. ```php $conv->format('A4'); ``` ###### Margin Margin is optional and defaults to 1cm. ```php array('margin' => '1cm') ``` ###### Orientation Orientation ('portrait', 'landscape') is optional and defaults to 'portrait'. ```php $conv->portrait(); $conv->landscape(); ``` ###### zoomFactor zoomFactor is optional and defaults to 1 (where 1 is 100% zoom). ```php array('zoomfactor' => 1) ``` ###### Custom width and height Custom dimension is optional. Supported formats are `cm`, `px` and `in`. ```php array('width' => '900px', height => '700px') ``` ##### Example ```php $options = [ 'format' => 'A4', 'zoomfactor' => 1, 'orientation' => 'portrait', 'margin' => '1cm' ]; $conv->setPdfOptions($options); // or $conv->pdfOptions($options); // or $conv->toPdf($options); ``` #### Image options ###### Width Width is optional and defaults to 1280px (720p) and only intergers are accepted. ```php $conv->width(1280); ``` ###### Height Height is optional and only integers are accepted. ```php $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 is optional and defaults to 80. The quality must be between 1-100. ```php $conv->quality(90); ``` #####Example ```php $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 - [Anam Hossain](https://github.com/anam-hossain) - [All Contributors](https://github.com/anam-hossain/phantommagick/graphs/contributors) ## License The MIT License (MIT). Please see [LICENSE](http://opensource.org/licenses/MIT) for more information. ================================================ FILE: composer.json ================================================ { "name": "anam/phantommagick", "description": "PhantomMagick provides a simple API to ease the process of converting HTML to PDF or images", "keywords": [ "Pdf", "Html to pdf", "html to image", "Html 2 pdf", "Html 2 jpg", "html to png", "Screen capture", "invoice", "Laravel pdf", "Laravel", "Phantomjs", "pdf converter", "image converter", "converter", "Phantom" ], "homepage": "https://github.com/anam-hossain/phantommagick", "license": "MIT", "authors": [ { "name": "Anam hossain", "email": "enam33@gmail.com" } ], "require": { "php": ">=5.4.0", "league/flysystem": "~1.0" }, "require-dev": { "mockery/mockery": "~0.9", "phpunit/phpunit": "~4.4" }, "autoload": { "psr-4": { "Anam\\PhantomMagick\\" : "src/" } }, "autoload-dev": { "psr-4": { "Anam\\PhantomMagick\\Test\\": "tests" } } } ================================================ FILE: phpunit.xml.dist ================================================ tests/ src/ ================================================ FILE: src/Adapter.php ================================================ client = $client; $this->args = $args; } /** * Set the Filesystem driver * * @param string $driver */ public function setDriver($driver) { $this->driver = $driver; } /** * Get the driver * * @return string */ public function getDriver() { return $this->driver; } /** * Determine which Flysystem adapter required * * @return mixed */ public function pick() { // Amazon S3 if ($this->client instanceof \Aws\S3\S3Client) { $this->setDriver('s3'); return $this->s3(); } // Dropbox if ($this->client instanceof \Dropbox\Client) { $this->setDriver('dropbox'); return $this->dropbox(); } // Rackspace cloudFiles if ($this->client instanceof \OpenCloud\ObjectStore\Resource\Container) { $this->setDriver('rackspace'); return $this->rackspace(); } } /** * Create a new AwsS3Adapter instance. * * @return mixed */ public function s3() { $pathPrefix = ''; if (! isset($this->args[0])) { throw new InvalidArgumentException('S3 Bucket name is required'); } $bucket = $this->args[0]; if (isset($this->args[1]) && $this->args[1]) { $pathPrefix = $this->args[1]; } return new \League\Flysystem\AwsS3v3\AwsS3Adapter($this->client, $bucket, $pathPrefix); } /** * Create a new DropboxAdapter instance. * * @return \League\Flysystem\Dropbox\DropboxAdapter */ public function dropbox() { $prefix = null; if (isset($this->args[0])) { $prefix = $this->args[0]; } return new \League\Flysystem\Dropbox\DropboxAdapter($this->client, $prefix); } /** * Create a new RackspaceAdapter instance. * * @return \League\Flysystem\Rackspace\RackspaceAdapter */ public function rackspace() { return new \League\Flysystem\Rackspace\RackspaceAdapter($this->client); } } ================================================ FILE: src/Converter.php ================================================ 'A4', // 1 = 100% zoom 'zoomfactor' => 1, 'quality' => '70', //Orientation: 'portrait', 'landscape' 'orientation' => 'portrait', 'margin' => '1cm' ]; /** * Default Image settings * * @var array */ protected static $imageOptions = [ // Dimension in pixels. // if only width is given full webpage will render // if both width and height is given, // the image will be clipped to given width and height 'dimension' => '1280px', // 1 = 100% zoom 'zoomfactor' => 1, 'quality' => '80' ]; /** * Supported image formats * * @var array */ protected static $imageFormats = [ 'png' => '.png', 'jpg' => '.jpg', 'gif' => '.gif' ]; /** * Supported Paper sizes. * Only use in PDF conversion * * @var array */ protected static $paperSizes = [ 'A3', 'A4', 'A5', 'Legal', 'Letter', 'Tabloid' ]; /** * Initialize the Converter * * @param string $source source of the data file */ public function __construct($source = null) { $this->initialize(); if ($source) { $this->setSource($source); } parent::__construct(); } /** * Initialize the converter settings * * @return void */ private function initialize() { self::$scripts['converter'] = dirname(__FILE__) . '/scripts/phantom_magick.js'; } /** * Create a new Converter instance. * * @param string $source Source of the data file * @return Converter */ public static function make($source) { return new self($source); } /** * Pick appropriate Flysystem adapter for a client * * @param mixed $client * @return $this */ public function adapter($client) { $args = func_get_args(); array_shift($args); $adapter = new Adapter($client, $args); $this->filesystem = new Filesystem($adapter->pick()); $this->driver = $adapter->getDriver(); return $this; } /** * Set visibility * * @param string $acl * @return $this */ public function acl($acl) { $this->acl = $acl; return $this; } /** * Set PhantomJS binary location * * @param string $binary phantomsjs location * @return $this **/ public function setBinary($binary) { $this->binary = $binary; return $this; } /** * Get the Executable PhantomJS binary source * * @return string */ public function getBinary() { return $this->binary; } /** * Get the driver of the filesystem * * @return string */ public function getDriver() { return $this->driver; } /** * Set the temporary file path * * @param string $filename * @return void */ public function setTempFilePath($filename) { $this->tempFilePath = $filename; } /** * Get the temporary file location * * @return string */ public function getTempFilePath() { return $this->tempFilePath; } /** * Get the conversion scripts * * @return array */ public function getScript() { return self::$scripts['converter']; } /** * Set the data source * * @param string $source * @return $this */ public function setSource($source) { $this->source = $source; return $this; } /** * Get the data source * * @return string */ public function getSource() { return $this->source; } /** * Alias of the setSource() * * @param string $source * @return string */ public function source($source) { return $this->setSource($source); } /** * Prepare converter for pdf conversion * * @param array $options PDF settings * @return $this */ public function toPdf(array $options = []) { $this->pdfOptions($options); $this->setTempFilePath(sys_get_temp_dir() . '/' . uniqid(rand()) . '.pdf'); return $this; } /** * Prepare converter for PNG conversion * * @param array $options Image settings * @return $this */ public function toPng(array $options = []) { return $this->prepareImage($options, $format = 'png'); } /** * Prepare converter for JPG conversion * * @param array $options Image settings * @return $this */ public function toJpg(array $options = []) { return $this->prepareImage($options, $format = 'jpg'); } /** * Prepare converter for GIF conversion * * @param array $options Image settings * @return $this */ public function toGif(array $options = []) { return $this->prepareImage($options, $format = 'gif'); } /** * Prepare converter for Image conversion * * @param array $options Image settings * @param string $format Image format JPG|PNG|GIF * * @return $this */ public function toImage($options, $format = 'png') { return $this->prepareImage($options, $format); } /** * Prepare converter for Image conversion * * @param array $options Image settings * @param string $format Image format JPG|PNG|GIF * * @return $this */ public function prepareImage($options, $format = 'png') { $format = strtolower($format); if (! array_key_exists($format, self::$imageFormats)) { throw new FileFormatNotSupportedException("{$format} file format not Supported."); } self::$format = $format; $this->imageOptions($options); $this->setTempFilePath(sys_get_temp_dir() . '/' . uniqid(rand()) . self::$imageFormats[$format]); return $this; } /** * Add HTMl page * * @param string $page Data file path|URL|Raw html code */ public function addPage($page) { self::$multiPage = true; if (count($this->pages)) { $this->pageBreak(); } $this->pushContent($page); return $this; } /** * Add multiple pages * * @param array $pages Data files paths|URLs|Raw HTML code * @return $this */ public function addPages(array $pages) { self::$multiPage = true; foreach ($pages as $page) { if (count($this->pages)) { $this->pageBreak(); } $this->pushContent($page); } return $this; } /** * Push data to pages * * @param string $page file path|URL|Raw HTML * @return void */ public function pushContent($page) { // @file_get_contents will not throw any exception due to @ symbol // file_get_contents will try to load file from physical path or from an URL // and will return the content as string // If failed, it will return false. $content = @file_get_contents($page); // Perhaps raw HTML content. if (! $content) { $content = $page; } array_push($this->pages, $content); } /** * Get pages * * @return array */ public function getPages() { return $this->pages; } /** * Add page break to pages * * @return void */ public function pageBreak() { $content = '
'; array_push($this->pages, $content); } /** * Add contents to the tempfile file. * * @param string $content * @return void */ protected function put($content) { $this->createTempFile(); file_put_contents($this->getTempFilePath(), $content, FILE_APPEND); } /** * Create a temporary html file for converting mutipages pdf * * @return string */ protected function createTempFile() { $this->setTempFilePath(sys_get_temp_dir() . '/' . uniqid(rand()) . '.html'); if (! touch($this->getTempFilePath())) { throw new RuntimeException('Unable to create file in temp directory: '. sys_get_temp_dir()); } return $this->getTempFilePath(); } /** * Force download file when conversion is completed * * @param string $downloadAs filename * @param boolean $inline Show file in browser or not * @return void */ public function download($downloadAs = null, $inline = false) { // Force "local" driver. $this->driver = 'local'; $filename = $this->getTempFilePath(); if (self::$multiPage) { $this->put(implode('', $this->pages)); $this->resetPages(); $filename = dirname($this->getTempFilePath()) . "/" . basename($this->getTempFilePath(), ".html") . ".pdf"; } $result = $this->save($filename); // Log warning or errors using PHP system logger if (trim($result)) { error_log($result); } $pathParts = pathinfo($filename); $downloadAs = $downloadAs? $downloadAs : $pathParts['basename']; $contentDisposition = $inline? 'inline' : 'attachment'; $contentType = $this->contentType($pathParts['extension']); if (! file_exists($filename)) { error_log("Conversion failed."); return "Conversion failed."; } header('Content-Description: File Transfer'); header("Content-Type: {$contentType}"); header("Content-Disposition: {$contentDisposition}; filename={$downloadAs}"); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); readfile($filename); unlink($filename); $this->clearTempFiles(); exit; } /** * Download and display the file in browser * * @return void */ public function serve() { $this->download(null, true); } /** * Save PDF|Image to the given file path if driver is local. * or Save file in cloud with provided filename * * @param string $filename full physical path with filename for local driver or just filename for cloud. * @return mixed **/ public function save($filename = null) { if ($this->driver == 'local' && ! $filename) { throw new InvalidArgumentException("Filename can not be empty. Please provide a full physical path with filename."); } if (! $filename) { $pathParts = pathinfo($this->getSource()); $filename = $pathParts['filename'] . '.' . self::$format; if (self::$multiPage) { $filename = uniqid('phantom_magick') . '.' . self::$format; } } if ($this->driver == 'local') { return $this->saveLocal($filename); } return $this->saveCloud($filename); } /** * Save PDF|Image to the given file path. * * @param string $filename full physical path with filename * @return mixed **/ public function saveLocal($filename) { if (self::$multiPage) { if (count($this->pages)) { $this->put(implode('', $this->pages)); } return $this->run(self::$scripts['converter'], $this->getTempFilePath(), $filename, self::$pdfOptions); } // Single page pdf if (self::$format === 'pdf') { return $this->run(self::$scripts['converter'], $this->getSource(), $filename, self::$pdfOptions); } // Image return $this->run(self::$scripts['converter'], $this->getSource(), $filename, self::$imageOptions); } /** * Save PDF|Image to the cloud. * * @param string $filename. * @return mixed **/ public function saveCloud($filename) { $tempFilename = $this->getTempFilePath(); // Multi page pdf if (self::$multiPage) { if (count($this->pages)) { $this->put(implode('', $this->pages)); } $tempFilename = dirname($this->getTempFilePath()) . "/" . basename($this->getTempFilePath(), ".html") . ".pdf"; $this->run(self::$scripts['converter'], $this->getTempFilePath(), $tempFilename, self::$pdfOptions); } elseif (self::$format === 'pdf') { $this->run(self::$scripts['converter'], $this->getSource(), $tempFilename, self::$pdfOptions); } else { $this->run(self::$scripts['converter'], $this->getSource(), $tempFilename, self::$imageOptions); } if (file_exists($tempFilename)) { $contents = file_get_contents($tempFilename); unlink($tempFilename); return $this->filesystem->put($filename, $contents, ['visibility' => $this->acl]); } } /** * Set the PDF options * * @param array $options * @return $this */ public function setPdfOptions(array $options) { $this->pdfOptions($options); return $this; } /** * Get PDF options * * @return $array */ public function getPdfOptions() { return self::$pdfOptions; } /** * Update PDF settings * * @param array $options * @return $this */ public function pdfOptions(array $options) { foreach ($options as $key => $option) { if (isset(self::$pdfOptions[$key])) { self::$pdfOptions[$key] = $option; } } // Custom paper width and height will replace the default format. if (isset($options['width']) && isset($options['height'])) { // ex. 10cm*5cm, 1200px*1000px, 10in*5in, 900*600 // note: without unit (i.e 900*600) will use px. self::$pdfOptions['format'] = $options['width'] . '*' . $options['height']; } return $this; } /** * Set the Image options * * @param array $options * @return $this */ public function setImageOptions(array $options) { $this->imageOptions($options); return $this; } /** * Get Image settings * * @return $array */ public function getImageOptions() { return self::$imageOptions; } /** * Update image settings * * @param array $options * @return $this */ public function imageOptions(array $options) { foreach ($options as $key => $option) { if (isset(self::$imageOptions[$key])) { self::$imageOptions[$key] = $option; } } if (isset($options['width']) && isset($options['height'])) { // Only digits accepted if (! ctype_digit($options['width'])) { throw new Exception('Width must be a number'); } if (! ctype_digit($options['height'])) { throw new Exception('Height must be a number'); } self::$imageOptions['dimension'] = $options['width'] . 'px' . '*' . $options['height'] . 'px'; } elseif (isset($options['width'])) { if (! ctype_digit($options['width'])) { throw new Exception('Width must be a number'); } self::$imageOptions['dimension'] = $options['width'] . 'px'; } return $this; } /** * Set the Paper format for PDF conversion * @return $this */ public function format($format) { if (! in_array($format, self::$paperSizes)) { throw new Exception('Paper format not supported.'); } self::$pdfOptions['format'] = $format; return $this; } /** * Set the Portrait orientation * Only use in PDF conversion * @return $this */ public function portrait() { self::$pdfOptions['orientation'] = 'portrait'; return $this; } /** * Set the Landscape orientation * Only use in PDF conversion * @return $this */ public function landscape() { self::$pdfOptions['orientation'] = 'landscape'; return $this; } /** * Set the Width * Only use in Image conversion * @return $this */ public function width($width) { if (! ctype_digit($width)) { throw new Exception('Width must be a number'); } $dimension = explode("*", self::$imageOptions['dimension']); $dimension[0] = $width . 'px'; self::$imageOptions['dimension'] = implode("*", $dimension); return $this; } /** * Set the Height * Only use in Image conversion * @return $this */ public function height($height) { if (! ctype_digit($height)) { throw new Exception('Height must be a number'); } $dimension = explode("*", self::$imageOptions['dimension']); $dimension[1] = $height . 'px'; self::$imageOptions['dimension'] = implode("*", $dimension); return $this; } /** * Set the Image quality * Only used in Image conversion * @return $this */ public function quality($quality = 80) { if (! ($quality >=1 && $quality <=100)) { throw new Exception('Quality must be between 1-100'); } self::$imageOptions['quality'] = $quality; return $this; } /** * Determine file mime * * @param string $ext * @return string */ public function contentType($ext) { switch ($ext) { case 'pdf': return 'application/pdf'; case 'jpg': return 'image/jpeg'; case 'png': return 'image/png'; case 'gif': return 'image/gif'; default: return 'application/pdf'; } } /** * Reset pages * @return void */ public function resetPages() { $this->pages = []; } /** * Remove temporary files * * @return void */ public function clearTempFiles() { if (file_exists($this->getTempFilePath())) { unlink($this->getTempFilePath()); } } public function __destruct() { $this->clearTempFiles(); } } ================================================ FILE: src/ConverterServiceProvider.php ================================================ app->bind('converter', function() { return new \Anam\PhantomMagick\Converter; }); } } ================================================ FILE: src/Exception/FileFormatNotSupportedException.php ================================================ binary = $binary; } if (class_exists('\Anam\PhantomLinux\Path')) { $this->setAlternateBinary(\Anam\PhantomLinux\Path::binaryPath()); } } /** * Set Alternate Binary * * @param string $binary * * @return void **/ public function setAlternateBinary($binary) { $this->alternateBinary = $binary; } /** * Get Alternate binary * * @return string **/ public function getAlternateBinary() { return $this->alternateBinary; } /** * Set shell command * * @param string $command * * @return void **/ public function setCommand($command) { $this->command = $command; } /** * Get PhantomJS shell command * * @return string **/ public function getCommand() { return $this->command; } /** * Run the phantomjs command * @param string $script Conversion script * @param string $source Data file location * @param string $output Output file location * @param array $options * @return string */ public function run($script, $source, $output, array $options = array()) { $binary = $this->pickBinary(); $arguments = ['script' => $script, 'source' => $source, 'output' => $output] + $options; $arguments = $this->escapeShellArguments($arguments); $this->setCommand(escapeshellcmd("{$binary} --ssl-protocol=any --ignore-ssl-errors=yes ") . implode(' ', $arguments)); return shell_exec($this->getCommand()); } /** * Escape shell arguments * * @param array $arguments * @return array */ private function escapeShellArguments(array $arguments) { foreach ($arguments as $key => $argument) { $arguments[$key] = escapeshellarg($argument); } return $arguments; } /** * Check phantomjs is installed or not * * @param string $binary Binary location * @return boolean */ public function verifyBinary($binary) { $uname = strtolower(php_uname()); if (Str::contains($uname, 'darwin')) { if (! shell_exec(escapeshellcmd("command -v {$binary} >/dev/null 2>&1"))) { return false; } } elseif (Str::contains($uname, 'win')) { if (! shell_exec(escapeshellcmd("{$binary}"))) { return false; } } elseif (Str::contains($uname, 'linux')) { if (! shell_exec(escapeshellcmd("which {$binary}"))) { return false; } } else { throw new \RuntimeException("Unknown operating system."); } return true; } /** * Choose binary * * @return string */ public function pickBinary() { if ($this->binary != 'phantomjs') { if (! $this->verifyBinary($this->binary)) { throw new Exception('Binary does not exist'); } return $this->binary; } if (! $this->verifyBinary($this->binary)) { if (! $this->verifyBinary($this->getAlternateBinary())) { throw new Exception('Binary does not exist'); } $this->binary = $this->getAlternateBinary(); } return $this->binary; } } ================================================ FILE: src/Str.php ================================================ 8) { console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom] [orientation] [margin] [quality]'); console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px'); console.log(' "800px*600px" window, clipped to 800x600'); phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; page.viewportSize = { width: 1280, height: 720 }; if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") { size = system.args[3].split('*'); page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: margin } : { format: system.args[3], orientation: orientation, margin: margin }; } else if (system.args.length > 3 && system.args[3].substr(-2) === "px") { size = system.args[3].split('*'); if (size.length === 2) { pageWidth = parseInt(size[0], 10); pageHeight = parseInt(size[1], 10); page.viewportSize = { width: pageWidth, height: pageHeight }; page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight }; } else { // If a number received, instead of width and height, // Try to use best width and height by inspecting the number. console.log("size:", system.args[3]); pageWidth = parseInt(system.args[3], 10); pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any console.log ("pageHeight:",pageHeight); page.viewportSize = { width: pageWidth, height: pageHeight }; } } if (system.args.length > 4) { page.zoomFactor = system.args[4]; } // Add better error reporting when url fails to load. page.onResourceError = function(resourceError) { page.reason = resourceError.errorString; page.reason_url = resourceError.url; }; page.open(address, function (status) { if (status !== 'success') { console.log('Unable to load the address!'); console.log( "Error opening url \"" + page.reason_url + "\": " + page.reason ); phantom.exit(1); } else { window.setTimeout(function () { page.render(output, {quality: quality}); phantom.exit(); }, 200); } }); } ================================================ FILE: tests/AdapterTest.php ================================================ 'A3', 'zoomfactor' => 2, 'quality' => '100', 'orientation' => 'landscape', 'margin' => '2cm' ]; protected $imageOptions = [ 'dimension' => '1000px', 'zoomfactor' => 2, 'quality' => '90' ]; public function setUp() { $this->converter = new Converter(); } public function testInitialize() { $this->invokeMethod($this->converter, 'initialize'); $this->assertContains('phantom_magick.js', $this->converter->getScript(), 'Verify that phantom_magick.js is used'); } public function testMake() { $this->assertInstanceOf('Anam\PhantomMagick\Converter', Converter::make('http://code-chunk.com'), 'Check Converter::make() method returning the instance of \Anam\PhantomMagick\Converter'); } public function testSetSource() { $this->converter->setSource('http://code-chunk.com'); $this->assertEquals('http://code-chunk.com', $this->converter->getSource()); } public function testGetSource() { $this->converter->setSource('http://code-chunk.com'); $this->assertEquals('http://code-chunk.com', $this->converter->getSource()); } public function testSource() { $this->converter->source('http://code-chunk.com'); $this->assertEquals('http://code-chunk.com', $this->converter->getSource()); } public function testDefaultFileSystemDriverIsLocal() { $this->assertEquals('local', $this->converter->getDriver()); } public function testFileSystemDriverIsS3WhenS3ClientIsUsedAsClient() { $client = S3Client::factory(array( 'key' => 'dummy-key-123', 'secret' => 'dummy-secret-123' )); $this->converter->adapter($client, 'dummy-bucket'); $this->assertEquals('s3', $this->converter->getDriver()); } public function testGetTempFilePath() { $path = '/dummy/file/path'; $this->converter->setTempFilePath($path); $this->assertEquals($path, $this->converter->getTempFilePath()); } public function testPdfOptions() { $this->converter->pdfOptions($this->pdfOptions); $this->assertEquals($this->pdfOptions, $this->converter->getPdfOptions()); } public function testImageOptions() { $options = [ 'dimension' => '900px', 'zoomfactor' => 1, ]; $this->converter->imageOptions($options); $this->assertArrayHasKey('quality', $this->converter->getImageOptions()); } public function testContentType() { $mime = $this->converter->contentType('pdf'); $this->assertEquals('application/pdf', $mime); } public function testPagesIsEmpty() { $this->assertTrue(empty($this->converter->getPages())); } public function testPagesIsNotEmpty() { $this->converter->addPage('http://google.com'); $this->assertFalse(empty($this->converter->getPages())); } public function testPushContents() { $pages = ['

Phantom magick

']; $this->converter->pushContent($pages[0]); $this->assertEquals($pages, $this->converter->getPages()); } public function testPageBreak() { $pages = [ '

Phantom magick

', '
' ]; $this->converter->pushContent($pages[0]); $this->converter->pageBreak(); $this->assertEquals($pages, $this->converter->getPages()); } public function testAddPages() { $expected = [ '

Page 1

', '
', '

Page 2

', ]; $pages = [ '

Page 1

', '

Page 2

' ]; $this->converter->addPages($pages); $this->assertEquals($expected, $this->converter->getPages()); } public function testToPdf() { $this->converter->toPdf($this->pdfOptions); // Check pdf options is set properly $this->assertEquals($this->pdfOptions, $this->converter->getPdfOptions()); // Check .pdf extension is set $this->assertContains('.pdf', $this->converter->getTempFilePath()); } public function testToPng() { $this->converter->toPng($this->imageOptions); $this->assertEquals($this->imageOptions, $this->converter->getImageOptions()); // Check .png extension is set $this->assertContains('.png', $this->converter->getTempFilePath()); } public function testToJpg() { $this->converter->toJpg($this->imageOptions); $this->assertEquals($this->imageOptions, $this->converter->getImageOptions()); // Check .png extension is set $this->assertContains('.jpg', $this->converter->getTempFilePath()); } public function testToGif() { $this->converter->toGif($this->imageOptions); $this->assertEquals($this->imageOptions, $this->converter->getImageOptions()); // Check .png extension is set $this->assertContains('.gif', $this->converter->getTempFilePath()); } } ================================================ FILE: tests/PrivateAndProtectedMethodsAccessibleTrait.php ================================================ getMethod($methodName); $method->setAccessible(true); return $method->invokeArgs($object, $parameters); } } ================================================ FILE: tests/RunnerTest.php ================================================ runner = new Runner(); } /** * Runner tests */ public function testVerifyBinary() { $this->assertTrue(true); } } ================================================ FILE: tests/test_page.html ================================================ PhantomMagick

Testing PhantomMagick

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.