2017 © Pedro Peláez
 

library laravel-snappy

Snappy PDF/Image for Laravel 4

image

barryvdh/laravel-snappy

Snappy PDF/Image for Laravel 4

  • Wednesday, June 13, 2018
  • by Barryvdh
  • Repository
  • 39 Watchers
  • 1067 Stars
  • 1,428,261 Installations
  • PHP
  • 35 Dependents
  • 10 Suggesters
  • 171 Forks
  • 159 Open issues
  • 15 Versions
  • 12 % Grown

The README.md

Snappy PDF/Image Wrapper for Laravel

Tests Packagist License Latest Stable Version Total Downloads Fruitcake, (*1)

This package is a ServiceProvider for Snappy: https://github.com/KnpLabs/snappy., (*2)

Wkhtmltopdf Installation

Choose one of the following options to install wkhtmltopdf/wkhtmltoimage., (*3)

  1. Download wkhtmltopdf from here;
  2. Or install as a composer dependency. See wkhtmltopdf binary as composer dependencies for more information.

Attention! Please note that some dependencies (libXrender for example) may not be present on your system and may require manual installation.

Testing the wkhtmltopdf installation

After installing you should be able to run wkhtmltopdf from the command line / shell., (*4)

If you went for the second option the binaries will be at /vendor/h4cc/wkhtmltoimage-amd64/bin and /vendor/h4cc/wkhtmltopdf-amd64/bin., (*5)

Attention vagrant users!

Move the binaries to a path that is not in a synced folder, for example:, (*6)

cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/

and make it executable:, (*7)

chmod +x /usr/local/bin/wkhtmltoimage-amd64 
chmod +x /usr/local/bin/wkhtmltopdf-amd64

This will prevent the error 126., (*8)

Package Installation

Require this package in your composer.json and update composer., (*9)

composer require barryvdh/laravel-snappy

Laravel

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider/Facade. If you also use laravel-dompdf, watch out for conflicts. It could be better to manually register the Facade., (*10)

After updating composer, add the ServiceProvider to the providers array in config/app.php, (*11)

Barryvdh\Snappy\ServiceProvider::class,

Optionally you can use the Facade for shorter code. Add this to your facades:, (*12)

'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

Finally you can publish the config file:, (*13)

php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"

Snappy config file

The main change to this config file (config/snappy.php) will be the path to the binaries., (*14)

For example, when loaded with composer, the line should look like:, (*15)

'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),

If you followed the vagrant steps, the line should look like:, (*16)

'binary'  => '/usr/local/bin/wkhtmltopdf-amd64',

For windows users you'll have to add double quotes to the bin path for wkhtmltopdf:, (*17)

'binary' => '"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf"'

Lumen

In bootstrap/app.php add:, (*18)

class_alias('Barryvdh\Snappy\Facades\SnappyPdf', 'PDF');
$app->register(Barryvdh\Snappy\LumenServiceProvider::class);

Optionally, add the facades like so:, (*19)

class_alias(Barryvdh\Snappy\Facades\SnappyPdf::class, 'PDF');
class_alias(Barryvdh\Snappy\Facades\SnappyImage::class, 'SnappyImage');

To customise the configuration file, copy the file /vendor/barryvdh/laravel-snappy/config/snappy.php to the /config folder., (*20)

Usage

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 inline (show in browser) or download., (*21)

Using the App container:, (*22)

$snappy = App::make('snappy.pdf');
//To file
$html = '<h1>Bill</h1><p>You owe me money, dude.</p>';
$snappy->generateFromHtml($html, '/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:, (*23)

$pdf = App::make('snappy.pdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->inline();

Or use the facade:, (*24)

$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

You can chain the methods:, (*25)

return PDF::loadFile('http://www.github.com')->inline('github.pdf');

You can change the orientation and paper size, (*26)

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., (*27)

See the wkhtmltopdf manual for more information/settings., (*28)

Testing - PDF fake

As an alternative to mocking, you may use the PDF facade's fake method. When using fakes, assertions are made after the code under test is executed:, (*29)

<?php

namespace Tests\Feature;

use Tests\TestCase;
use PDF;

class ExampleTest extends TestCase
{
    public function testPrintOrderShipping()
    {
        PDF::fake();

        // Perform order shipping...

        PDF::assertViewIs('view-pdf-order-shipping');
        PDF::assertSee('Name');
    }
}

Other available assertions:

PDF::assertViewIs($value);
PDF::assertViewHas($key, $value = null);
PDF::assertViewHasAll(array $bindings);
PDF::assertViewMissing($key);
PDF::assertSee($value);
PDF::assertSeeText($value);
PDF::assertDontSee($value);
PDF::assertDontSeeText($value);
PDF::assertFileNameIs($value);

License

This Snappy Wrapper for Laravel is open-sourced software licensed under the MIT license, (*30)

The Versions

13/06 2018

dev-master

9999999-dev

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

08/02 2018

v0.4.1

0.4.1.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

14/08 2017

v0.4.0

0.4.0.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

10/02 2017

v0.3.3

0.3.3.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

20/01 2017

v0.3.2

0.3.2.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

05/08 2016

v0.3.1

0.3.1.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

17/05 2016

0.1.x-dev

0.1.9999999.9999999-dev

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

25/02 2016

v0.3.0

0.3.0.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

22/12 2015

v0.2.2

0.2.2.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

04/06 2015

v0.2.1

0.2.1.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

11/03 2015

v0.1.3

0.1.3.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

05/02 2015

v0.2.0

0.2.0.0

Snappy PDF/Image for Laravel 4

  Sources   Download

MIT

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

22/08 2014

v0.1.2

0.1.2.0

Snappy PDF/Image for Laravel 4

  Sources   Download

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

10/06 2014

v0.1.1

0.1.1.0

Snappy PDF/Image for Laravel 4

  Sources   Download

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage

10/06 2014

v0.1.0

0.1.0.0

Snappy PDF/Image for Laravel 4

  Sources   Download

The Requires

 

laravel image pdf wkhtmltopdf snappy wkhtmltoimage