forci/pdf-generator-bundle

基于 WKHTMLTOPDF 的 PDF 生成器

安装数: 6,105

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 3

分支: 0

公开问题: 0

类型:symfony-bundle

v1.0.1 2024-05-03 21:43 UTC

This package is auto-updated.

Last update: 2024-09-03 22:32:29 UTC


README

基于 wkhtmltopdf 的 PDF 生成器 Bundle

需求

  • 需要在您的服务器上安装 xvfb(在 Debian/Ubuntu 上使用 apt-get install xvfb 命令)

默认情况下,此 Bundle 使用 h4cc/wkhtmltopdf-i386 包的二进制文件。要使用系统二进制文件,请将其添加到您的 parameters.yml.dist 文件中,并执行 composer install

    forci_pdf_generator.binary: wkhtmltopdf

在您的 AppKernel 中注册此 Bundle

<?php
public function registerBundles() {
    $bundles = [
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        // Add ForciPdfGeneratorBundle to your AppKernel
        new \Forci\Bundle\PdfGenerator\ForciPdfGeneratorBundle(),
    ];
}

基本用法

<?php 

use \Forci\Bundle\PdfGenerator\Generator\PdfResult;

/** @var \Forci\Bundle\PdfGenerator\Generator\PdfGenerator $generator */
$generator = $container->get('forci_pdf_generator.generator');
$filename = 'someFile.pdf';
$html = 'someHtmlString';
// Get a PdfResult. The wkPrint and bootstrap methods both return a PdfResult
/** @var \Forci\Bundle\PdfGenerator\Generator\PdfResult $result */
$result = $generator->wkPrint($html);
// This will NOT cleanup on KernelEvents::TERMINATE
$result = $generator->wkPrint($html, false);
// The PdfResult is the result of the PDF generation. It has access to the temporary PDF file
$tempPdfPath = $result->realPath();
// return a Symfony BinaryFileResponse
return $generator->bootstrap($html)->response($filename);
// return a BinaryFileResponse, 500 on error
return $manager->wkPrint($html)->response($filename, PdfResult::RESPONSE_ON_ERROR_500_RESPONSE);
// return a Response, 500 on error. This will read the whole file into memory and set it to the newly created Response object
return $manager->wkPrint($html)->response($filename, PdfResult::RESPONSE_ON_ERROR_500_RESPONSE | PdfResult::RESPONSE_TYPE_NORMAL);
// return a Symfony Response and copy the file some place else
// The Generator does NOT save the files; it will unlink them as soon as the request is finished
// The copy() method returns PrintResult
return $generator->bootstrap($html)->copy('/some/location/someFile.pdf')->response($filename);
// And last, you can also get the PDF file contents as string
$contents = $generator->bootstrap($html)->contents();

待办事项