barryvdh/laravel-snappy
Composer 安装命令:
composer require barryvdh/laravel-snappy
包简介
Snappy PDF/Image for Laravel
README 文档
README
This package is a ServiceProvider for Snappy: https://github.com/KnpLabs/snappy.
Wkhtmltopdf Installation
Choose one of the following options to install wkhtmltopdf/wkhtmltoimage.
- Download wkhtmltopdf from here;
- 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
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.
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.
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:
<?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
barryvdh/laravel-snappy 适用场景与选型建议
barryvdh/laravel-snappy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.37M 次下载、GitHub Stars 达 2.76k, 最近一次更新时间为 2013 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「snappy」 「pdf」 「laravel」 「wkhtmltopdf」 「wkhtmltoimage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 barryvdh/laravel-snappy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 barryvdh/laravel-snappy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 barryvdh/laravel-snappy 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
MvlabsSnappy is a Zend Framework and Laminas module that allow easy to thumbnail, snapshot or PDF generation from a url or a html page using Snappy PHP wrapper for the wkhtmltopdf/wkhtmltoimage conversion utility.
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
The Yii2 extension uses jQuery PrettyPhoto and OwlCarousel js and makes image galary from php array of structure defined.
Snappy Service Provider for Silex
Snappy PDF/Image for Laravel
统计信息
- 总下载量: 26.37M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2784
- 点击次数: 28
- 依赖项目数: 49
- 推荐数: 14
其他信息
- 授权协议: MIT
- 更新时间: 2013-12-18