Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/templates/ja_barite/ja_menus/Base.class.php on line 62
Printable Document Generation with PHP

Excellent Fundamental...!!!

 
  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • green color
  • blue color

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

Trackback(0)
Comments (10)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.
...
written by pixces, March 19, 2009
Hey! the sample is good, the logic is excellent but what about database. How will you add a list of say 2 - 5 records generated from MySql query into this doucment. Please let me know. I am interested.

Bye
Thanks for the file
written by Sunil, May 07, 2009
Ur work is excellent and if there was an option to generate pages as well it would be excellent.
Anyways Thanks a lot! smilies/wink.gif
Great Code
written by SP, October 29, 2009
This code was very useful and totally efficient
...
written by Rambabu, September 13, 2010
Hi All,

This is really good, But how set that image for all pages(like header and footer for all pages).


If any help thanks in advance
...
written by Jess, November 23, 2011
This is really a good work. It would be helpful if this work could accommodate data from Mysql and put them in a tabular format. There can be various number of rows depend on the situation. Is it possible?

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
 

Articles


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Adobe Gallery


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Community


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

FAQ's


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Resources


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Who's Online

We have 34 guests online

Subscribe Feeds

Enter your email address:

Sponsor Adds


 
 

Submit Article

We want to develop such a Community portal which will provide a good resource on latest hot technologies 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 admin[at]xfunda.com

Adobe Gallery Photoshop Tutorials...

Photoshop Tutorial - Create Colorful Composition in Photoshop Photoshop Tutorial - Create Colorful Composition in Photoshop Photoshop Tutorial - Create Colorful Composition in Photoshop Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Category - Photoshop >> Photo Effects. ...
 
Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Category - Photoshop >> Photo Effects. ...