galmet/mpdf-port-bundle

用于封装 mPDF 类的包装器,允许在 Symfony2 项目中使用 mPDF

安装: 592

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 25

类型:symfony-bundle

1.2.2b 2014-01-22 09:53 UTC

This package is not auto-updated.

Last update: 2024-09-24 04:32:41 UTC


README

使用 Composer (Symfony 2.1+)

  • 在您的 composer.json 文件中添加一行
"require": {
		...
        
        "tfox/mpdf-port-bundle": "1.2.*"
}
  • 运行命令
php composer.phar update
  • app/AppKernel.php 中添加一行
$bundles = array(
  ...
  new TFox\MpdfPortBundle\TFoxMpdfPortBundle(),
)

使用 deps-file (Symfony 2.0.x)

  • 在您的 deps 文件中添加一个新条目
[TFoxMpdfPortBundle]
    git=https://github.com/tasmanianfox/MpdfPortBundle.git
    target=/bundles/TFox/MpdfPortBundle 
  • app/AppKernel.php 中添加一行
new TFox\MpdfPortBundle\TFoxMpdfPortBundle(), 
  • app/autoload.php 中添加一行
'TFox' => __DIR__.'/../vendor/bundles',
  • 运行命令
php bin/vendors install

快速入门指南

如何创建一个 Response 对象

以下示例创建了一个 A4 格式、纵向排列的 PDF 文档

$mpdfService = $this->get('tfox.mpdfport');
$html = "Hello World!";
$response = $mpdfService->generatePdfResponse($html);

生成包含 PDF 内容的变量

有时需要获取一个内容为 PDF 文档的变量。显然,可以从前面的示例生成一个响应,然后调用一个方法

$response->getContent()

但是有更简洁的方法来获取原始内容

$mpdfService = $this->get('tfox.mpdfport');
$html = "Hello World!";
$content = $mpdfService->generatePdf($html);

如何获取 \mPDF 类的实例

如果您想直接使用 mPDF 类,可以使用 getMpdf 方法

$mpdfService = $this->get('tfox.mpdfport');
$mPDF = $mpdfService->getMpdf();

警告

  • 默认情况下,该包会向 mPDF 类构造函数添加两个属性 'utf-8' 和 'A4'。要关闭这些选项,请使用 setAddDefaultConstructorArgs 方法
$mpdfService->setAddDefaultConstructorArgs(false);
  • 由于该包默认将前两个参数插入到 mPDF 构造函数中,因此额外的构造函数参数应从第三个参数(默认字体大小)开始。

  • 如果调用 setAddDefaultConstructorArgs(false) 方法,构造函数的额外参数应从第一个参数(模式)开始。

额外参数

由于该包使用 mPDF 类的方法,可以添加一些额外的参数到这些方法中。该包使用了 3 个 mPDF 方法

要传递额外的参数,需要创建一个包含参数的数组

$arguments = array(
	'constructorArgs' => array(), //Constructor arguments. Numeric array. Don't forget about points 2 and 3 in Warning section!
	'writeHtmlMode' => null, //$mode argument for WriteHTML method
	'writeHtmlInitialise' => null, //$mode argument for WriteHTML method
	'writeHtmlClose' => null, //$close argument for WriteHTML method
	'outputFilename' => null, //$filename argument for Output method
	'outputDest' => null //$dest argument for Output method
);

不需要在数组中包含所有键。该数组可以作为第二个参数传递给 generatePdfgeneratePdfResponse 方法

$mpdfService->generatePdf($html, $arguments);
$mpdfService->generatePdfResponse($html, $arguments);