codeigniter and fpdf code blockin during pdf output - php

i have a big problem with fpdf
in my page i have a lot of code and at the end i create a pdf file with this sample:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
ob_end_clean();
$pdf->Output();
the result is that only the creatioin of pdf are make and the other code no !
first the code of the sample is:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
//ob_end_clean();
$pdf->Output();
like this the code before is execute without problem but not the pdf and i receive this on browser:
FPDF error: Some data has already been output, can't send PDF file
can you help me ?
im newbie with fpdf libraries

that is because your php code has already outputted some data to the browser.
Check that your php do not write any data to the browser yet ( except for the headers ) before calling $pdf->Output(), thus avoiding sending unknown data that has a pdf binary data appended at the end.
the ob_end_clean() function call ensure that your script clean any previous output your php script could have made.

Related

FPDF gives only lines of code back

I'm trying to work with FPDF to export Data from a HTML-Form to a PDF and can't get it to work. I tried the example given in the downloadable tutorial and got only a part of the code back instead of an actual error or even a PDF.
This is the example I used
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
And the output looks like this:
AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); ?>
I did it exactly as any youtube-tutorials shows it. The file fpdf.php is in the same folder as my php-code and so on. I just don't see what I'm doing wrong.

Print PDF file with variables from other file?

I am wondering is it possible to print PDF file, and replace the blank spots in it with variables I get from my website, so output depends on the variable's value etc.
Use FPDF to do what you need.
But in this case you have to know that you must create a pdf file from scratch and fill it in the way you want.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage(); // add page to PDF
$pdf->SetFont('Arial','B',16); // Choose a font and size
$pdf->Cell(40,10,'Hello World!'); // write anything to any line you want
$pdf->Output("your_name.pdf"); // Export the file and send in to browser
?>

FPDF error: Some data has already been output, can't send PDF file on 000webhost

