diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c1fc0c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0a1c1cb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + +before_script: + - curl -s http://getcomposer.org/installer | php + - php composer.phar install --dev + +script: phpunit \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2a64523 --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "barryvdh/laravel-snappy", + "description": "Snappy PDF/Image for Laravel 4", + "keywords": ["laravel","snappy", "pdf", "image", "wkhtmltopdf", "wkhtmltoimage"], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "require": { + "php": ">=5.3.0", + "illuminate/support": "4.x", + "knplabs/knp-snappy": "*" + }, + "autoload": { + "psr-0": { + "Barryvdh\\Snappy": "src/" + } + }, + "minimum-stability": "dev" +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e89ac6d --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..793142f --- /dev/null +++ b/readme.md @@ -0,0 +1,71 @@ +## Snappy PDF/Image Wrapper for Laravel 4 + +You need to have wkhtmltopdf/wkhtmltoimage installed. See https://github.com/KnpLabs/snappy#wkhtmltopdf-binary-as-composer-dependencies +This package is a ServiceProvider for Snappy: https://github.com/KnpLabs/snappy +It provides $app['snappy.pdf'] and $app['snappy.image']. You have to set the binary location in the config file. +For example, when loaded with composer: + + 'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'), + +In addition to the Snappy classes, it provides a wrapper, similar to https://github.com/barryvdh/laravel-dompdf + +Require this package in your composer.json and update composer. + + "barryvdh/laravel-snappy": "dev-master" + +After updating composer, add the ServiceProvider to the providers array in app/config/app.php + + 'Barryvdh\Snappy\ServiceProvider', + +You can optionally use the facade for shorter code. Add this to your facades: + + 'PDF' => 'Barryvdh\Snappy\Facades\SnappyPdf', + 'Image' => 'Barryvdh\Snappy\Facades\SnappyImage', + +You can create a new Snappy PDF/Image instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download. + +Using the App container: + + $snappy = App::make('snappy.pdf'); + //To file + $snappy->generateFromHtml('

Bill

You owe me money, dude.

', '/tmp/bill-123.pdf'); + $snappy->generate('http://www.github.com', '/tmp/github.pdf')); + //Or output: + return new Response( + $snappy->getOutputFromHtml($html), + 200, + array( + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'attachment; filename="file.pdf"' + ) + ); + +Using the wrapper: + + $pdf = App::make('snappy.pdf.wrapper'); + $pdf->loadHTML('

Test

