PHP -> PDF generation ( Using PDF templates on the fly )

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

kirkham99
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Mar 11, 2003 8:25 am

PHP -> PDF generation ( Using PDF templates on the fly )

Postby kirkham99 » Tue Mar 11, 2003 8:30 am

I wonder if you could help, I have a question of using PHP -> PDF templates

I have written 3 simple scripts one to use an RFT as a template and resolve variables on the fly and also a version using PDFLIB functions and they both work OK.

The problem is I want to use PDF templates and resolve variables on the fly, and I keep getting the same error message from the web browser "The file is damaged and could not be repaired". I am using Acrobat 5.0

By the way, the script works fine if I comment out the PDF_REPLACE option. I assume these are causing some type of error?? I tried updating the Acrobat prefences/options to not load in a Browser but that just downloaded to my PC and the same error message appeared after I double clicked on it...

The PDF template small script is attached

<?php

set_time_limit( 180 ); // this script can be very slow

//create short variable names
$name = $HTTP_POST_VARS['name'];
$score = $HTTP_POST_VARS['score'];

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;
}
?>

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: PHP -> PDF generation ( Using PDF templates on the fl

Postby WiZARD » Wed Mar 12, 2003 4:25 am

Hi kirkham99!
I'm see very good manual about generatinf PDF
http://www.devshed.com/Server_Side/PHP/PDF/page1.html

User avatar
Skeletor
New php-forum User
New php-forum User
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio
Contact:

Postby Skeletor » Wed Mar 12, 2003 4:30 am

Not sure if this will be of any help, but I used the
fdf_set_value()
function to write to a pdf on my site. I don't have the script in front of me to compare the two, but it may be worth your while to look into that function as well.

kirkham99
New php-forum User
New php-forum User
Posts: 4
Joined: Tue Mar 11, 2003 8:25 am

Fdf.. script

Postby kirkham99 » Wed Mar 12, 2003 7:12 am

Hi,

Do you still have a copy of that script, in summary

BACKGROUND
============

I am looking to create a series of Certificates and Brouchures etc and then using the products Print/Acrobat Distiller option save the documents as PDF(with some pre-built in <<Name1>>, <<address1>>, <<address2>> variables that can be automatically resolved
"on-the fly" using a PHP script using a PDF_REPACE(see below code) option or something similar. This then allows me to Create Nice looking Brouchures/Certificates etc with "variable <<abc>> markers" before converting to PDF so my online user can have "personalised stationary" without the need for them to EDIT any of the documents.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests