Open PDF from FPDF in new tab

20,294

Solution 1

You should create the new tab before running the FPDF code.

Alternative you can save the pdf as a file and open a new tab with the correct header. see this question: Show a PDF files in users browser via PHP/Perl

The code terminates with output by design, unless you save it to file or string.

$pdf->Output($filename,'F');

If you could elaborate on what you want to do after the output i might be able to help more.

Solution 2

Here is what we are doing and some thoughts:

  1. The Output() method takes 2 arguments, name and dest. You are sending an array in for the name parameter, probably not what you want. The second, dest, will use the "D" as you have specified.
  2. Output() sends headers and the data depending on the value you specify for dest. See below.

What that means is if you want to continue executing code, you are likely going to need to separate out the logic that generates this PDF into a new page, open that in the new tab using target="_new" like you were thinking, which then prompts the user to download or in that case you can use the "I" value and open it within the browser.

Output() from fpdf.php [lines 999-1036]:

switch($dest)
    {
        case 'I':
            // Send to standard output
            $this->_checkoutput();
            if(PHP_SAPI!='cli')
            {
                // We send to a browser
                header('Content-Type: application/pdf');
                header('Content-Disposition: inline; filename="'.$name.'"');
                header('Cache-Control: private, max-age=0, must-revalidate');
                header('Pragma: public');
            }
            echo $this->buffer;
            break;
        case 'D':
            // Download file
            $this->_checkoutput();
            header('Content-Type: application/x-download');
            header('Content-Disposition: attachment; filename="'.$name.'"');
            header('Cache-Control: private, max-age=0, must-revalidate');
            header('Pragma: public');
            echo $this->buffer;
            break;
        case 'F':
            // Save to local file
            $f = fopen($name,'wb');
            if(!$f)
                $this->Error('Unable to create output file: '.$name);
            fwrite($f,$this->buffer,strlen($this->buffer));
            fclose($f);
            break;
        case 'S':
            // Return as a string
            return $this->buffer;
        default:
            $this->Error('Incorrect output destination: '.$dest);
    }
Share:
20,294
Deep
Author by

Deep

Updated on July 09, 2022

Comments

  • Deep
    Deep almost 2 years

    I have a process where customer clicks if he wants a report to be generated and downloaded while creating a business unit. This report will be in pdf format.

    Currently I am using Codeigniter and I use FPDF to generate pdf files.

    The pdf opens well when it is requested. But

    Problem: 1) PDF opens in the same tab. I would like the pdf to open up in new tab which I am kind of thinking how. "_target" will help me open the pdf in new tab if it is a pdf link. But here it is server side generated pdf. Hence "_target" will not work so I am looking for alternative on this.

    2) After the pdf generates, the next line of code is not read. The execution actually stops here. I would like to know how I can make the process continue even after outputting pdf file.

    Example

    $pdf->Output($exampleArray, 'D'); // exampleArray carries all data to PDF and helps output the pdf and D forces FPDF to download PDF rather than opening it. Instead of 'D' I can use 'I' but that will output the pdf in same tab.
    
    $this->continueNextFunction(); // This function should run and open the views in it.
    

    From the above example I would love to see either PDF downloaded or 'opened in new tab' followed by next line executed helping the page to redirect and open required views.

    Also please let me know if further explanation is required. I tried my best to explain the situation here. I had looked on this over google but I have not really found any solution on this.

    Any help regarding this will be greatly appreciated.