[encode_rtf]

Description

Link: [encode_rtf]
Author: Johan Sölve
Category: String
Version: 8.x
License: Public Domain
Posted: Oct. 30, 2008
Updated: Oct. 30, 2008
More by this author...

When building RTF files by hand, the text content needs to be encoded properly according to RTF specifications. This tag encodes extended characters and linebreaks for use in RTF files, and converts the html tags <b>, <i> and <br> to the RTF correspondents.

This tag does NOT produce a complete RTF document, you need to take care of that yourself. It is only intended to help you encode the text properly.

Read more about the RTF format here:

http://en.wikipedia.org/wiki/Rich_Text_Format

http://search.cpan.org/~sburke/RTF-Writer-1.11/lib/RTF/Cookbook.pod

Keep in mind that Lasso can also create RTF files with the PDF tags using PDF_Doc(-format='rtf'), but this tag is useful if you need to hand craft RTF files according to a specific format instead of creating full RTF documents from scratch.

Parameters

-input string, required The text string to encode

Sample Usage

var('rtfdoc'=encode_rtf('This is a räksmörgås.

Now we are <b>done</b>!');

// add RTF prolog and end of document
$rtfdoc = '{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0 Times New Roman;}}'
	+ $rtf_doc
	+ '}';

// serve the file
file_serve($rtfdoc, 
	-file='myfile.rtf', 
	-type='application/rtf');
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
define_tag('encode_rtf', -required='input');
	local('output'=string(#input));
	#output -> replace('\r\n', '\n') & replace('\r', '\n');
	#output = encode_url(bytes(#output, 'windows-1252'));	
	#output -> replace('%0a', '\n') & replace('%20', ' ') & replace('%3c', '<') & replace('%3e', '>');
	#output -> replace(regexp(-find='<br[^>]*>', -replace='\n', -ignorecase));
	#output -> replace('\n', '\\line ');
	#output -> replace(regexp(-find='%([0-9a-f]{2})', -replace='{\\\\\'\\1}', -ignorecase));
	#output -> replace(regexp(-find='<b\\b[^>]*>(.*?)</b>', -replace='{\\\\b \\1}', -ignorecase));
	#output -> replace(regexp(-find='<i\\b[^>]*>(.*?)</i>', -replace='{\\\\i \\1}', -ignorecase));
	return(@#output);
/define_tag;

 

Comments

none

Email:


Password:



Newest

Most Popular