'); + return $pdf->stream(); + +Or use the facade: + + $pdf = PDF::loadView('pdf.invoice', $data); + return $pdf->download('invoice.pdf'); + +You can chain the methods: + + return PDF::loadFile('http://www.github.com')->stream('github.pdf'); + +You can change the orientation and paper size + + PDF::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf') + +If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself. + +You can publish the config-file to change some settings (default paper etc). + + php artisan config:publish barryvdh/laravel-snappy + + +### License + +This Snappy Wrapper for Laravel4 is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) diff --git a/src/Barryvdh/Snappy/Facades/SnappyImage.php b/src/Barryvdh/Snappy/Facades/SnappyImage.php new file mode 100644 index 0000000..f4169aa --- /dev/null +++ b/src/Barryvdh/Snappy/Facades/SnappyImage.php @@ -0,0 +1,16 @@ +snappy = $snappy; + $this->options = array(); + } + + + /** + * Set the paper size (default A4) + * + * @param string $paper + * @param string $orientation + * @return $this + */ + public function setPaper($paper, $orientation=null){ + $this->snappy->setOption('page-size', $paper); + if($orientation){ + $this->snappy->setOption('orientation', $orientation); + } + return $this; + } + + /** + * Set the orientation (default portrait) + * + * @param string $orientation + * @return static + */ + public function setOrientation($orientation){ + $this->snappy->setOption('orientation', $orientation); + return $this; + } + + /** + * Show or hide warnings + * + * @param bool $warnings + * @return $this + * @deprecated + */ + public function setWarnings($warnings){ + //Doesn't do anything + return $this; + } + + public function setOption($name, $value){ + $this->snappy->setOption($name, $value); + return $this; + } + + public function setOptions($options){ + $this->snappy->setOptions($options); + return $this; + } + + /** + * Load a HTML string + * + * @param string $string + * @return static + */ + public function loadHTML($string){ + $this->html = (string) $string; + $this->file = null; + return $this; + } + + /** + * Load a HTML file + * + * @param string $file + * @return static + */ + public function loadFile($file){ + $this->html = null; + $this->file = $file; + return $this; + } + + /** + * Load a View and convert to HTML + * + * @param string $view + * @param array $data + * @param array $mergeData + * @return static + */ + public function loadView($view, $data = array(), $mergeData = array()){ + $html = \View::make($view, $data, $mergeData); + $this->html = (string) $html; + $this->file = null; + return $this; + } + + + + /** + * Output the PDF as a string. + * + * @return string The rendered PDF as string + */ + public function output(){ + $output = ''; + if($this->html){ + $output = $this->snappy->getOutputFromHtml($this->html, $this->options); + }elseif($this->file){ + $output = $this->snappy->getOutput($this->file, $this->options); + } + return $output; + } + + /** + * Save the PDF to a file + * + * @param $filename + * @return static + */ + public function save($filename){ + + if($this->html){ + $this->snappy->generateFromHtml($this->html, $filename, $this->options); + }elseif($this->file){ + $this->snappy->generate($this->file, $filename, $this->options); + } + + return $this; + } + + /** + * Make the PDF downloadable by the user + * + * @param string $filename + * @return \Symfony\Component\HttpFoundation\Response + */ + public function download($filename = 'document.pdf' ){ + $output = $this->output(); + return \Response::make($output, 200, array( + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'attachment; filename="'.$filename.'"' + )); + } + + /** + * Return a response with the PDF to show in the browser + * + * @param string $filename + * @return \Symfony\Component\HttpFoundation\Response + */ + public function stream($filename = 'document.pdf' ){ + $that = $this; + return \Response::stream(function() use($that){ + echo $that->output(); + }, 200, array( + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename="'.$filename.'"', + )); + } + + public function __call($name, $arguments){ + return call_user_func_array (array( $this->snappy, $name), $arguments); + } + +} \ No newline at end of file diff --git a/src/Barryvdh/Snappy/ServiceProvider.php b/src/Barryvdh/Snappy/ServiceProvider.php new file mode 100644 index 0000000..13cf74d --- /dev/null +++ b/src/Barryvdh/Snappy/ServiceProvider.php @@ -0,0 +1,63 @@ +package('barryvdh/laravel-snappy'); + + if($this->app['config']->get('laravel-snappy::config.pdf.enabled')){ + $this->app['snappy.pdf'] = $this->app->share(function($app) + { + $binary = $app['config']->get('laravel-snappy::config.pdf.binary'); + $options = $app['config']->get('laravel-snappy::config.pdf.options'); + $snappy = new Pdf($binary, $options); + return $snappy; + }); + + $this->app['snappy.pdf.wrapper'] = $this->app->share(function($app) + { + return new PdfWrapper($app['snappy.pdf']); + }); + } + + + if($this->app['config']->get('laravel-snappy::config.image.enabled')){ + $this->app['snappy.image'] = $this->app->share(function($app) + { + $binary = $app['config']->get('laravel-snappy::config.image.binary'); + $options = $app['config']->get('laravel-snappy::config.image.options'); + $image = new Image($binary, $options); + return $image; + }); + } + + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('snappy.pdf', 'snappy.pdf.wrapper', 'snappy.image'); + } + +} \ No newline at end of file diff --git a/src/config/.gitkeep b/src/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/config/config.php b/src/config/config.php new file mode 100644 index 0000000..6c770a3 --- /dev/null +++ b/src/config/config.php @@ -0,0 +1,18 @@ + array( + 'enabled' => true, + 'binary' => '/usr/local/bin/wkhtmltopdf', + 'options' => array(), + ), + 'image' => array( + 'enabled' => true, + 'binary' => '/usr/local/bin/wkhtmltoimage', + 'options' => array(), + ), + + +); diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29