Printable Document Generation with PHP
This article focuses on the document that can be generated using PHP namely Rich Text Format (RTF). Rich Text Format (RTF) is a file type used to transfer formatted text documents between applications, even those that run on different platforms, such as IBM and Macintosh. RTF is a file format that many word processing programs understand; it is often used when a document is created in one word processing program but is expected to be edited in another word processing program.Advantages of RFT Format
- Consistency in rendering and printing
- A cross-platform solution
- Flexible enough for most tasks
- Easy to edit
- Contain more text and less file size
- Supports Open Office, Microsoft Word, or any other major word processor
Steps for generating dynamic RTF
- Generate a template document with placeholders for dynamic content using your favorite word processor.
- Open the template document in PHP
- Replace the placeholder strings within the document with the desired dynamic content.
- Save the generated document or display it.
To achieve our goal we take example that a company name XCELSION TECHNOLOGY INDIA Take online test on different programming languages. And then offer Grade based certificate on the basis of marks obtain by student. Here we going RTF certificate file based on templates.
Generate a template document
First we developing template using Microsoft Word which looks as figure 1. And save that file with filename certificate.rtf this is main file which we modify to generate certificate. In this document, we placed a number of placeholders, each of them in the format %%PLACEHOLDER%%, in which PLACEHOLDER is a unique string for each value to be filled in within the document.
Open the template document in PHP
Now the Template document has been generated. The next process is simply a matter of opening the template from within a PHP script and making the necessary modifications. To open our file using php we are using primary file-access function the fopen() function. The formal syntax of the fopen() function is as follows:fopen(string $filename, string $mode [, boolean $use_include_path ])
$filename represents the filename to open, $mode represents the "mode" under which the fopen() function is opening the file (see Table 1), and $use_include_path is a Boolean value that indicates whether the file should be looked for in the PHP include path. The fopen() function then returns a "reference" to the opened file, which is used when working with other file system functions, or returns false if the fopen() call fails.
| Table 1 Modes supported by fopen() | |
| r | Open the file for reading. |
| r+ | Open the file for reading and writing. |
| w | Open the file for writing, overwriting existing files, and creating the file if it does not exist. |
| w+ | Open the file for reading and writing, overwriting existing files, and creating the file if it does not exist. |
| a | Open the file for writing, creating the file if it does not exist, and appending to the file if it does. |
| a+ | Open the file for reading and writing, creating the file if it does not exist, and appending to the file if it does. |
| b | Open the file in binary reading/writing mode (applicable only on Windows systems; however, recommended in all scripts). |
Modifying contents
Here we just replace all the placeholders in our template file and generate our modified output file as ‘output.rtf’. for that purpose we are using function modifier($vars, $doc_file).This function takes two parameters the first, $vars, is an array of key/value pairs representing the placeholder and value for that placeholder. The second parameter, $doc_file, represents the template file to populate. (Code 1:- test.php)
<?php
$vars = array('date' => date("F d, Y"),
'place' => 'Nagpur',
'name' => 'Flint Hancock',
'language' => 'PHP',
'grade' => 'A', );
$new_rtf = modifier($vars, "certificate.rtf");
$fr = fopen('output.rtf', 'w') ;
fwrite($fr, $new_rtf);
fclose($fr);
function modifier($vars, $rftfile) {
$xchange = array ('\\' => "\\\\",
'{' => "\{",
'}' => "\}");
$document = file_get_contents($rftfile);
if(!$document) {
return false;
}
foreach($vars as $key=>$value) {
$search = "%%".strtoupper($key)."%%";
foreach($xchange as $orig => $replace) {
$value = str_replace($orig, $replace, $value);
}
$document = str_replace($search, $value, $document);
}
return $document;
}
?>
<h2>sucessfully Generated File</h2>
Save the generated document
Upon execution of the above script (Code 1:- test.php), contain function modifier($vars, $doc_file) which returns a string representing the new RTF document with all the provided placeholders fields filled by new values of array $vars and RTF file stored in the file system as ‘output.rtf’.
Source Code:
Download Source code for 'Printable Document Generation with PHP '
Download

written by Chetankumar Akarte, April 17, 2008
Thanks for Your Comment.
I don't have any kind problem if you used my Script on soccer team site.
Thanks & Regards
Chetankumar Akarte
Admin
http://www.xfunda.com/
written by Udayakumar, August 05, 2008
written by sachith darshana, November 04, 2008
thanks
written by dave, December 19, 2008
For example if you were reading a list of subject marks from the db and wanted to reflect them in the document and there were varying numbers of subjects.
written by pixces, March 19, 2009
Bye
written by Sunil, May 07, 2009
Anyways Thanks a lot!










Regards...Rob