Question

I have searched for creating pdf file in symfony 2.3 but was not successful. I've got 2 bundle

  1. Knp snapy bundle and another is
  2. psliwa / PHPPdf

My task is just download pdf file on click. For this I have given the link in html.twig like

<a href="{{path('route name')}}">Download file</a>

In pdf action I am generating the PDF file

In knp snapy bundle I am doing:

$html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
    'some'  => $vars
));

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

And got error

The exit status code '1' says something went wrong: stderr: "The system cannot find the path specified.

Is wkpdftohtml necessary for installation if YES then how can I install on sharing based hosting.

In psliwa / PHPdf I have read the example from:

and got

unable to find twig file

If I change the $format = $this->get('request')->get('_format'); to $format='pdf'; then it show simple html file.
Unable to understand what should I do for completion of task...

Was it helpful?

Solution 2

This is an excerpt from a controller in a live shared host environment using psliwa/PHPPdf:

    $facade = $this->get('ps_pdf.facade');
    $response = new Response();
    $this->render('ManaClientBundle:Contact:roster.html.twig', array(
        'date' => $found['latestDate'],
        'center' => $location,
        'roster' => $found['contactSet'],
            ), $response);
    $date = new \DateTime($found['latestDate']);
    $filename = str_replace(' ', '', $location) . date_format($date, '_Ymd') . '.pdf';
    $xml = $response->getContent();
    $stylesheet = $this->renderView('ManaClientBundle:Contact:contact.xml.twig', array());
    $content = $facade->render($xml, $stylesheet);
    return new Response($content, 200, array
        ('content-type' => 'application/pdf',
        'Content-Disposition' => 'attachment; filename=' . $filename
    ));

OTHER TIPS

Yes. For Knp Snappy Bundle, wkhtmltopdf is required and you need to configure it properly in the config.yml

knp_snappy:
pdf:
    enabled:    true
    binary:     /usr/local/bin/wkhtmltopdf #path to wkhtmltopdf binary
    options:    []
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top