krishna.p Posted September 11, 2009 Share Posted September 11, 2009 Hi freaks, I would like to know how to merge 2 rtf files onto a single rtf file using PHP. let me explain you the problem briefly. In my application i would like to present a word template with specific info to the user using open,save and cancel dialog box. i.e.. if user requests for one template and clicks on OK button, i need to open that word template through dialog box. If user requests for 2 templates, then user should get those 2 templates onto 1 word document and that document should be opened through open,save and cancel dialog box. Currently i am able to populate the data onto 1 RTF template and able to present it to user. Also, i have no problem in creating multiple word documents. My questions are: 1)can i be able to populate the data(depending on users request) onto a single word document with multiple templates each in one page?i.e. one template per one page. 2)if the above option is not possible, then can i be able to merge existing 2 rtf files into one and display it to user as a single word document using open, save and cancel so that this final document contains 1st template in first page and 2nd template in second page. I would be thankful to you if guys could help me in solving this problem. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
krishna.p Posted September 11, 2009 Author Share Posted September 11, 2009 using php is this possible? how to merge 2 rtf files onto single rtf file....guys waiting for your reply...Thanks.... Quote Link to comment Share on other sites More sharing options...
DavidAM Posted September 11, 2009 Share Posted September 11, 2009 It is possible, and fairly simple. HOWEVER, there are several gotchas that could cause significant problems with the result. RTF is a markup language, kind of like HTML. However, it has some different rules and very different structure and tags. Open one of your RTF files in notepad (or your favorite text editor) and have a look. RTF elements are grouped together using curly braces ( { and } ) and always start with \rtf. So a minimum rtf file would be {\rtf } which is an empty document. So to merge two RTF files together the way you described, you would load the first one and drop the ending brace, add a page command (\page), then add everything from the second file after the openning brace. BUT WATCH OUT!!!! All commands in an rtf file start with a back-slash, and end when whitespace is found OR an new command starts (with another back-slash) or a new group starts (curly brace). So to change the color there is a command like \cf1, which changes to color 1 (one). But what is color 1 you ask? I'm glad you asked that. At the beginning of an RTF document is a color table. It lists the colors that are used in the document, ex: {\colortbl ;\red0\green128\blue0;\red0\green0\blue255;} So in this color table, color 1 is green and color 2 is blue. The colors are added to the color table in the order that they are introduced to the document. NOT the order they appear in the document. So in this case, I made something green and then later made something blue. The blue text may be before the green or not, it's the sequence of adding them that matters. There is also a font table at the beginning of the document that behaves in the same manner. THIS IS A PROBLEM in the merge instructions above because a well-behaved rtf reader will ignore unexpected and unknown tags. The color table and font table belong at the beginning of the document, so these tables from the second document, appearing in the middle of the document are ignored. If the color tables or font tables are different between the documents, the second document will not be formatted properly. There is also a styles table, which I have never analyzed much, but I expect it behaves in a very similar manner. So, unless you have complete control over all the RTF files and you KNOW you will ALWAYS create the documents using the same colors and fonts and styles IN THE SAME ORDER your merge is going to break down. There is a lot more going on in the "head" of an rtf document (margins, tabs and indents, default font, color, etc) that may or may not be "obeyed" in the (second) merged document. To get around the tables issues, you could load the two documents separately, grab the color tables and compare them. Merge them into a single table and do a search and replace in the second document to use the color indexes from the merged table. Do the same with the font table (and possibly the style table). Throw away the tables from the second document and append the rest of it to the first (with the \page in between). It will take some work, but is probably doable. Quote Link to comment Share on other sites More sharing options...
krishna.p Posted September 13, 2009 Author Share Posted September 13, 2009 Thank you so much DavidAM for your reply. first i have read contents of first rtf file and removed the last character of that file. i.e.. "}". and then i have removed the first 6 characters of second rtf file i.e.. "{\rtf1" and finally added "}" character. Now iam able to merge two rtf templates. Once again thank you so much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.