creating a pdf

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

creating a pdf

Post by rsmarsha »

I am trying to create a pdf file of a quote drawn from a database.

To start with i'm trying to get one variable from the address bar to show in a pdf template i created. The template and code was obtained from a php book i have.

Here is the php code:

Code: Select all

<?php
  set_time_limit( 180 ); // this script can be very slow
  //create short variable names
  $number = $_GET['orderno'];

  function pdf_replace( $pattern, $replacement, $string )
  {
    $len = strlen( $pattern );
    $regexp = '';
    for ( $i = 0; $i<$len; $i++ )
    {
      $regexp .= $pattern[$i];
      if ($i<$len-1)
        $regexp .= "(\)\-{0,1}[0-9]*\(){0,1}";
    }
    return ereg_replace ( $regexp, $replacement, $string );
  }
  
  if(!$number)
  {
    echo '<h1>Error:</h1>This page was called incorrectly';
  }
  else
  {
    //generate the headers to help a browser choose the correct application
    header( 'Content-Disposition:  filename=test.pdf');
    header( 'Content-type: application/pdf' );
  
    $date = date( 'F d, Y' );
  
    // open our template file
    $filename = 'test.pdf';
    $fp = fopen ( $filename, 'r' );
    //read our template into a variable
    $output = fread( $fp, filesize( $filename ) );

    fclose ( $fp );

    // replace the place holders in the template with our data  
    $output = pdf_replace( '<<NUMBER>>', strtoupper( $number ), $output );
  //  $output = pdf_replace( '<<cat>>', $prods, $output );
   // $output = pdf_replace( '<<mm/dd/yyyy>>', $date, $output );
   
    // send the generated document to the browser
    echo $output;
  }
?>
As you can see i'm just trying to use the one variable $number and insert it into it's place holder <<NUMBER>> which is in the given template pdf file.

All i get as output in the browser is :

Code: Select all

%PDF-1.4 %âãÏÓ 15 0 obj <> endobj xref 15 9 0000000016 00000 n 0000000805 00000 n 0000001070 00000 n 0000001281 00000 n 0000001527 00000 n 0000001815 00000 n 0000002057 00000 n 0000000640 00000 n 0000000476 00000 n trailer <]>> startxref 0 %%EOF 23 0 obj<>stream xÚb```e``îb
Any idea where i'm going wrong?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Whietspace somewhere above the opening <?php tags thats messing your headers up?
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Don't think so, thats all that is in the file.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Tried removing all gaps in the file and still nothing. :(
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

rsmarsha wrote:Tried removing all gaps in the file and still nothing. :(
Call exit() right after your echo.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

For some reason it just started working, without the exit.

Now it opens the pdf, but with a popup error "this file is damaged and cannot be repaired", the pdf then opens but with <<number>> and not the variable.
Last edited by rsmarsha on Thu Apr 20, 2006 6:23 am, edited 1 time in total.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

I've had similar issues with dynamic pdfs before and the solution was to actually do a header redirect to the newly created file rather than trying to open it on the same page even with the correct headers. I think its a known issue, I'll post again if I can find where I learned about it.

Hope this helps.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The R&OS PDF library hiccups like this on occassions fairly sporadically too.
hessodreamy
Forum Commoner
Posts: 58
Joined: Wed Apr 20, 2005 8:11 am

Post by hessodreamy »

I think the broken output is a problem in IE (assuming you're using IE). Closing the browser and re-opening usually does the trick. For me the problem seems to occur only in development when you're going backwards and forwards in the browser. Nice to know the trick about redirecting to the created file, though.

The "file is damaged" message would normally be just a problem in your code. Bad arguments in a pdf-related function, for example.

This is just based on my use of FPDF, though.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

The code is from a large php book i have, it's not using fpdf or pdflib, as fpdf is more for forms i think and pdflib isn't on the server. This code uses a template pdf with place holders in it. If anyone wants to try it themselves, just create a pdf with <<NUMBER>> in and then pass a number in via the browser to a file with the above code in.

I can't seem to get it to work, i've tried making a new pdf file and checking the code over.

Without the line :

Code: Select all

$output = pdf_replace( '<<NUMBER>>', $number, $output );
it opens the pdf template, obviously without the substituted information. So the error is either in the function or the replacement process. Still can't figure it out though. :(

Tried using preg_replace instead and it opens the pdf template but without replacing the data.

Really stuck on this one.

I can't seem to get it working, and nobody else seems to know either.

I looked into getting pdflib installed on the server where the site i'm working on is hosted. I was a bit shocked how much pdflib costs, as i can't use lite due the site i want to use it on.

Does anyone have any ideas about how to get the template thing working? The code is all in the above post, just create a pdf with <<NUMBER>> in and pass the info over into the file.

Any help is much appreciated, as i can't see why this doesn't work. Just need to get the place holder replaced.

Or if nobody can help with this error, is there another way apart from pdflib (which costs), to generate pdf's? Can't use FPDF as thats for forms. Easiest way would be to do it as i'm trying, but can't fix the error.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I hope you realize that all textual data in a PDF is stored in encoded binary bytes, not in plain text. The only plain text is control codes for a reader to interpret the data at hand.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Well i'm using a book called "php and mysql web development" the code is straight from that. Was testing it first before i added all the rest i needed. The book said to use a placeholder like <<NUMBER>>, the pdf_replace function in the above code is meant to find the placeholders. Having never generated a pdf before, i used the code from the book.
Post Reply