Page 1 of 1

PDF header problem

Posted: Thu Nov 01, 2007 3:05 pm
by SimpleNL
Hi, I'm trying to output a PDF as an attachment for a component. I'm trying to achieve this by outputting PHP headers like so:

Code: Select all

$hidden = php::createRandomPassword();

header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" );

header( "Content-Type: application/pdf" );
header( 'Content-Length: '. filesize( $file ) );
header( 'Content-Disposition: attachment; filename="file_'. $hidden .'.pdf";' );
header( 'Content-Transfer-Encoding: Binary' );

readfile( $file );
Funny thing is, it generates an error in which it states that the file is corrupt, or that the "file doesn't begin with '%PDF'". When I separate the header code from the Joomla file (a separate file without any Joomla dependencies) it works like a charm. Could anyone tell me please what I am doing wrong, or what I should do to get it right?

Re: PDF header problem

Posted: Tue Dec 11, 2007 12:57 pm
by tekguc
I have faced with the same problem today ???, after a long googling here is the solution I end up with  :pop:
if you use readfile() function inside the joomla add the below code before you read the file.
// clean the output buffer
  while( @ob_end_clean() );
so, in your case your code should be  ;),

Code: Select all

$hidden = php::createRandomPassword();
  while( @ob_end_clean() );
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" );

header( "Content-Type: application/pdf" );
header( 'Content-Length: '. filesize( $file ) );
header( 'Content-Disposition: attachment; filename="file_'. $hidden .'.pdf";' );
header( 'Content-Transfer-Encoding: Binary' );

readfile( $file );
Umut TEKGÜÇ,
QuickWASP Limited
www.quickwasp.com