knplabs/knp-snappy-bundle
Composer 安装命令:
composer require knplabs/knp-snappy-bundle
包简介
Easily create PDF and images in Symfony by converting Twig/HTML templates.
README 文档
README
Snappy is a PHP wrapper for the wkhtmltopdf conversion utility. It allows you to generate either pdf or image files from your html documents, using the webkit engine.
The KnpSnappyBundle provides a simple integration for your Symfony project.
Limitations
If you use JavaScript to render your pages, you may encounter some issues because of wkhtmltopdf not being fully compatible with ES6 apis. The only way to solve this issue is to provide polyfills that fix the gaps between modern ES6 apis and the wkhtmltopdf rendering engine.
Installation
With composer, require:
composer require knplabs/knp-snappy-bundle
If you are not using Flex, enable it in your kernel :
// config/bundles.php <?php return [ //... Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' => true], //... ];
Configuration
If you need to change the binaries, change the instance options or even disable one or both services, you can do it through the configuration.
# config/packages/knp_snappy.yaml knp_snappy: pdf: enabled: true binary: /usr/local/bin/wkhtmltopdf #"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\"" for Windows users options: [] image: enabled: true binary: /usr/local/bin/wkhtmltoimage #"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\"" for Windows users options: []
If you want to change temporary folder which is sys_get_temp_dir() by default, you can use
# config/packages/knp_snappy.yaml knp_snappy: temporary_folder: "%kernel.cache_dir%/snappy"
You can also configure the timeout used by the generators with process_timeout:
# config/packages/knp_snappy.yaml knp_snappy: process_timeout: 20 # In seconds
Usage
The bundle registers two services:
- the
knp_snappy.imageservice allows you to generate images; - the
knp_snappy.pdfservice allows you to generate pdf files.
Generate an image from a URL
// @var Knp\Snappy\Image $knpSnappyImage->generate('http://www.google.fr', '/path/to/the/image.jpg');
Generate a pdf document from a URL
// @var \Knp\Snappy\Pdf $knpSnappyPdf->generate('http://www.google.fr', '/path/to/the/file.pdf');
Generate a pdf document from multiple URLs
// @var \Knp\Snappy\Pdf $knpSnappyPdf->generate(array('http://www.google.fr', 'http://www.knplabs.com', 'http://www.google.com'), '/path/to/the/file.pdf');
Generate a pdf document from a twig view
// @var \Knp\Snappy\Pdf $knpSnappyPdf->generateFromHtml( $this->renderView( 'MyBundle:Foo:bar.html.twig', array( 'some' => $vars ) ), '/path/to/the/file.pdf' );
Render an image as response from a controller
use Knp\Bundle\SnappyBundle\Snappy\Response\JpegResponse; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class SomeController extends AbstractController { public function imageAction(\Knp\Snappy\Image $knpSnappyImage) { $html = $this->renderView('MyBundle:Foo:bar.html.twig', array( 'some' => $vars )); return new JpegResponse( $knpSnappyImage->getOutputFromHtml($html), 'image.jpg' ); } }
Render a pdf document as response from a controller
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class SomeController extends AbstractController { public function pdfAction(\Knp\Snappy\Pdf $knpSnappyPdf) { $html = $this->renderView('MyBundle:Foo:bar.html.twig', array( 'some' => $vars )); return new PdfResponse( $knpSnappyPdf->getOutputFromHtml($html), 'file.pdf' ); } }
Render a pdf document with a relative url inside like css files
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class SomeController extends AbstractController { public function pdfAction(\Knp\Snappy\Pdf $knpSnappyPdf) { $pageUrl = $this->generateUrl('homepage', array(), true); // use absolute path! return new PdfResponse( $knpSnappyPdf->getOutput($pageUrl), 'file.pdf' ); } }
Maintainers
KNPLabs is looking for maintainers (see why).
If you are interested, feel free to open a PR to ask to be added as a maintainer.
We’ll be glad to hear from you :)
Credits
SnappyBundle and Snappy are based on the awesome wkhtmltopdf. SnappyBundle has been developed by KnpLabs.
knplabs/knp-snappy-bundle 适用场景与选型建议
knplabs/knp-snappy-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.22M 次下载、GitHub Stars 达 1.24k, 最近一次更新时间为 2012 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「knp」 「knplabs」 「bundle」 「snappy」 「pdf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 knplabs/knp-snappy-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 knplabs/knp-snappy-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 knplabs/knp-snappy-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configure your knp menus with yaml files accross your project bundles
KnpMenu for CakePHP 3
KNP Menu Bundle integrations with Superdesk Publisher.
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
统计信息
- 总下载量: 33.22M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1257
- 点击次数: 29
- 依赖项目数: 64
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2012-01-16