Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: CodeIgniter 4.0.3 Controller will not display pdf files in browser #3144

Closed
InsiteFX opened this issue Jun 21, 2020 · 3 comments
Closed
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@InsiteFX
Copy link
Contributor

InsiteFX commented Jun 21, 2020

Direction
We use github issues to track bugs, not for support.
If you have a support question, or a feature request, raise these as threads on our
forum.

Describe the bug
Forum reference: https://forum.codeigniter.com/thread-76768-post-377020.html#pid377020

The controller will not display pdf files in the wen browser using php headers.
` // Store the file name into variable
// The location of the PDF file on the server
$file = WRITEPATH . 'pdf/CodeIgniterAjaxPagination.pdf';
$fileName = 'CodeIgniterAjaxPagination.pdf';

	// Header content type
	header('Content-type: application/pdf');
	header('Content-Disposition: inline; filename="' . $fileName . '"');
	header('Content-Transfer-Encoding: binary');
	header('Content-Length: ' . filesize($file));
	header('Accept-Ranges: bytes');

	// Read the file
	readfile($file);`

Displays garbage characters.

CodeIgniter 4 version
Which version (and branch, if applicable) the bug is in.

CodeIgniter 4.0.3 Dev

Affected module(s)
Which package or class is the bug in, if known.

Something to do with the Controller were the code is ran from.

Expected behavior, and steps to reproduce if appropriate
A clear and concise description of what you expected to happen,
and how you got there.

It should have displayed the pdf file in the web browser chrome newest version.
Works file using straight php code script and loading form the folder directly into chrome.
.
Feel free to include a text/log extract, but use a pastebin facility for any
screenshots you deem necessary.

Context

  • OS: [e.g. Windows 10 x64]
  • Web server [e.g. Apache 2.4.3]
  • PHP version [e.g. 7.4.7]
@InsiteFX InsiteFX added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 21, 2020
@michalsn
Copy link
Member

We should use the Response class to set headers.

$this->response->setHeader('Content-Type', 'application/pdf');

@jorge-koki
Copy link

$content = file_get_contents($ruta);
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($content));
header('Content-Disposition: inline; filename="' . $file . '"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
ini_set('zlib.output_compression', '0');
die($content);

@InsiteFX
Copy link
Contributor Author

As mentioned above use:

$this->response->setHeader('Content-Type', 'application/pdf');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants