Jasshh Andrews
Jasshh Andrews

Reputation: 175

Codeigniter force_download() does not work for PDF extensions

I'm facing two problems.

  1. force_download() does not work for pdf extensions.

when I try to download a txt extension file the functions work fine below code.

public function index()
    {
        $data = "some text";
        $name = "sample.txt";
        force_download($name,$data);
    }

but when I try to do it with a pdf extension, the file gets downloaded properly but when I click the downloaded pdf file it shows Error: Failed to load PDF document.

public function index()
        {
            $data = "some text for pdf";
            $name = "sample.pdf";
            force_download($name,$data);
        }
  1. secondly is there a way to ask or show a dialogue box before downloading a file instead of force download.

Upvotes: 0

Views: 1916

Answers (2)

Amir Khan
Amir Khan

Reputation: 441

pass name of the file you want to download

Controller

function download($file_name)
{
    $this->load->helper('download');
    force_download('./assets/'.$file_name, NULL);
}

View

<a href="<?php echo SURL.'controller_name/download/'.$file_name;?>">Download
</a>

For second question try onclick()

Upvotes: 0

Vickel
Vickel

Reputation: 7997

You cannot create a (not currupt) pdf file with force_download(). You'll need to create the pdf file using a php library like fpdf or similar. The force_download() function only generates a header which forces a download to happen.

If the pdf file exists on the server you can force_download('/path/to/my_pdf.pdf', NULL);

concerning the second part of your question, this is too broad, there are thousands of ways to create a dialog box

Upvotes: 3

Related Questions