1

I would like to open the pdf file in the window browser but I have "The file "\\servername\20\2016080.pdf" does not exist" If I copy this path in a browser, it's work.

Edit: I have found in the logs

CRITICAL - Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: "The file "\\servername\20\2016080.pdf" does not exist" at C:\wamp64\www\his\vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\File\File.php line 37

Thank you.

$response = new BinaryFileResponse($result = $ServerModel->getDocument($request-> get('id'));
$response->headers->set('Content-Type', 'application/pdf');
return $response; 
8
  • have you already tried this stackoverflow.com/a/28920003/2270041 ? Commented Jan 30, 2018 at 9:05
  • Are you sure you don't need a RedirectResponse instead? Commented Jan 30, 2018 at 9:05
  • I already tried it, I have the same error. No, I don't need it. thank you. Commented Jan 30, 2018 at 9:15
  • is this a typo or a copy paste error? $request-> get('id') Commented Jan 30, 2018 at 9:16
  • Copy past the path also no works and I don't understand typo. Commented Jan 30, 2018 at 9:19

2 Answers 2

1

If you're using symfony 3.2 or later (which you should be), you can use the new file helper to serve binary files.

from the symfony docs

$pdfPath = $this->getParameter('dir.downloads').'/sample.pdf';

return $this->file($pdfPath);

how you go about getting the path of the file may differ depending on your implementation. But if its a straight SplFileInfo:: object php docs then you can just use:

$file->getPathname();

The file helper will automagically do much of heavy lifting for you.

Make sure the file is accessible, either by a route or by an un-firewalled path.

Sign up to request clarification or add additional context in comments.

4 Comments

your path is not accessible by the looks of it. Whats the actual path of the file?
I don't have access.
if you dont have access how do you think you can download it?
I don't have access to the server but I have access to the network share.
1
/**
* @Route("/show-pdf", name="show-pdf")
*/
public function showPdf(Request $request) {

    ini_set('display_errors', 'On');

    $pdf = file_get_contents('path/to/file.pdf');

    return new Response($pdf, 200, [
                'Content-Type' => 'application/pdf',
                'Content-Disposition' => 'inline; filename="file.pdf"'
    ]);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.