Printable Document Generation with PHP

E-mail Print PDF

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
We use technique that is the concept of generating dynamic RTF documents through the use of templates. A template determines the basic structure for a document and contains document settings such as AutoText entries, fonts, key assignments, macros, page layout, special formatting, and styles etc.

Steps for generating dynamic RTF

  1. Generate a template document with placeholders for dynamic content using your favorite word processor.
  2. Open the template document in PHP
  3. Replace the placeholder strings within the document with the desired dynamic content.
  4. 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.


the template document in PHP

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’.

the template document in PHP

Source Code:

Download Source code for 'Printable Document Generation with PHP '

Download

Comments (5)Add Comment
IT Manager
written by Rob Elliott, April 11, 2008
I would like to see a working example of your very interesting php script - Printable Document Generation with PHP. I would also like to use it on a non-profit disabled soccer team site. Pleases let me know your comments for each.

Regards...Rob
Thanks Rob
written by Chetankumar Akarte, April 17, 2008
Hi Rob,

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/
Mr.
written by Udayakumar, August 05, 2008
Good one. Can you do the same by taking the values from MySQL and store the table data in the word in the tabular format?
great
written by sachith darshana, November 04, 2008
hey this is great. but can u try to edit the rtf rather than replacing the strings.
thanks
...
written by dave, December 19, 2008
Seems good, however I wondered how you would deal with a situation where you wanted to insert varying numbers of rows into the document.

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.

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

Who's Online

We have 15 guests online

Subscribe Feeds

Enter your email address:

Sponsor Adds


Submit Article

I want to develop such portal which will provide a good resource on latest hot techologies such as .NET, VB.NET, C#.NET, ADO.NET, ASP.NET, PHP, Flash, Photoshop. If you like to write for us, this guideline for you. please send your article, faqs on This e-mail address is being protected from spambots, you need JavaScript enabled to view it





Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo
Demo

Latest Reviews...

php|architect's Zend PHP 5 Certification Study Guide php|architect's Zend PHP 5 Certification Study Guide php|architect's Zend PHP 5 Certification Study Guide PHP is a most popular, widely used server-side scripting language ...
 
Pragmatic Unit Testing in C# with NUnit, 2nd Ed. Pragmatic Unit Testing in C# with NUnit, 2nd Ed. Pragmatic Unit Testing in C# with NUnit, 2nd Ed.While developing any .net application Unit testing is an essential techn...
 
Your Brain: The Missing Manual Your Brain: The Missing Manual Your Brain: The Missing Manual Our brain which controls our day to day activities is a mysterious creation of the Natur...
 



Apple

Apple TV

Lorem ipsum dolor sit amet consectetuer id nibh convallis suscipit netus

iTunes Gifting

iPod + iTunes Gifting

Quisque elit dolor ut congue sed ligula turpis Phasellus Aenean hac. Et Sed ipsum

Apple TV

Apple TV

Lorem ipsum dolor sit amet consectetuer id nibh convallis suscipit netus

iPod + iTunes Gifting

iPod + iTunes Gifting

Quisque elit dolor ut congue sed ligula turpis Phasellus Aenean hac. Et Sed ipsum

iPod Your Life

iPod Your Life

Tortor Aenean tincidunt pede massa Nunc id dui massa elit In. Nam amet a tellus lacus