Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am trying to get a script to work

Status
Not open for further replies.

zzzzprogram

Programmer
Joined
Sep 7, 2001
Messages
2
Location
US
I am trying to get a script to work that replaces text from a PDF template. This template is a certificate that will be given to students upon completing an online training.

The script comes directly from the chapter 30 in the book, "PHP and MySQL Web Development" by Luke Welling & Laura Thomson.

The error message I get when the PDF file is opened in Acrobat Reader 5.0 is "There as an error processing a page. There was a problem reading this document (14)" and "Could not find the Extended Graphics State named 'GS1'"

Has anyone tried to use this script and already identified the problem or have any advice on ways to fix it?

Thanks for your help,
Laura
======================================
<?
set_time_limit( 180 ); // this script can be very slow

function pdf_replace( $pattern, $replacement, $string )
{
$len = strlen( $pattern );
$regexp = '';
for ( $i = 0; $i<$len; $i++ )
{
$regexp .= $pattern[$i];
if ($i<$len-1)
$regexp .= &quot;(\)\-{0,1}[0-9]*\(){0,1}&quot;;
}
return ereg_replace ( $regexp, $replacement, $string );
}

if(!$name||!$score)
{
echo &quot;<h1>Error:</h1>This page was called incorrectly&quot;;
}
else
{
//generate the headers to help a browser choose the correct application
header( &quot;Content-Disposition: filename=cert.pdf&quot;);
header( &quot;Content-type: application/pdf&quot; );

$date = date( &quot;F d, Y&quot; );

// open our template file
$filename = &quot;PHPCertification.pdf&quot;;
$fp = fopen ( $filename, &quot;r&quot; );

//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( &quot;<<NAME>>&quot;, strtoupper( $name ), $output );
$output = pdf_replace( &quot;<<Name>>&quot;, $name, $output );
$output = pdf_replace( &quot;<<score>>&quot;, $score, $output );
$output = pdf_replace( &quot;<<mm/dd/yyyy>>&quot;, $date, $output );

// send the generated document to the browser
echo $output;
}
?>

 
Question:

Can you open the PDF before you manipulate it, i.e. does the template itself open without trouble?

Also:
Do you have Acrobat to edit the PDF template or was is supplied with the book?
If you have Acrobat you'd be much better off using a template that is an interactive PDF with form fields. The you can use the FDF extensions of PHP and send form data to the template rather than doing a regular expression replace in the template PDF.
 
I can open the PDF template before it is manipulated. Also, the PDF template was supplied with the book. I will be creating my own template in the future so FDF is definitely an option.

It was mentioned in the book that FDF does not work well for substituting in text that is inline but works great for text that looks like a form field.

Do you have any experience using FDF? Would you agree that it is difficult using FDF for inline text?

Thanks for your help!
Laura

 
zzzzprogram,

I've used FDF to dynamically fill out complicated government forms that have countless fields, numbers, and checkboxes.

For inline text it might just be a question of how you set the form up. I agree that a field that would have to accomodate the name of a person within a paragraph is hard to substitute using FDF, as the name might be longer or shorter.

However, you could set your form up to contain the entire paragraph in the field and substitute there. Setting the form up in a way that helps using FDF would be certainly of advantage.

I was surprised to see that the book recommends regular expressions to infuse values into a PDF. I looked at a few PDFs in a text editor and there is barely any clear text in there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top