views:

71

answers:

4

I am getting this error:

Warning: Cannot modify header information - headers already sent by (output started at E:\www\dev\elearning1\WebProjects\elearning\public\test.php:1) in E:\www\dev\elearning1\WebProjects\elearning\public\test.php on line 3

This is my entire PHP file:

<?php

header('Content-Type: text/html; charset=utf-8');

// set the include path
set_include_path(dirname(__FILE__).'/../../../WebLibThirdParty/Zend/library');

function __autoload($class) {
    require_once(str_replace('_', '/', $class) . '.php');
}

// Create new PDF 
$pdf = new Zend_Pdf();

$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

// Set font 
$page->setFont(Zend_Pdf_Font::fontWithPath('times.ttf'), 12); 

// Draw text
$string = 'škola volá';
$page->drawText($string, 200, 643, 'UTF-8');

$pdf->pages[] = $page;


$pdf->save('test.pdf');

As you can see, I am not seding any output to the browser anywhere so why do I get that error?

+3  A: 

Something is being sent to the browser. My guess is that your file is save with a BOM (Byte Order Mark). There should be a setting in your editor to save without the BOM. Or open it with a different editor (or a hex editor) and then remove it.

ircmaxell
I used a hex editor and indeed there were three weird invisible characters before <?php.
Richard Knop
Yeah... It's a problem in a fair bit of code. That's why most modern IDEs have BOM turned off...
ircmaxell
I'm using Eclipse. It's a modern browser, no?
Richard Knop
Yes, there's a setting in Eclipse for it (I don't have it installed on this computer, but I remember setting it back when I used it)...
ircmaxell
Duh. I meant IDE, not "browser". Anyways, thanks for help.
Richard Knop
+2  A: 

I'd guess you have a UTF-8 BOM mark in the beginning of your script.

Save it as UTF-8 without BOM (how you do this depends on your text editor/IDE) or use a PHP built with multibyte support compiled in.

Artefacto
A: 

php doesn't allow anything to be sent to the server after sending a header function command. i.e. header('Content-Type: text/html; charset=utf-8'); instead you should use echo with tags.

Rittwick Banerjee
Wrong way, PHP doen't allow to send a header, after something else was send. And the header is a very useful thing, that can be used without problems
Tokk
+1  A: 

hi, you need to clear the output buffer before calling header() in that way no matter where you call it in the code it will always be correct.

<?php
    ob_start();
    echo "Blah Blah Blah";
    // more echo
    ob_clean();
    header('Content-Type: text/html; charset=utf-8');
    //you code
    $pdf->save('test.pdf');
Ayaz Alavi