phpfpdf

I am using PHP FPDF to convert HTML to PDF. Can I use fpdf output() parameters 'I' and 'D' simultaneously?


I am collecting form data and generating output to PDF file using PHP and fpdf. I want to directly open generated PDF file in browser and also save pdf file automatically to client side. First I can achieve by using "I" parameter in output() command and later by using "D" parameter. Can I use both commands at the same time so that pdf file will be saved automatically and also user will instantly see PDF file in browser?

Note: I don't want to force additional steps to user such as saving PDF or opening downloaded PDF. Both should be done automatically.

I am able to use Output() "F" and "I" parameters at the same time, but "F" saves file to server side.

I tried following:

file = $customer.'.pdf';
$pdf-> output($file,'D');
$pdf-> output($file,'I');

And also:

file = $customer.'.pdf';
$pdf-> output($file,'I');
$pdf-> output($file,'D');
    

In both cases first line is executed and not later.


Solution

  • To understand why this doesn't make sense, you have to understand a bit about how the web works:

    Note that it is not possible for the server to send two responses for one request, or to explicitly command actions on the browser, only send suggestions for what to do with the response.

    In the case of FPDF, both the 'D' and 'I' options are sending the content of the PDF as the body of the response. The only difference between them is that they send different headers - specifically, a Content-Disposition header of either "inline" (suggesting that the browser display the content direcly) or "attachment" (suggesting that the browser save the file to a local directory, or show a "Save As..." dialog, as well as a suggested file name).

    While there might be some complex trickery you could do with client-side JavaScript, you are best off choosing one or the other: