Using Symfony2.3.4
I'm generating PDF docs with this bundle: SpraedPDFGeneratorBundle and this is its main php class(I think):PDFGenerator.php
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>) */);
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...
Has anyone ever worked with this bundle or some like it; if yes can you please help me?
Alright I found the answer.
You are asking two things: how to add images and how to add new fonts.
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 %}
And you use it like this
{% import '::imgurimacro.html.twig' as img %}
...
<img src="{{ img.image('img/MyImage.png') }}" />
And it should work.
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"
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'));
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.
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') }};
}
Now whenever you write
<p style="font-family: MyFontFamily">Hello World!</p>
It should print "Hello World!" using the registered font in the pdf.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With