How to display save pdf window in mdPDF ? - php

How i can show "save as" window, instead of showing generated PDF file (from mdPDF plugin) in browser window ? I try to set- $mpdf->Output($_SESSION['pdf_filename'].'.pdf', 'F');
but its just save the file on folder, not giving me a link.

I assume you are using this mPDF, correct? (Link is for the github page dedicated to the project.)
If so, I believe you need to be using the D parameter for the Output(...) call, not the F parameter. So that would change your output call to the following:
$mpdf->Output($_SESSION['pdf_filename'].'.pdf', 'D');
According to the documentation page about the Output function, the D parameter does the following:
D: send to the browser and force a file download with the name given by $filename.

Related

Chrome opening CSV file instead of save it

I have a PHP application that generates a CSV file and redirect the user to a static page linking to the file, just the example below :
https://www.example.com/public_html/static/temp/myfile.csv
Problem is, Chrome is opening the file instead of saving it. I need Chrome to save this file, as it would do with any other file like a zip or mp3, for instance.
Here is what I tried :
header('location:https://www.example.com/public_html/static/temp/myfile.csv');
header('Content-Disposition: attachment; filename=myfile.csv');
But no luck, Chrome keeps showing the myfile.csv contents instead of downloading it.
Any ideas ?
Thanks
Your argumentation in the comments has one never-ending misunderstanding: the Location header instructs any client to perform a new request to the given URI. With that the current request is over. Headers from the current request (i.e. Content-Disposition) aren't magically carried over to the next request.
In other words: your "static page linking to the file, just the example below" must send your wanted header.
Ultimately I'm sure it's not a Chrome problem either, but affects all internet browsers, as they easily detect the CSV data as text, hence being able to render/display that data instead of only being able to save it to a file.
With html5 you can set the "download" attr in an element.
Download it!
Source : http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
After struggling with this issue for some days, the only real solution i got is to ZIP the file and then redirecting to the ZIP file instead of the CSV. By doing this, Chrome will download the ZIP file instead of opening it :
header('location:https://www.example.com/public_html/static/temp/myfile.csv.zip');

Output() in fpdf does not work

I am trying to generate a pdf file containing pie charts. By i am having the following Error:
FPDF error: Some data has already been output, can't send PDF file
After fighting for about an hour I found out that it works pretty well with: Output("Report.pdf",'F'); but shows error message with: Output();.
So I am stuck here...can anyone help me with this situation ?
Try putting the following at the start of your php file:
error_reporting(E_ERROR | E_PARSE);
that will avoid E_WARNING errors to be outputted, without looking at the code for a proper fix, this could help.
You have already echoing something, so your httpresponse has already a header that says "I am a html text response", you can't modify it to say you are also a pdf response. You have to chose wether you output only html (so you profide a filename as argument of the output function so that you can generate a link to access to your pdf) or you output only pdf by removing any echo, printf or outside
Output
Output accept second parameter
I: send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string. name is ignored.

How to display pdf in browser

I am done with generating PDF file using FPDF in php. But the problem is how to open this pdf without the Save As option? I want to display the pdf document in the browser.
http://www.fpdf.org/en/doc/output.htm
Syntax: Output([string name] , string dest) , use I as Destination and fdpf will try to show it in the browser, if browser plugings and so on enable it
You cannot force this display, as it is up to the user to choose to display the PDF inline or systematically save them. I prefer the second option...
Now, there is a JavaScript / HTML 5 project (experimental!) to display PDF without plugin, so perhaps you can try that.
Even when using fpdf passing the output to the browser, I believe its still up to the user if they open or save it.
A solution would be to use some kind of PDF viewer, for example http://view.samurajdata.se/
Try this $pdf->Output('I', 'filename.pdf')
See the reference http://www.fpdf.org/en/doc/output.htm
Set header's content-type to 'application/pdf'. Then, most browsers will try to open it and show in-browser (or at least ask user to save or open file)
Your browser must have pdf plugin installed. If you havent done so install latest version of Acrobat Reader. If you are using fpdf, output the string instead of forcing download
For details
http://www.fpdf.org/en/doc/output.htm
Try echoing the PDF instead to using header function. The header function will force the browser to download. The echo 'might' show the pdf.

FPDF library "Output" doesn't work?

My problem with [FPDF] library is the next:
$pdf->Output("doc1.pdf","D");
In the above example I replaced the D with I or S or F the result is always the same: a popup appears to save or open the file.
I want to save it without a popup into a specific directory.
Have you added an header which tells the browser the output is an pdf file?
Try:
header('Content-type: application/pdf');
Just before the output.
maybe use this
$pdf->Output('D:/example2.pdf','F');
F = save to a local file with the name given by name
D:/example2.pdf = dest folder and name of file ,,,,
thats it, save it without a windows popup appears
if you read the documentation at http://www.fpdf.org/es/doc/output.htm
says that dest should be first and the name second, although all of them are optional.
$pdf->Output("D","example2");
you shouldnt name your file .pdf because it will add it when you download it
Make sure you didn't output absolutely nothing before calling the Output (neither a whitespace, tab or anything) or the browser will just go nuts. Pay special attention at your includes, if any.

TCPDF output without saving file

How to use TCPDF to output pdf file in browser without saving like in ezpdf?
Use I for "inline" to send the PDF to the browser, opposed to F to save it as a file.
$pdf->Output('name.pdf', 'I');
This is what I found out in the documentation.
I : send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
D : send to the browser and force a file download with the name given by name.
F : save to a local server file with the name given by name.
S : return the document as a string (name is ignored).
FI : equivalent to F + I option
FD : equivalent to F + D option
E : return the document as base64 mime multi-part email attachment (RFC 2045)
If You want to open dialogue window in browser to save, not open with PDF browser viewer (I was looking for this solution for a while), You should use 'D':
$pdf->Output('name.pdf', 'D');
Hint - with a saving file:
$pdf->Output('sandbox/pdf/example.pdf', 'F');
Print the PDF header (using header() function) like:
header("Content-type: application/pdf");
and then just echo the content of the PDF file you created (instead of writing it to disk).
I've been using the Output("doc.pdf", "I"); and it doesn't work, I'm always asked for saving the file.
I took a look in documentation and found that
I send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
Then I think you have to use a plugin to print it, otherwise it is going to be downloaded.
It works with I for inline as stated, but also with O.
$pdf->Output('name.pdf', 'O');
It is perhaps easier to remember (O for Open).
$filename= time()."pdf";
//$filelocation = "C://xampp/htdocs/Nilesh/Projects/mkGroup/admin/PDF";
$filelocation = "/pdf uplaod path/";
$fileNL = $filelocation."/".$filename;
$pdf->Output($fileNL,'F');
$pdf->Output($filename, 'S');

Categories