bushidoio / pdf-bundle
BushidoIOPDFBundle
v2.0.0
2018-02-17 21:10 UTC
Requires
- php: >=5.3.2
- mpdf/mpdf: ^7.0
- symfony/framework-bundle: ~2.3
This package is not auto-updated.
Last update: 2024-09-28 17:28:08 UTC
README
The BushidoIOPDFBundle adds PDF file creation support in Symfony.
包含的功能
- 从HTML字符串内容创建PDF
- 使用
Symfony\Component\HttpFoundation\Response
对象封装,内容类型为application/pdf
- 临时数据和字体路径可以位于Symfony应用文件夹树内部或外部
安装
步骤 1: Composer
将以下require行添加到composer.json
文件中
{ "require": { "bushidoio/pdf-bundle": "dev-master" } }
并使用Composer在您的项目中实际安装它
php composer.phar install
您也可以使用此命令一步完成
$ php composer.phar require bushidoio/pdf-bundle "dev-master"
步骤 2: 启用组件
在内核中启用组件
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new BushidoIO\PDFBundle\BushidoIOPDFBundle(), ); }
配置
可以在app/config/config.yml
中配置临时内容文件夹。默认情况下,tmp和ttffontdatapath文件夹将存储在app/cache
中。请确保您在这两个文件夹上都有写权限。
bushidoio_pdf: tmp: ~ ttffontdatapath: ~
使用示例
您可以使用bushidoio_pdf
服务将任何HTML字符串转换为PDF
public function indexAction() { ... $PDFService = $this->get('bushidoio_pdf'); $html = '...'; $pdf = $PDFService->createPDFFromHtml($html); ... }
您可以使用Twig模板或您喜欢的任何东西来创建HTML字符串
public function indexAction() { ... $PDFService = $this->get('bushidoio_pdf'); $html = $this->get('twig')->render( 'default/index.html.twig', array( 'greeting' => 'Hi' ) ); $pdf = $PDFService->createPDFFromHtml($html); ... }
使用createResponse
方法返回一个Symfony\Component\HttpFoundation\Response
对象,内容类型为application/pdf
,如果在控制器操作中返回,则会直接下载
public function indexAction() { $PDFService = $this->get('bushidoio_pdf'); $html = $this->get('twig')->render( 'default/index.html.twig', array( 'greeting' => 'Hi' ) ); return $PDFService->createResponse($html); }