1
votes

Controller:

public function test(){
    $data = array();
    $this->load->library('tcpdf');
    $this->display_page($data);
}

test.php: (Not posting all of it, as its a bit long)

global $l;
$l = Array();
$l['a_meta_charset'] = 'UTF-8';
$l['a_meta_dir'] = 'ltr';
$l['a_meta_language'] = 'en';
$l['w_page'] = 'page';

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8',              false);


-- pdf stuff -- 

$pdf->Output('example_007.pdf', 'I');

In Chrome, it just says "Failed to load PDF document.". No errors, nothing in console. In firefox, it says: "File does not begin with '%PDF-'. Local\EWH`_'s3-0y".

The string at the end of Local\ appears random with each load.

I have tried using output buffering and flush, to no avail. I am at a loss.

5

5 Answers

6
votes

Try to put exit; after outputting pdf. I hope it will fix that like it did for me.

-- pdf stuff --

$pdf->Output('example_007.pdf', 'I');
exit;
2
votes

try this

ob_clean();
ob_flush();
$pdf->Output('example_007.pdf', 'I');
ob_end_flush();
ob_end_clean();
0
votes

It's possible that you are outputting something to the browser before you are generating the invoice. Go to the source of the page and see if there is anything, including white spaces (new lines or spaces) and remove them.

0
votes

Duniyadnd is correct. When I looked at the network response in the debug tools in Chrome, I saw that the '%PDF-' magic was on the second line and not the first. I discovered some whitespace in my required include files before and after the delimiters which was outputting to the browser before the pdf file. Eliminating this whitespace before and after the php delimiters cured the problem.

0
votes

If you are using YII FRAMEWORK, ensure that you renderPartial and not render the view in your controller