I am using FPDF class to generate a pdf on my website. Everything worked well until last few weeks when I started getting error:
FPDF error: Some data has already been output, can't send PDF file
During last few weeks haven't change anything in my code and I have also checked for any output execpt the fpdf (including unecessary space before php, disabled BOM signature etc.)
I have my website on 000webhost.com so I have also disabled the analytic code at the end of the page, but the pdf still doesn't work. The only trace I have left is misterious "" in a source code (I can see it when checking source code in Chrome browser).
I cant get to work even this simple example:
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage()
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Is there a way to disable any other output on web page by php? or does someone use fpdf on 000webhost?
just insert ob_end_clean(); before outputing.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage()
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
ob_end_clean();
$pdf->Output();
?>
I think that session.auto_start is set to 1. This will start a session and send a PHPSESSID cookie to the browser.
You can try to disable it using the following code:
<?php
ini_set("session.auto_start", 0);
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage()
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
In case setting session.auto_start to 0 does not work, then try this:
<?php
ob_start();
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage()
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
ob_end_flush();
?>
In my case i had set:
ini_set('display_errors', 'on');
error_reporting(E_ALL | E_STRICT);
When i made the request to generate the report, some warnings were displayed in the browser (like the usage of deprecated functions).
Turning off the display_errors option, the report was generated successfully.
Use line like this:
require('fpdf.php'); ob_end_clean(); header("Content-Encoding: None", true);
Issue will resolved ;)
This error will occur if you try to generate the PDF after you have already rendered something else on that browser page, for example, if you have done something like this:
echo $value;
The FPDF code wants a "blank canvas" to render its output on (or, one surmises, a blank iframe, although I haven't tested that yet).
SELECT motivo,
unidad_trans,
km_inicial,
km_final,
rut_chofer,
To_char(hora_inicial, 'DD/MM/YYYY HH:mm'),
To_char(hora_final, 'DD/MM/YYYY HH:mm'),
total_recorrido,
destino,
cod_combustible,
cantidad_litros,
cod_vehiculo,
d.cod_estableci
FROM mov_bitacora b,
mov_chofer c,
nuc_dependencias d,
mov_combustible co,
mov_vehiculo v
WHERE b.unidad_tran = d.cod_estableci
AND b.rut_chofer = c.rut_chofer
AND b.cod_combustible = co.cod_combustible
AND b.cod_vehiculo = v.cod_vehiculo
AND id_bitacora = 6fpdf
Error: Some data has already been output, can't send PDF file.
I putted this to fix this problem in the beggining (ob_clean) doesn't change PDF structure:
require('fpdf/fpdf.php');
ob_clean();

how to generate pdf and save to server

Well, i'm trying to generate pdf file and save it to server but when i'm trying to view that pdf its showing invalid format.
Code :
<?php
$content = "32w434";
file_put_contents("xyz.pdf",$content);
?>
If i change file from pdf to doc it works and opening perfectly but not pdf
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output();
file_put_contents("xyz.pdf",$content);
That code giving output i don't want output on browser and still file is corrupted / invalid format
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output('doc.pdf','F');
http://www.fpdf.org/
You can use FPDF to generate pdf file....
http://www.fpdf.org/
This is simple and best way to generate pdf...
Use TCPDF to generate PDF file,
TCPDF Site: http://www.tcpdf.org/
Demo: http://www.tcpdf.org/examples.php
#Mohit,
This
$content = $pdf->Output('doc.pdf','F');
generates a new file. If your file is generated using variables that can change in php, the pdf file you see on screen, will be different from the pdf file saved on the server.
You should generate pdf file using pdf libraries such as tcpdf or fpdf. Putting contents directly into a file with .pdf extension will definitely not working since pdf has its own file format

FPDF error: Some data has already been output, can't send PDF

I am using the fpdf library for my project, and I'm using this to extend one of the drupal module. These lines
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
give me an error: FPDF error: Some data has already been output, can't send PDF
I tried creating this in a separate file outside the drupal area name test.php and when viewed it worked. Anyone here know why this don't work? Or anyone here can point me a right pdf library which I can use in drupal to view HTML to PDF format.
For fpdf to work properly, there cannot be any output at all beside what fpdf generates. For example, this will work:
<?php
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
While this will not (note the leading space before the opening <? tag)
<?php
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Also, this will not work either (the echo will break it):
<?php
echo "About to create pdf";
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
I'm not sure about the drupal side of things, but I know that absolutely zero non-fpdf output is a requirement for fpdf to work.
add ob_start (); at the top and at the end add ob_end_flush();
<?php
ob_start();
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
ob_end_flush();
?>
give me an error as below:
FPDF error: Some data has already been output, can't send PDF
to over come this error:
go to fpdf.php in that,goto line number 996
function Output($name='', $dest='')
after that make changes like this:
function Output($name='', $dest='') {
ob_clean(); //Output PDF to so
Try to save the file without the option: "BOM comment", i.e. in Adobe Dreamweaver, you Save File As..., uncheck the box below the filename that says, "Include Unicode signature(BOM)".
On Notepad++ you should select the menu: Encoding, "Encode in UTF-8 without BOM".
And make it default for other files you create, it will spare you a lot of headaches in future.
Hi do you have a session header on the top of your page.
or any includes
If you have then try to add this codes on top pf your page it should works fine.
<?
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);
?>
cheers :-)
In my case i had set:
ini_set('display_errors', 'on');
error_reporting(E_ALL | E_STRICT);
When i made the request to generate the report, some warnings were displayed in the browser (like the usage of deprecated functions).
Turning off the display_errors option, the report was generated successfully.
The FPDF Error Message will point you to the PHP Line that is sending some content.
If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.
For me
fpdf.php was ANSI-encoded,
my pdf-generator.php was UTF-8-encoded and
my database-connect-inlude was UTF-8-encoded (this UTF-8-encoding did raise the FPDF error. I had to switch it back to ANSI)
if you're code outputs notices/warnings before the PDF generation, try turning them off. error_reporting(0). Then work on the warnings there-after
Add to the beginning of the script
ob_start();
require ('fpdf.php');
and at the end, after output()
ob_end_flush();
It worked for me! =)
First step
check the permissions on the folders
second step
put this
ob_start();
before the line
$pdf->Output();
I used the following and it worked for me
require_once ('pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output(F,'/var/www/html/PATH/filename.pdf');
ob_end_flush();
Even a single space in the included php files causes that warning. There shouldn't be any output in any way.
You need to call the library
require ('fpdf.php');
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'¡Hola, Mundo!');
$pdf->Output();
?>
http://www.fpdf.org/
http://www.fpdf.org/es/tutorial/tuto1.htm
Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file (output started at /home/asri123/public_html/bill/invoice/invoice.php:743)' in /home/asri123/public_html/bill/invoice/fpdf.php:271 Stack trace: #0 /home/asri123/public_html/bill/invoice/fpdf.php(1052): FPDF->Error('Some data has a...') #1 /home/asri123/public_html/bill/invoice/fpdf.php(1012): FPDF->_checkoutput() #2 /home/asri123/public_html/bill/invoice/mirasbill.php(262): FPDF->Output('MSFS/2018-19/76...', 'D') #3 {main} thrown in /home/asri123/public_html/bill/invoice/fpdf.php on line 271
Another answer that nobody else has posted here... Double-check the encoding of your PHP file and make sure that it's not something other than UTF-8. The wrong code editor (or FTP upload?) can mess with the file's encoding in which case none of the other fixes mentioned in this thread will help.

Categories