使用CSS和图像生成PDF [英] Generate PDF with CSS and Images
问题描述
使用Symfony2.3.4
Using Symfony2.3.4
我使用此捆绑包生成PDF文档: SpraedPDFGeneratorBundle ,这是它的主要php类(我认为): PDFGenerator.php < a>
I'm generating PDF docs with this bundle: SpraedPDFGeneratorBundle and this is its main php class(I think):PDFGenerator.php
我设法生成它们很好,但我不知道如何设置fontPath参数生成一个css修改的PDF与其中的一些图像。
这里:
I managed to generate them just fine but I don't know how to set the fontPath parameter for generating a css-modified PDF with some images in it too. Here:
///XController.php
$pdfGenerator = $this->get('spraed.pdf.generator');
$pdf = $pdfGenerator->generatePDF($postData/* ,
'UTF-8', array(<SOME_PATH_I_GUESS>) */);
第一个参数是要输出到PDF的html数据,第二个我猜猜但我不确定,第三个是fontPath。
the first param is the html data to be output to the PDF, the second one I kinda guessed it but I'm not sure and the third is the fontPath.
图片的东西也是我想要的提示。
The image thing is also something I would appreciate tips about...
捆绑或一些喜欢它;
推荐答案
好的,我找到了答案。
您要问两件事:如何添加图片和如何添加新字体。
这很简单。假设您使用的是Twig,您需要执行以下操作:
This is pretty simple. Assuming you're using Twig, you would need to do the following:
<img src="{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset("img/MyImage.png") }}" />
当然,你也可以使这个宏成为宏,像这样。
Of course you can also make this a macro so you don't repeat yourself all over the place, like this.
{% macro image(uri) %}
{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset(uri) }}
{% endmacro %}
$ b b
这样使用它
And you use it like this
{% import '::imgurimacro.html.twig' as img %}
...
<img src="{{ img.image('img/MyImage.png') }}" />
它应该可以工作。
这个有点复杂。首先确保您拥有最新的Spraed PDF Generator版本。在composer.json中:
This one is a bit more complex. First make sure you have the latest Spraed PDF Generator version. In composer.json:
"spraed/pdf-generator-bundle": "dev-master"
每当您必须将字体添加到新的PDF时,请执行以下操作:
Whenever you must add the fonts to your new PDF, do the following:
$pdf = $pdfGenerator->generatePDF($postData , 'UTF-8', array('C:\Windows\Fonts\Font1.ttf', 'C:\Windows\Fonts\Font2.ttf'));
接下来,创建一个 fonts
web
文件夹(或放置您的资源的任何位置)并在其中添加.ttf文件。
Next, create a fonts
folder (or any name of your choice) in the web
folder (or wherever you're placing your assets) and add the .ttf files there.
在您的Twig文件中,添加以下作为CSS样式:
In your Twig file, add the following as a CSS style:
@font-face {
font-family: "MyFontFamily";
src: {{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('fonts/Font1.ttf') }};
}
无论何时写
<p style="font-family: MyFontFamily">Hello World!</p>
它应该打印Hello World!使用pdf中的注册字体。
It should print "Hello World!" using the registered font in the pdf.
这篇关于使用CSS和图像生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!