symfony2 download file The Response content must be a string

969 views Asked by At

i want download my pdf file where he's saved in my phpmyadmin 'blob'

i try this code:

public function downloadAction()
{
    $ma_base_de_donne = new myprojectdbEntity();

    $item = $this->getDoctrine()->getRepository('myprojectBundle:myprojectdbEntity')->find(5);
    if (!$item) {
        throw $this->createNotFoundException("File with ID 5 does not exist!");
    }
    $pdfFile = $item->getFichier(); //returns pdf file stored as mysql blob
    //$headers = array('Content-Type' => 'application/pdf', 'Content-Disposition' => "attachment; filename=" . urlencode($pdfFile));
    $response = new Response($pdfFile, 200, array('Content-Type' => 'application/pdf'));
    //return $response;
    var_dump($response); die();
}

and i get this error :

The Response content must be a string or object implementing __toString(), "resource" given.

help me please

thanks first

1

There are 1 answers

0
Cristian Bujoreanu On

Define a __toString() method in your entity object($pdfFile):

public function __toString() {
    return $name;
}

Or send directly the string in your response:

$response = new Response($pdfFile->getName(), 200, array('Content-Type' => 'application/pdf'));