Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

laravel-snappy

Laravel Snappy PDF

Star full 4f7b624809470f25b6493d5a7b30d9b9cb905931146e785d67c86ef0c205a402Star full 4f7b624809470f25b6493d5a7b30d9b9cb905931146e785d67c86ef0c205a402Star full 4f7b624809470f25b6493d5a7b30d9b9cb905931146e785d67c86ef0c205a402Star full 4f7b624809470f25b6493d5a7b30d9b9cb905931146e785d67c86ef0c205a402Star blank 374f33e4d622a2930833db3cbea26b5d03dc44961a6ecab0b9e13276d97d6682 (1 ratings)
Rated 4.0 out of 5
Subscribe to updates I use laravel-snappy


Statistics on laravel-snappy

Number of watchers on Github 923
Number of open issues 142
Average time to close an issue 17 days
Main language PHP
Average time to merge a PR 8 days
Open pull requests 9+
Closed pull requests 10+
Last commit over 1 year ago
Repo Created almost 6 years ago
Repo Last Updated over 1 year ago
Size 59 KB
Organization / Authorbarryvdh
Latest Releasev0.4.1
Contributors7
Page Updated
Do you use laravel-snappy? Leave a review!
View open issues (142)
View laravel-snappy activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating laravel-snappy for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Snappy PDF/Image Wrapper for Laravel 5 and Lumen 5.1

For Laravel 4.x, check version 0.1

This package is a ServiceProvider for Snappy: https://github.com/KnpLabs/snappy.

Wkhtmltopdf Installation

Choose one of the following options to install wkhtmltopdf/wkhtmltoimage.

  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.

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

Attention vagrant users!

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

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:

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

This will prevent the error 126.

Package Installation

Require this package in your composer.json and update composer.

composer require barryvdh/laravel-snappy

Laravel

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

Barryvdh\Snappy\ServiceProvider::class,

Optionally you can use the Facade for shorter code. Add this to your facades:

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

Finally you can publish the config file:

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.

For example, when loaded with composer, the line should look like:

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

If you followed the vagrant steps, the line should look like:

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

For windows users you'll have to add double quotes to the bin path for wkhtmltopdf:

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

Lumen

In bootstrap/app.php add:

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

Optionally, add the facades like so:

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.

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.

Using the App container:

$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:

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

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')->inline('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.

See the wkhtmltopdf manual for more information/settings.

License

This Snappy Wrapper for Laravel is open-sourced software licensed under the MIT license

laravel-snappy open issues Ask a question     (View All Issues)
  • almost 3 years Rasterize PDF and Signature Field
  • almost 3 years The exit status code '126' says something went wrong
  • about 3 years Class Barryvdh\Snappy\ServiceProvider not found
  • about 3 years PDF header and footer
  • about 3 years RuntimeException
  • about 3 years Problem display Asset
  • about 3 years margin-top cuts off header
  • about 3 years Greek characters problem in footer and header
  • about 3 years The process has been signaled with signal "11".
  • about 3 years Symfony\Component\Process\Exception\ProcessTimedOutException after 60 seconds
  • about 3 years The exit status code '2'
  • about 3 years The exit status code '134' says something went wrong stderr: "QXcbConnection: Could not connect to display Abosrted"
  • about 3 years Exit with code 1 due to network error: HostNotFoundError
  • about 3 years Strange behavior - duplicate inserts
  • over 3 years escapeshellarg() has been disabled for security reasons
  • over 3 years Unable to call the Snappy from within the Controller
  • over 3 years Method setOption() removes HTML symbol on Linux
  • over 3 years Error Google Chart
  • over 3 years Print color instead of grey pdf.
  • over 3 years thead and tbody contents overlap from second page onwards
  • over 3 years new changes for Lumen support
  • over 3 years Problem With Encoding
  • over 3 years Won't load assets from public folder on shared hosting
  • over 3 years Windows server 2012, IIS, exit status code '1'
  • over 3 years Laravel 5, Windows 7, PDF
  • over 3 years Laravel-Snappy Installation on Godaddy share hosting
  • over 3 years Failing to load resources from http://localhost:8888
  • over 3 years Send laravel session?
  • almost 4 years Send generated file as multipart.
  • almost 4 years Call to undefined method Knp\Snappy\Pdf::loadView()
laravel-snappy open pull requests (View All Pulls)
  • Use fully-qualified class names in return docblocks for download and stream methods of PdfWrapper
  • highlights
  • Facade class referenced by using the ::class function
  • Enable overwriting of files
  • Adding language hints to code blocks.
  • add support for env variables
  • added addView method
  • Added info for using laravel-snappy in Homestead
  • Added setTempFolder fluent method to PdfWrapper
laravel-snappy questions on Stackoverflow (View All Questions)
  • laravel-Snappy PDF DIVs incorrectly sized using height:11in
  • Laravel / Snappy: can not generate PDF or create folder
  • Could not install laravel-snappy in laravel 4
laravel-snappy list of languages used
Other projects in PHP