Is that easy, or what?#!/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;
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 | |
|
Re: Writing Unicode to an RTF file
by GeneralElektrix (Acolyte) on May 23, 2011 at 19:06 UTC | |
|
Re: Writing Unicode to an RTF file
by tqviet (Acolyte) on Mar 16, 2018 at 10:13 UTC |