chrisandchris / mpdf-port-bundle
1.2.6
2015-04-13 08:01 UTC
Requires
- mpdf/mpdf: ~5.7
- symfony/framework-bundle: ~2.1
Requires (Dev)
README
使用Composer(Symfony 2.1+)
- 在您的
composer.json
文件中添加新行
"require": {
...
"tfox/mpdf-port-bundle": "1.2.*"
}
- 运行命令
php composer.phar update
- 在
app/AppKernel.php
中添加新行
$bundles = array(
...
new ChrisAndChris\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 ChrisAndChris\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();
警告
- 默认情况下,此包将'utf-8'和'A4'两个属性添加到mPDF类构造函数中。要关闭这些选项,请使用
setAddDefaultConstructorArgs
方法
$mpdfService->setAddDefaultConstructorArgs(false);
-
由于包默认将前两个参数插入到mPDF构造函数中,因此额外的构造函数参数应从第三个参数(默认字体大小)开始。
-
如果调用了
setAddDefaultConstructorArgs(false)
方法,构造函数的额外参数应从第一个参数(模式)开始。
额外参数
由于包使用mPDF类的方法,可以添加一些额外的参数到这些方法中。包中使用了3个mPDF方法
- 构造函数。文档:http://mpdf1.com/manual/index.php?tid=184
- WriteHTML。文档:http://mpdf1.com/manual/index.php?tid=121
- Output。文档:http://mpdf1.com/manual/index.php?tid=125
要传递额外的参数,应创建一个包含参数的数组
$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
);
不需要在数组中包含所有键。此数组可以作为第二个参数传递给generatePdf
和generatePdfResponse
方法
$mpdfService->generatePdf($html, $arguments);
$mpdfService->generatePdfResponse($html, $arguments);