Where should I start?
I have this PHP script that draws up a PDF file and then modifies text placeholders within this PDF document. It works like a dream with the PDF Document that came with it.
Here comes the problem:
The original PDF Doc was created in word and exported to Acrobat Distiller in ASCII format and compression turned off, (meaning Acrobat 3 was used for the creation of the PDF). I have Acrobat 4.05 and uses the same conditions when exporting a new PDF-template. The difference is the PHP Script still opens the new PDF-template BUT it doesn’t overwrite the placeholders anymore.
I have tried everything over the past two weeks in my small and limited knowledge to correct this problem without any success. I thus do not know whether the problems lie within the php script or within my distiller settings.
PLEASE HELP!!!
The following information and script was taken from the Book “PHP and MySQL Web Development” by Luke Welling and Laura Thomson published by SAMS PUBLISHING
Copyright © 2001 Sams Publishing
“To create the file, we used Microsoft Word to design a document. One of the tools in the Acrobat package is Adobe Distiller. Within Distiller, we needed to select a few non-default options. The file must be stored in ASCII format, and compression needs to be turned off.” Copyright © 2001 Sams Publishing
This function works with the following pages:
1. index.html
2. score.php
3. pdf.php
I herewith include the source code for these three files. The working pdf document (PHPCertification.pdf) and the non-working pdf (test.pdf) can be downloaded in a zip format from the following address: http://www.rocci.org/downloads/pdf.zip.
Index.html
<body>
<h1><p align = center>
<img src = "rosette.gif" alt = "">
Certification
<img src = "rosette.gif" alt = ""></h1>
<p>You too can earn your highly respected PHP certification
from the world famous Fictional Institute of PHP Certification.
<p>Simply answer the questions below:
<form action = score.php method = post>
<p>Your Name <input type = text name = name>
<p>What does the PHP statement echo do?
<ol>
<li><input type = radio name = q1 value = 1>
Outputs strings.
<li><input type = radio name = q1 value = 2>
Adds two numbers together.
<li><input type = radio name = q1 value = 3>
Creates a magical elf to finish writing your code.
</ol>
<p>What does the PHP function cos() do?
<ol>
<li><input type = radio name = q2 value = 1>
Calculates a cosine in radians.
<li><input type = radio name = q2 value = 2>
Calculates a tangent in radians.
<li><input type = radio name = q2 value = 3>
It is not a PHP function it is a lettuce.
</ol>
<p>What does the PHP function mail() do?
<ol>
<li><input type = radio name = q3 value = 1>
Sends a mail message.
<li><input type = radio name = q3 value = 2>
Checks for new mail.
<li><input type = radio name = q3 value = 3>
Toggles PHP between male and female mode.
</ol>
<p align = center><input type = image src = "certify-me.gif" border = 0>
</form>
</body>
score.php
<?
// check that all the data was received
if($q1==''||$q2==''||$q3==''||$name=='')
{
echo "<h1><p align = center><img src = 'rosette.gif' alt = ''>
Sorry:
<img src = 'rosette.gif' alt = ''></h1>";
echo "<p>You need to fill in your name and answer all questions";
}
else
{
//add up the scores
$score = 0;
if($q1 == 1) // the correct answer for q1 is 1
$score++;
if($q2 == 1) // the correct answer for q2 is 1
$score++;
if($q3 == 1) // the correct answer for q3 is 1
$score++;
//convert score to a percentage
$score = $score / 3 * 100;
if($score < 50)
{
// this person failed
echo "<h1><p align = center><img src = 'rosette.gif' alt = ''>
Sorry:
<img src = 'rosette.gif' alt = ''></h1>";
echo "<p>You need to score at least 50% to pass the exam";
}
else
{
// create a string containing the score to one decimal place
$score = number_format($score, 1);
echo "<h1><p align = center><img src = 'rosette.gif' alt = ''>
Congratulations
<img src = 'rosette.gif' alt = ''></h1>";
echo "<p>Well done $name, with a score of $score%,
you have passed the exam. ";
// provide links to scripts that generate the certificates
echo "<p>Please click here to download your certificate as
a Microsoft Word (RTF) file. ";
echo "<form action = 'rtf.php' method = get>";
echo "<center>
<input type = image src = 'certificate.gif' border = 0>
</center>";
echo "<input type = hidden name = score value = '$score'>";
echo "<input type = hidden name = name value = '$name'>";
echo "</form>";
echo "<p>Please click here to download your certificate as
a Portable Document Format (PDF) file. ";
echo "<form action = 'pdf.php' method = get>";
echo "<center>
<input type = image src = 'certificate.gif' border = 0>
</center>";
echo "<input type = hidden name = score value = '$score'>";
echo "<input type = hidden name = name value = '$name'>";
echo "</form>";
echo "<p>Please click here to download your certificate as
a Portable Document Format (PDF) file generated with PDFLib. ";
echo "<form action = 'pdflib.php' method = get>";
echo "<center>
<input type = image src = 'certificate.gif' border = 0>
</center>";
echo "<input type = hidden name = score value = '$score'>";
echo "<input type = hidden name = name value = '$name'>";
echo "</form>";
echo "<p>Please click here to download your Modified certificate as
a Portable Document Format (PDF) file. ";
echo "<form action = 'mypdf.php' method = get>";
echo "<center>
<input type = image src = 'certificate.gif' border = 0>
</center>";
echo "<input type = hidden name = score value = '$score'>";
echo "<input type = hidden name = name value = '$name'>";
echo "</form>";
}
}
?>
pdf.php
<?
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 .= "()-{0,1}[0-9]*(){0,1}";
}
return ereg_replace ( $regexp, $replacement, $string );
}
if(!$name||!$score)
{
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=cert.pdf");
header( "Content-type: application/pdf" );
$date = date( "F d, Y" );
// open our template file
$filename = "PHPCertification.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( "<<NAME>>", strtoupper( $name ), $output );
$output = pdf_replace( "<<Name>>", $name, $output );
$output = pdf_replace( "<<score>>", $score, $output );
$output = pdf_replace( "<<mm/dd/yyyy>>", $date, $output );
// send the generated document to the browser
echo $output;
}
?>
END