bushidoio/pdf-bundle

BushidoIOPDFBundle

安装: 45

依赖: 0

建议者: 0

安全: 0

星星: 1

关注者: 1

分支: 0

类型:symfony-bundle

v2.0.0 2018-02-17 21:10 UTC

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);
}