PDF header problem

For Joomla! 1.0 Coding related discussions.
Locked
User avatar
SimpleNL
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Sat Aug 11, 2007 1:03 pm
Location: Amsterdam, Netherlands

PDF header problem

Post by SimpleNL » Thu Nov 01, 2007 3:05 pm

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?
Webdeveloper - Van Eldijk Studio's
http://www.vaneldijk.nl

tekguc
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Jan 16, 2007 2:27 pm

Re: PDF header problem

Post by tekguc » Tue Dec 11, 2007 12:57 pm

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


Locked

Return to “Joomla! 1.0 Coding”