mdavies23 has asked for the wisdom of the Perl Monks concerning the following question:

My program takes in an RTF document and can print in plain text but I am having trouble printing to a RTF document. Here is my code so far:

#!/usr/bin/perl use Data::Dumper 'Dumper'; use strict; use RTF::Parser; use RTF::TEXT::Converter; use RTF::Writer; #my $tokenizer = RTF::Tokenizer->new( file => "123456.rtf" ); die "usage: $0 input output\n" unless @ARGV == 2; my $infile = shift; my $outfile = shift; open my $fh, "<", $infile; open my $output, ">", $outfile; my $string; my $rtf = RTF::Writer->new_to_file($output); my $object = RTF::TEXT::Converter->new (output => \$string); $object->parse_stream( $fh ); chomp $string; my @un = split("\n", $string); my @sorted = sort { my @fields_a = split ' ' , $a; my @fields_b = split ' ', $b; chomp($a, $b); $fields_a[0] <=> $fields_b[0]; if($fields_a[1] || $fields_b[1] eq 'ERROR'){ $rtf->prolog(); $rtf->print($fields_a[1]); } } @un; $rtf->close; close $fh; close $output;

Basically if the field in the array says "ERROR" I want to print that to a RTF document. Any help would be much appreciated!

Replies are listed 'Best First'.
Re: How to print a text document to RTF document
by thanos1983 (Parson) on Jul 10, 2017 at 15:13 UTC

    Hello mdavies23

    I thought that I provided you with a solution on how to write to a RTF file at Re: How to tokenize a RTF file and print it to another file (Update with solution).

    But never the less let me try again. I would suggest some modifications on your code. For example you are calling use use RTF::Parser; but you do not use it. Then you are opening a write file handle open my $output, ">", $outfile; for no reason. The module RTF::Writer handles that for you, you do not need to use this (remove it). Last point you open a file but you do not use close die or warn. I wrote that also on my previous answer it is very important it help you know why your code might fail.

    I put together a working example based on the sample.rtf document that I posted on my previous answer. I can not fully test your code because you have not provide us a sample of input data and you are comparing numbers probably. So in my case I am getting error such as Useless use of numeric comparison (<=>) in void context at test.pl line 35, <DATA> line 183. Argument "La" isn't numeric in numeric comparison (<=>) at test.pl line 35.. But it should help you get going the following part of working code.

    #!/usr/bin/perl use say; use strict; use warnings; use RTF::Writer; use Data::Dumper; use RTF::TEXT::Converter; die "usage: $0 input output\n" unless @ARGV == 2; my $infile = shift; my $outfile = shift; open my $rtf_data, "<", $infile or die "Could not open the ".$infile.": $!"; my $output_str; my $object = RTF::TEXT::Converter->new( output => \$output_str ); $object->parse_stream( $rtf_data ); close $rtf_data or warn "Could not close ".$infile.": $!"; chomp $output_str; # say $output_str; my @un = split("\n", $output_str); my $rtf = RTF::Writer->new_to_file($outfile); my @sorted = sort { my @fields_a = split ' ' , $a; my @fields_b = split ' ', $b; chomp($a, $b); $fields_a[0] <=> $fields_b[0]; if($fields_a[1] || $fields_b[1] eq 'ERROR'){ $rtf->prolog(); $rtf->print($fields_a[1]); } } @un; print Dumper \@sorted; $rtf->close;

    If you have any further problems let us know.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Sorry yeah the other post helped me out but I realized i need to convert it to text to sort it. Now I am having trouble writing it to an RTF file from text and changing the font color. The documentation with the colors is not very clear. Would you happen to know about this? I got it to the font i want but the colors will not work for me.

       $rtf->printf( \'{\fs30\lang1036\noproof %s} \par ', "$_\n");

      Again, Thanks for all your help!

        Red and blue are defaults, for other colors you have to define a color table

        #!/usr/bin/perl use strict; use RTF::Writer; my $output = 'c:/temp/colortext.rtf'; my $rtf = RTF::Writer->new_to_file($output); $rtf->prolog( 'title' => "Color Test", 'colors' => [ undef, [255,0,0], # 1-red [0,255,0], # 2-green [0,0,255], # 3-blue [255,128,0], # 4-orange ], ); $rtf->paragraph( \'\cf1',"This is Red" ); $rtf->paragraph( \'\cf2',"This is Green" ); $rtf->paragraph( \'\cf3',"This is Blue" ); $rtf->paragraph( \'\cf4',"This is Orange" ); $rtf->close;

        Hello again mdavies23,

        No problem no worries. This is the reason that we need to have a valid input file/data so we can test based on your data and your expected output.

        Provide us a few lines of input, and the font/color that you get so we can experiment. I am not an expert with RTF files but I could play around if I have more data.

        Helps us to help you, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to print a text document to RTF document
by mdavies23 (Acolyte) on Jul 10, 2017 at 13:27 UTC

    Right now it does not print anything to the RTF document