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.
Related
I'm trying to use FPDF and FPDI to edit a PDF and add text to it. I keep getting an "Incorrect output destination" error but the destination is the correct location that I want it to create a file in, why does FPDF not like my output destination?
This is in a laravel project
$pdf = new \setasign\Fpdi\Fpdi();
$pdf->AddPage();
$pdf->setSourceFile(public_path('/pdf/higher.pdf'));
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 100);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output(public_path('/pdf/'),'higher2');
return $pdf;
and the error is:
message: "FPDF error: Incorrect output destination: /home/vagrant/code/project-name/public/pdf/"
I've also tried removing the "public_path()" and just setting it to Output('pdf', 'higher2') and no good there either.
Furthermore I've also tried changing the name of the output pdf to higher2.pdf just in case it wanted to see the extension (but obviously it's having more of a problem with the destination and not the name)
I've even tried changing permissions on this folder to be writable by anyone:
drwxrwxrwx 5 ion staff 160 May 21 05:44 pdf
edit: Just to note I see that the method with the public_path() is trying to save to my vagrant folder for some reason, that's part of the reason I'm confused. When I try to save to '/pdf' without public_path(), I get this error:
message: "FPDF error: Incorrect output destination: /pdf/"
edit 2:
I've also tried this:
$pdf->Output('F','/pdf/higher2.pdf');
and got the error:
message: "file_put_contents(/pdf/higher2.pdf): failed to open stream: No such file or directory"
and also tried the original name of the pdf which definitely exists and got the same error:
$pdf->Output('F','/pdf/higher.pdf');
You should never overwrite the file you are reading from!
The signature of the Output() method is:
string Output([string dest [, string name [, boolean isUTF8]]])
The $dest parameter is defined as:
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available.
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.
The default value is I.
So your code:
$pdf->Output(public_path('/pdf/'),'higher2');
makes absolutely no sense. I guess you want to save the resulting PDF to the path in the public area with the name higher2.pdf. So your code should look like:
$pdf->Output('F', public_path('/pdf/higher2.pdf'));
PS: You cannot edit a PDF with FPDI!
The Output() method requires the first parameter to be the destination and the 2nd parameter the filename.
From the documentation:
F: save to a local file with the name given by name (may include a path).
Try this:
$filename="/pdf/higher2.pdf";
$pdf->Output($filename,'F');
For the FPDF package, the syntax $pdf->Output('F','/pdf/higher2.pdf'); is wrong and you need to adjust your call as Jan Slabon explained.
However, if you want to support UTF-8 characters, then you need the tFPDF package, which is also supported by the setasign vendor:
$pdf = new \setasign\Fpdi\Tfpdf\Fpdi();
For this package you can store the output like this:
$pdf->Output('/pdf/higher2.pdf');
I'm new to PhP and need some help please
I have the following code to open a Staff member's PDF payslip however when it opens it is easy enough for the User to see the path in the address bar and then be able to manipulate the path to see other people's payslips.
This would however not be possible it the PDF file opened outside the Browser (maybe downloading the file and then opening it in Adobe or so).
(There is a login procedure before it gets to this code)
Can someone help with this please?
if(isset($_POST['openpayslip'])){
if(!file_exists('payslips/'.$_SESSION['username'].'.pdf')){
echo "Payslip or function does not exist";
}
if(file_exists('payslips/'.$_SESSION['username'].'.pdf')){
header('Location: payslips/'.$_SESSION['username'].'.pdf');
}
}
You load the PDF file into the PHP file and then output it with a PDF header, so that people can only see one file, with some authorisation variables given.
For example, you user name is an uneditable SESSION value (in a perfect world), so you display the payslip with:
payslip.php
and in this PHP file you load
if(file_exists('payslips/'.$_SESSION['username'].'.pdf')){
$output = file_get_contents("payslips/".$_SESSION['username'].".pdf");
header("Content-type:application/pdf");
print $output;
}
This is a VERY rough and sketchy example above, but with this approach you essentially are showing the PDF file through the PHP file so the source material is never revealed directly to the end user.
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.
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.
I want to pre-append some text a a CSV file that is created by MySQL.
Basically I want to add the header information for the rows so when the user opens it in Excel they know what each column is.
Whats the best way to do this. I presume there is some easy linux command that can do this? I can also do it in the PHP script. I'd like to know how to do both just for educational purposes.
General setup:
Debian Etc
LAMP website
Cron calls a PHP script which creates the csv file every night.
Thanks,
Derek
You can do for example this:
echo "header information" | cat - data.csv > dataInfo.csv
echo prints the header, cat takes this header from standard input and writes it together with everything from data.csv then into dataInfo.csv
Instead of pre-pending the header text to the data file, start with file which contains the header and then append the data/CSV file to this header file.