Reputation: 391
I have a app made with Symfony2 and in my twig template, I show a table with some pdf files.
This pdf files (one for user) are stored in /app/var/pdf/xxx.pdf
.
If I use:
<a href="{{ 'entity.pdf' }}">PDF</a>
My path is correct, for example: symfony/app/var/pdf/123123.pdf
, but when I click in the link, my browser return a 404 Not Found error. Obviusly I have checked that the file is stored in this path.
Any help?
Thanks in advance.
Upvotes: 3
Views: 10652
Reputation: 590
Alternative :
<a download href="{{ asset('/images/CV.pdf') }}" >Télécharger mon CV <i class="fa fa-download"></i></a>
Upvotes: 0
Reputation: 1680
You can use absolute URL like below
<a href="{{ absolute_url(asset('uploads/YOURPATCH/'))}}pdf_download" download>
Download
</a>
Upvotes: 0
Reputation: 432
To force download the pdf file try this in the controller.
/**
* @Route("/download/{id}",name="pdf_download")
*/
public function downloadAction($id) {
$downloadedFile = $repository->findOneBy(
array(
'id' => $id,
)
);
$response=new Response();
$response = new Response();
$response->headers->set('Content-type', 'application/octet-stream');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $downloadedFile->getFilename() ));
$response->setContent(file_get_contents($downloadedFile->getAbsolutePath()));
$response->setStatusCode(200);
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');
return $response;
}
And in the template
<a href={{path('pdf_download',{'id':file.id})}}>{{file.filename}}</a>
Upvotes: 3
Reputation: 12306
You better need to store this file in public web
dir, and then create link to it like:
<a href="{{ asset('web/var/pdf/xxx.pdf') }}"/>PDF</a>
But browsers open pdf
files in new tab. And if you really want to force dowload of this file, need to use headers. Use this question for help Symfony2 - Force file download
Upvotes: 2