Welcome Guest, Not a member yet? Register   Sign In
pdf download wont work
#7

(This post was last modified: 06-21-2020, 08:50 AM by InsiteFX.)

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
pdf download wont work - by badger - 06-18-2020, 01:20 AM
RE: pdf download wont work - by InsiteFX - 06-18-2020, 04:51 AM
RE: pdf download wont work - by badger - 06-18-2020, 06:52 AM
RE: pdf download wont work - by InsiteFX - 06-18-2020, 07:26 AM
RE: pdf download wont work - by badger - 06-21-2020, 02:20 AM
RE: pdf download wont work - by InsiteFX - 06-21-2020, 06:13 AM
RE: pdf download wont work - by InsiteFX - 06-21-2020, 07:58 AM
RE: pdf download wont work - by badger - 06-21-2020, 09:48 AM
RE: pdf download wont work - by InsiteFX - 06-21-2020, 12:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB