MicWit
MicWit

Reputation: 685

Including fonts for spread PDF generator bundle in Symfony

I have a Symfony project that I need to export PDF code (stored in the database and then downloaded with header later) that contains images, text etc. I can't really install anything on the web servers, so have not looked at KnpSnappyBundle, but am using the Spread PDF generator Bundle (https://github.com/stedekay/SpraedPDFGeneratorBundle).

I can get it to create a pdf in the controller:

$html = $this->render('templates/test1.html.twig');
$pdfGenerator = $this->get('spraed.pdf.generator');
return new Response($pdfGenerator->generatePDF($html),
    200,
    array(
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="out.pdf"'
    )
);

From the twig file:

<html>
<head>
    <title>test</title>
    <style>
        @font-face {
            font-family: 'Roboto';
            src: url({{ absolute_url(asset('css/fonts/Roboto/Roboto-Medium.ttf')) }}) format('truetype');
        }

        h1 {
            font-family: Roboto;
            font-size: 100px;
        }
    </style>
</head>
<body>
    <h1>test</h1>
</body>
</html>

But it always shows Times New Roman as the font! Even when I choose something as common as Arial.

How can I get the font to embed? Or is there another bundle for Symfony that will create a PDF from html that doesn't need anything installed on the server?

Upvotes: 1

Views: 1339

Answers (1)

MicWit
MicWit

Reputation: 685

I am now using the Knp Snappy Bundle https://github.com/KnpLabs/KnpSnappyBundle that extends wkhtmltopdf. To get around having to manually install anything on the server, I am using https://github.com/zendre4/wkhtmltopdf-amd64 that will include the binary when you do your composer install (I did not use the project this is forked from as it is stale).

Then in your config (generally /app/config/config.yml) add:

knp_snappy:
  pdf:
    enabled:    true
    binary:     %kernel.project_dir%/vendor/bin/wkhtmltopdf-amd64
    options:    []

The composer install will add a vendor (so the binary is not included in your project repository) that will create a symlink to the binary in vendor/bin. This config will then tell knp_snappy where to find this binary, so everything for your deployment is taken care of in your composer install (or update).

These are brief instructions, if anyone requires further details add a question in a comment and I will update this answer.

Upvotes: 0

Related Questions