obtao/html2pdf-bundle

此扩展包允许您在 Symfony2 项目中生成 PDF 文件。

安装数: 7,295

依赖关系: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 1

公开问题: 0

类型:symfony-bundle

dev-master 2014-02-06 10:09 UTC

This package is not auto-updated.

Last update: 2024-09-24 05:10:48 UTC


README

此扩展包使得在 Symfony2 项目中生成 PDF 文件变得非常简单(甚至有趣?)。此扩展包基于 HTML2PDF 库的 4.03 版本,该库使用 TCPDF 库的 5.0.002 版本,链接如下:HTML2PDF 库TCPDF 库

扩展包比较重,但它已经包含了重型的 HTML2PDF 和 TCPDF 库。如果您可以在服务器上安装任何您想要的库,我们建议您使用 KnpSnappyBundle,它轻便且易于使用,但需要在您的服务器上安装 wkhtmltopdf 库。

此外,HTML2PDF 库 并非完美,某些 CSS 规则不受支持。因此,在可能的情况下,请考虑使用优秀的 KnpSnappyBundle。

Build Status

使用 Composer 安装

将以下内容添加到您的 composer.json 文件中

{
    "require" :  {
        "obtao/html2pdf-bundle": "master-dev"
    }
}

使用方法

然后像平常一样在 app/AppKernel.php 中注册它

  // ...
  new Obtao\Bundle\Html2PdfBundle\ObtaoHtml2PdfBundle(),
<?php

// in a controller
public function generatePdfAction(){

	// ... some code

	$content = $this->renderView('ObtaoSomeBundle:Pdf:template.html.twig');
	$pdfData = $this->get('obtao.pdf.generator')->outputPdf($content);

	/* You can also pass some options.
	   The following options are available :
	   		protected $font = 'Arial'
			protected $format = 'P'
			protected $language = 'en'
			protected $size = 'A4'
	   Here is an example to generate a pdf with a special font and a landscape orientation
	*/
	$pdfData = $this->get('obtao.pdf.generator')->outputPdf($content,array('font'=>'Georgia','format'=>'L'));

	$response = new Response($pdfData);
	$response->headers->set('Content-Type', 'application/pdf');

	return $response;
}

HTML2PDF 库需要遵守一些规则

  • 无标签
  • 标签应替换为(该扩展包将处理它)
  • 样式需要直接在页面中定义(并且不支持所有 CSS 规则...)
  • 需要调整资源路径(见下文)

以下是一个示例

<page>
	<style type="text/css">
		#hello{
			color: #002549;
			font-family: Helvetica, sans-serif;
			text-align:center;
			font-size: 40px;
		}
	</style>

	<div id="hello">

		<div style="position:absolute;top:0;left:0;z-index:1;opacity:0.6;">
			Hello you!
			<img style="opacity:0.6;" src="{{ web_path~asset('/bundles/obtaosomebundle/images/greet.png') }}" alt="background" />
		</div>

	</div>
</page>

注意:web_path 变量在 app/config/config.yml 文件中定义

twig:
    globals:
        web_path: %kernel.root_dir%/../web

鸣谢

Html2PdfBundle最初由Obtao开发。