prolix / knp-snappy-bundle
通过转换Twig/HTML模板,在Symfony2中轻松创建PDF和图片。
v1.6.1
2019-12-16 05:11 UTC
Requires
- php: >=7.1
- knplabs/knp-snappy: 1.2.*
- symfony/framework-bundle: ~2.7|~3.0|^4.0|^5.0
Requires (Dev)
- doctrine/annotations: ~1.8
- phpunit/phpunit: ~8.5
- symfony/asset: ~2.7|~3.0|^4.0|^5.0
- symfony/finder: ~2.7|~3.0|^4.0|^5.0
- symfony/security-csrf: ~2.7|~3.0|^4.0|^5.0
- symfony/templating: ~2.7|~3.0|^4.0|^5.0
- symfony/validator: ~2.7|~3.0|^4.0|^5.0
- symfony/yaml: ~3.0|~4.0|^5.0
This package is auto-updated.
Last update: 2024-09-16 16:12:45 UTC
README
Snappy 是一个PHP (5.6+) 包装器,用于wkhtmltopdf转换工具。它允许您使用webkit引擎从HTML文档生成PDF或图片文件。
KnpSnappyBundle 为您的Symfony项目提供了一个简单的集成。
安装
使用 composer,需要
composer require knplabs/knp-snappy-bundle
然后在您的kernel中启用它(如果您使用的是Symfony4的Flex配方,则此步骤是可选的)
// app/AppKernel.php public function registerBundles() { $bundles = array( //... new Knp\Bundle\SnappyBundle\KnpSnappyBundle(), //...
配置
如果您需要更改二进制文件、实例选项,甚至禁用一个或两个服务,您可以通过配置来实现。
# app/config/config.yml (or config/packages/knp_snappy.yaml if using Symfony4 and the Flex recipe) 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: []
如果您想更改默认的临时文件夹 sys_get_temp_dir()
,可以使用
# app/config/config.yml (or config/packages/knp_snappy.yaml if using Symfony4 and the Flex recipe) knp_snappy: temporary_folder: "%kernel.cache_dir%/snappy"
您还可以使用 process_timeout
配置生成器使用的超时时间。
# app/config/config.yml (or config/packages/knp_snappy.yaml if using Symfony4 and the Flex recipe) knp_snappy: process_timeout: 20 # In seconds
使用
该捆绑包注册了两个服务
- 的
knp_snappy.image
服务允许您生成图片; - 的
knp_snappy.pdf
服务允许您生成PDF文件。
从URL生成图片
$container->get('knp_snappy.image')->generate('http://www.google.fr', '/path/to/the/image.jpg');
从URL生成PDF文档
$container->get('knp_snappy.pdf')->generate('http://www.google.fr', '/path/to/the/file.pdf');
从多个URL生成PDF文档
$container->get('knp_snappy.pdf')->generate(array('http://www.google.fr', 'http://www.knplabs.com', 'http://www.google.com'), '/path/to/the/file.pdf');
从Twig视图生成PDF文档
$this->get('knp_snappy.pdf')->generateFromHtml( $this->renderView( 'MyBundle:Foo:bar.html.twig', array( 'some' => $vars ) ), '/path/to/the/file.pdf' );
从控制器中渲染图片作为响应
use Knp\Bundle\SnappyBundle\Snappy\Response\JpegResponse; class SomeController { public function imageAction() { $html = $this->renderView('MyBundle:Foo:bar.html.twig', array( 'some' => $vars )); return new JpegResponse( $this->get('knp_snappy.image')->getOutputFromHtml($html), 'image.jpg' ); } }
从控制器中渲染PDF文档作为响应
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; class SomeController extends Controller { public function pdfAction() { $html = $this->renderView('MyBundle:Foo:bar.html.twig', array( 'some' => $vars )); return new PdfResponse( $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 'file.pdf' ); } }
渲染包含相对URL的PDF文档,例如CSS文件
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; class SomeController extends Controller { public function pdfAction() { $pageUrl = $this->generateUrl('homepage', array(), true); // use absolute path! return new PdfResponse( $this->get('knp_snappy.pdf')->getOutput($pageUrl), 'file.pdf' ); } }
维护者
KNPLabs正在寻找维护者(查看原因)。
如果您感兴趣,请随意提交PR请求以请求成为维护者。
我们将很高兴听到您的消息 :)
鸣谢
SnappyBundle 和 Snappy 基于出色的 wkhtmltopdf。SnappyBundle 由 KnpLabs 开发。