replacing a placeholder in a pdf template

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

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

replacing a placeholder in a pdf template

Post by rsmarsha »

I have posted in the php code forum about my problem with pdf templates. Can't seem to find an answer to the errors, and i think the error is in the funtion (which came from a book). As the function uses a regular expression i thought i'd post here.

The page that runs to create the pdf from a template is :

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>>', $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; 
  } 
?>
The pdf itself just has :

QUOTE: <<NUMBER>>

As i have only the one line to test the basic placeholder replacement. At present i'm getting an error saying the file is damaged. If i remove the line calling in the function to replce the placeholder with the data, the page opens, though obviously with the placeholder still there. So it seems to be the replacement function thats causing the errors. Anyone any idea?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sorry, we don't need a second thread involving the exact same code. Please continue to use the orignal thread.
Locked