phpsymfonypdf-generationsymfony2

Show mysql blob pdf file in Symfony 2.0


I have a link:

{% for item in list %}
    ...
    <a href="{{ path('show', { 'id': item.id }) }}"> read pdf file</a>
{% endfor %}

When the user clicks the link I would like to show the pdf file (file stored as a blob in mysql). The code below is not correct, but I want my action to do something like following.

/**
* @Route("/show", name="show")
*/
public function showAction()
{
    $id = $this->get('request')->query->get('id');
    $item = $this->getDoctrine()->getRepository('MyDocsBundle:Files')->file($id);
    $pdfFile = $item->getFile(); //returns pdf file stored as mysql blob
    $response = new Response();
    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/pdf');
    $response->setContent($pdfFile);
    $response->send() //not sure if this is needed
    return $response;
}

Solution

  • I'm not sure Doctrine has a blob type natively, so I'll assume you've got it setup to properly store the file as an actual BLOB in the database.

    Try something more along the lines of this...

    /**
    * @Route("/show/{id}", name="show")
    */
    public function showAction($id)
    {
        $item = $this->getDoctrine()->getRepository('MyDocsBundle:Files')->find($id);
        if (!$item) {
            throw $this->createNotFoundException("File with ID $id does not exist!");
        }
    
        $pdfFile = $item->getFile(); //returns pdf file stored as mysql blob
        $response = new Response($pdfFile, 200, array('Content-Type' => 'application/pdf'));
        return $response;
    }