CodeIgniter Forums
pdf download wont work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: pdf download wont work (/showthread.php?tid=76768)



pdf download wont work - badger - 06-18-2020

This was already mentioned under c4 addins->pdf customized and no solution was offered.
Chrome and Firefox can download pdfs (generated from adobe and others) to my download folder but both refuse to display in the browser,
saying the file cant be downloaded. The files display perfectly using ci3 with setheaders and force_download.
I've tried all combinations of the code below and failed. Can anybody help? The project has come to a halt because I cant print.
Code:
return $this->response
        //->setHeader('Pragma','public')
        //->setHeader('Expires','0')
        //->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
        //->setHeader('Content-Disposition','inline; filename="'.$filename.'"')
        //->setContentType('application/pdf')
        //->setHeader('Content-Length',filesize($fullname))
        ->download($fullname);//'',true);//,$data);
Many thanks, Bill


RE: pdf download wont work - InsiteFX - 06-18-2020

Read this:

Show a PDF files in users browser via PHP/Perl


RE: pdf download wont work - badger - 06-18-2020

%PDF-1.5 %���� 1 0 obj <>>> endobj 2 0 obj <> endobj 3 0 obj <>/XObject<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.28 842.01] /Contents 4 0 R/Group<>/Tabs/S>> endobj 4 0 obj <> stream x��[Ys�F~W���<���9���kK���+��]�V �`��T@*ǿ߯{�@�e�%^��������||�3i-��F�Ҙ(N�����(��w_���ѓ��ӯ0�F1�/>I�&

no luck. The exact same code has worked since 2 years and continues to work perfectly in ci3 on this same computer and others as well but this current project using ci4 just refuses to work. i've now no hair left to tear out 8-(
Bill


RE: pdf download wont work - InsiteFX - 06-18-2020

I'll check the code later to day and see if I can find anything.


RE: pdf download wont work - badger - 06-21-2020

is there anybody anywhere who can help me with this problem. the project is an accounting package and cannot work unless i can show pdfs in the browser. the current project using ci3 has no problem doing it but despite all my efforts i can't get it to work with ci4.
many thanks, Bill


RE: pdf download wont work - InsiteFX - 06-21-2020

NOTE: both code bases were using php header.

I took all morning testing this on my system here with Google Chrome (latest) and it doe's the
same thing here displaying garbage using CodeIgniter latest!

So I took and tried it with straight php and it works fine, it also works fine running from the file folder.

So I have created a bug report on this.


RE: pdf download wont work - InsiteFX - 06-21-2020

Ok took me a little while but I got it to work like below.

PHP Code:
    // -------------------------------------------------------------------

    /**
     * displayPdf ()
     * -------------------------------------------------------------------
     *
     *
     */
    
public function displayPdf()
    {
        
// 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
        
$this->response->setHeader('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);
    } 

So it looks like we just needed to use CodeIgniters 
PHP Code:
$this->response->setHeader('Content-Type''application/pdf'); 

At that's it.

This one I could not get to work with CodeIgniter for some reason.

PHP Code:
header('Content-Disposition: inline; filename="' $fileName '"'); 

Figured it out this is how to display a pdf file using CodeIgniter 4

PHP Code:
$this->response->setHeader(); 

Here is the new method on how it is done and working, you will need to change the
top two lines to you pdf path and filename.

PHP Code:
    // -------------------------------------------------------------------

    /**
     * displayPdf ()
     * -------------------------------------------------------------------
     *
     * Displays a pdf file into the users web browser using php.
     */
    
public function displayPdf()
    {
        
// Store the path location of the PDF file on the server into a php property.
        
$file     WRITEPATH 'pdf/CodeIgniterAjaxPagination.pdf';

        
// Store the file name into php property.
        
$fileName 'CodeIgniterAjaxPagination.pdf';

        
// Set header content type.
        
$this->response->setHeader('Content-Type''application/pdf');
        
$this->response->setHeader('Content-Disposition: inline;''filename="' $fileName '"');
        
$this->response->setHeader('Content-Transfer-Encoding''binary');
        
$this->response->setHeader('Content-Length: 'filesize($file),'');
        
$this->response->setHeader('Accept-Ranges:''bytes');

        
// Read the file.
        
readfile($file);
    } 

Tested an working on my Windows 10 Pro x64 Build 2004


RE: pdf download wont work - badger - 06-21-2020

Wow, you have no idea how much I appreciate the work you have put into solving this. I just couldn't make any progress and since pdf is so important it looked as if the project would have to be shelved until some time in the future. Very many thanks indeed, you're a gentleman and a scholar.
Bill


RE: pdf download wont work - InsiteFX - 06-21-2020

Michal Sniatala in bug report gave me the answer to use CodeIgniter setHeader() but I had trouble getting the
other headers to work.

Many thanks Michal.