mehdi
mehdi

Reputation: 147

Symfony2 Knp-snappy to generate PDF from a twig view that contain an image

I want to generate a PDF file from a twig view that contain an image. this is my Controller:Action :

public function generatePdfAction(Request $request)
    {
       // initialize the $emp variable
        $html = $this->renderView('PFEEmployeesBundle:Employee:view.html.twig',
            array('employee'=> $employee)
        );

        return new Response(
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
            200,
            array(
                'Content-Type'          => 'application/pdf',
                'Content-Disposition'   => 'attachment; filename="file.pdf"'
            )
        );
    }

this is the configuration in the config.yml :

knp_snappy:
    pdf:
        enabled:    true
        binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""
        options:    []
    image:
        enabled:    true
        binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\""
        options:    []

The code generates a pdf file including the CSS. I have two problems, the first is the image not displayed in the pdf file. the second is how to render not the whole view but just a block of the twig view ( the view extends an other).

Upvotes: 2

Views: 4165

Answers (2)

Rajesh Vasani
Rajesh Vasani

Reputation: 397

try this.

<img src="{{ app.request.scheme ~'://'~ src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" />

Upvotes: 0

bastien
bastien

Reputation: 190

I have two problems, the first is the image not displayed in the pdf file

All assets must be called in absolute URL:

 <img src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" />

Upvotes: 1

Related Questions