Reputation:
I'm trying to output PDF Files, which later on, i want them to be donwloaded, not shown. But right now, i got a problem. I'm creating a wordpress plugin, but i'm getting this error:
FPDF error: Some data has already been output, can't send PDF file (output started at C:\xampp\htdocs\wp-admin\menu-header.php:132)
This is the code i currently have:
<?php
/**
* Plugin Name: PHPtoPDFTest
* Plugin URI: http://mypluginuri.com/
* Description: Turns PHP into a PDF
* Version: 1.0 or whatever version of the plugin (pretty self explanatory)
* Author: Brent Lobbezoo
* Author URI: Author's website
* License: A "Slug" license name e.g. GPL12
*/
add_action('admin_menu', 'PHPtoPDFTest_admin_actions');
function PHPtoPDFTest_admin_actions() {
add_options_page('PHPtoPDFTest', 'PHPtoPDFTest', 'manage_options', __FILE__, 'PHPtoPDFTest_admin');
}
function PHPtoPDFTest_admin() {
//Hier worden de database inloggegevens opgeslagen
$username = "root";
$password = "";
$hostname = "localhost";
//Hier wordt connectie gemaakt met mysql
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//Hier wordt de database Test gekozen, waarin alles op dit moment staat opgeslagen
$selected = mysql_select_db("test",$dbhandle) or die("Could not select examples");
//Het result, een array, van de query wordt hier in $result gestopt
$result = mysql_query(
"
SELECT lid.Lid_ID, lid.Naam, lid.Adres, lid.Email,
(SELECT Bedrag FROM Tarief WHERE Soort = 'contributie' LIMIT 1) AS 'Contributie',
(SELECT Bedrag FROM Tarief WHERE Soort = 'liggeld' LIMIT 1)*IFNULL(schip.lengte,0) AS 'Liggeld',
(SELECT Bedrag FROM Tarief WHERE Soort = 'contributie' LIMIT 1)+
(SELECT Bedrag FROM Tarief WHERE Soort = 'liggeld' LIMIT 1)*IFNULL(schip.lengte,0) AS 'Totaal'
FROM lid
LEFT JOIN Schip ON lid.Schip=schip.Naam
WHERE lid.ContributieBetaald <> 'ja'
"
);
ob_start();
require('fpdf.php');
while ($row = mysql_fetch_assoc($result)) {
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$row{'Naam'},1);
$pdf->Cell(40,10,$row{'Adres'},1);
$pdf->Cell(40,10,$row{'Email'},1);
$pdf->Cell(40,10,'Contributie bedrag: '.$row{'Contributie'},1);
$pdf->Cell(40,10,'Liggeld Bedrag: '.$row{'Liggeld'},1);
$pdf->Cell(40,10,'Totaal bedrag: '.$row{'Totaal'},1);
$pdf->Output();
}
}
?>
Basically, I need to generate a receipt for every user in the database (currently only two), but for some reason, the error pops up!
This is the code at line 132 from mneu-header:
echo "\n\t<a href='{$submenu_items[0][2]}'$class $aria_attributes>$arrow<div class='wp-menu-image$img_class'$img_style>$img</div><div class='wp-menu-name'>$title</div></a>";
If someone knows how to fix this problem, i'd be very thankfull!
Upvotes: 1
Views: 1324
Reputation: 2010
FPDF can't output to the browser if you've already output text to the screen previously. It looks like the header is causing this problem from what you've shown. You should open a blank page or window, one that does not extend or call upon WP's templates, and generate the PDF there.
Upvotes: 1