The man page for RTF::Writer doesn't discuss this, but what if you want to create an rtf file containing unicode text in some obscure language that uses a wacko writing system? I played with a sample unicode file (in Thai), managed to load it into MS Word and "Save as ... RTF"; this showed me the rtf expression for unicode characters. Well, all we have to do is express our non-ascii unicode characters that way, pass the result to RTF::Writer, and we're done...
#!/usr/bin/perl use strict; use RTF::Writer; die "Usage: $0 file.txt\n (this will create file.rtf)\n" unless ( @ARGV and $ARGV[0] =~ /\.txt$/ and -f $ARGV[0] ); # input file is expected to be utf8 open( I, "<:utf8", $ARGV[0] ) or die "$ARGV[0]: $!"; my $utf = do { local $/; <I> }; # slurp it # here's the magic part: replace each wide character with # "\uN\5f", where "N" is the decimal numeric codepoint: $utf =~ s/([^[:ascii:]])/sprintf("\\u%d\\'5f",ord($1))/eg; ( my $out = $ARGV[0] ) =~ s/txt$/rtf/; my $rtf = RTF::Writer->new_to_file( $out ); my @pars = split( /\n+/, $utf ); $rtf->prolog( title => $out ); for my $par ( @pars ) { $rtf->paragraph( \$par ); # need to pass $par by reference } $rtf->close;
Is that easy, or what?

Update: Caveat emptor: YMMV, especially depending on what language you're dealing with (I've only tried Thai so far). I don't know how/whether RTF handles bidirectional text (Arabic- or Hebrew-based writing systems), and various Indic scripts (Hindi, Tamil, etc) might have weird problems if the rtf recipient doesn't know the "special rules" for rendering those languages. But the "normal, tidy, linear" languages (Cyrillic, Greek, Chinese, Korean, etc) should be fine (assuming you have the appropriate unicode fonts available).

Replies are listed 'Best First'.
Re: Writing Unicode to an RTF file
by john_oshea (Priest) on Feb 02, 2006 at 10:59 UTC

    ++ - I wish I'd seen this about a year ago ;-)

    Bear in mind that RTF expects signed 16-bit values for character codes, which has bitten me more than once in the past. It's also worth pointing out that MS Word, in particular, will choke if you don't follow each Unicode character with its "ansi equivalent", which you're achieving with the '\5f' in your sprintf - most of the other RTF-aware editors I experimented with showed no such restriction.

Re: Writing Unicode to an RTF file
by GeneralElektrix (Acolyte) on May 23, 2011 at 19:06 UTC
    ++ Amazing. More than 5 years later this post saves my life. We still use the RTF::Writer module with some obscure Reporting System and we migrate from Latin-1 to UTF-8. Since the input is now all UTF-8, I had to decode it before using the magical regexp, but at the end, it works as expected. Thank you graff!
Re: Writing Unicode to an RTF file
by tqviet (Acolyte) on Mar 16, 2018 at 10:13 UTC
    WOW ... it's amazing! Now most languages are coming out with unicode. For instance, I have to print all my stuffs written in Vietnamese with UTF8 out to .doc files. This magic is really helpful. Thank you very much, GRAFF.