Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Displaying RTF string to browser

by ikkon (Monk)
on Aug 21, 2007 at 16:26 UTC ( [id://634124]=perlquestion: print w/replies, xml ) Need Help??

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

I am creating a rtf file using RTF::Writer, and I am trying to display to the browser or at the minimum open rtf application. every time I print to screen I come up with a blank page I have tried multiple content type (ex. application/rtf, text/rtf ect.) does anyone know what i might be doing wrong?
#!/usr/bin/perl -w use perlchartdir; use CGI qw(:standard); use strict; no warnings; use RTF::Writer; use constant mm => 25.4/72; use constant in => 1/72; use constant pt => 1; #--------------------------------------------------------------------- +---------------------# #-------------------------- Main Variables (email,fonts,graphics, ect. +) -------------------# #--------------------------------------------------------------------- +---------------------# my $q = new CGI; print $q->header( "application/rtf" ); my %valueHash; my $fh; my $RTF_obj; #--> File variables #-----------------------> my $ID = "12345"; my $currencySymbol = $valueHash{"CurrencySymbol"}; my $rtfReport = "rtf_PCWS$ID".'.rtf'; my $rtfReportDel = "rtf_PCWS$ID".'.rtf'; my $CompanyName = "Company Name"; #--------------------------------------------------------------------- +--------------------------------------------------------------------- +-------# #------------------------------------------------------------- START R +TF ------------------------------------------------------------------ +-------# #--------------------------------------------------------------------- +--------------------------------------------------------------------- +-------# #--> Declare new rtf object either save to disk or save to string #$fh = RTF::Writer->new_to_file($rtfReport); $fh = RTF::Writer->new_to_string(\$RTF_obj); #--> MUST write a prolog - font and color table (RGB) #--> Font: when calling font use \f0 (or number of the array, to c +hange font size double the size #--> example: for 12 point font do - \fs24 $fh->prolog( fonts => ['Arial'], colors => [ undef, # color 0 == black [255,0,0], # color 1 == red [0,128,0], # color 2 == green [0,0,255], # color 3 == blue [255,255,0], # color 4 == yellow [255,255,255],# color 5 == white [200,200,200],# color 6 == light gray [187,204,224],# color 7 == table blue [144,172,202],# color 8 == table header bl +ue [102,102,102],# color 9 == gray ], ); #--> Setting up margins (these MUST be here) #----------------------------------------------> my $iMarginLeftInches = my $iMarginRightInches = 500; my $iMarginTop = 500; #--> Need bigger bottom margin for footer to fit: #-------------------------------------------------> my $iMarginBottom = 500; my $iMarginLeft = $iMarginLeftInches; my $iMarginRight = $iMarginRightInches; eval "\$fh->Margt$iMarginTop"; eval "\$fh->Margl$iMarginLeft"; eval "\$fh->Margr$iMarginRight"; eval "\$fh->Margb$iMarginBottom";
PAGEONE: { #---> Write Text ( parameters, Text ) - also does paragraphs #------------------------------------------------------------> writeText( '\par \f0 \fs20 \cf0\b', "Company Name: $CompanyName", ); lineBreak(); my @tableProp2 = ( widths => [inches(4), inches(4)], #--> width of +each column align => 'l l ', ); my @tableProp2b = ( widths => [inches(4), inches(4)], #--> width of +each column align => 'l l ', ); my $oDecl2 = RTF::Writer::TableRowDecl->new(@tableProp2,); $oDecl2->add_bgcolors(8); # Adds background color using color + hash $fh->row($oDecl2, \'\fs20\b\cf5 rtf Desktop Manager', "Price") +; my $oDecl2 = RTF::Writer::TableRowDecl->new(@tableProp2b,); $oDecl2->add_bgcolors(5); # Adds background color using color + hash $fh->row($oDecl2, \'\fs20\b Software Services (Month to Month) +', ""); $fh->row($oDecl2, \'\fs20 Asset Discovery (AD)', "0.45"); $fh->row($oDecl2, \'\fs20 Asset Management (price/seat/month)' +, "0.45"); $fh->row($oDecl2, \'\fs20 Software Distribution (price/seat/mo +nth)', "0.45"); $fh->row($oDecl2, \'\fs20 Patch Management (price/seat/month)' +, "0.45"); $fh->row($oDecl2, \'\fs20 Virus Protection (price/seat/month)' +, "0.45"); $fh->row($oDecl2, \'\fs20 On-line Backup 5 GB (price/seat/mont +h)', "0.45"); $fh->row($oDecl2, \'\fs20 On-line Each Additional 5 GB (price/ +seat/month)', "0.45"); $fh->row($oDecl2, \'\fs20 Remote Control (price/seat/month)', +"0.45"); $fh->row($oDecl2, \'\fs20 Data Encryption', "0.45"); $fh->row($oDecl2, \'\fs20 ', ""); $fh->row($oDecl2, \'\fs20\b Software Service Bundles', ""); $fh->row($oDecl2, \'\fs20 Management Suite (Asset Discovery an +d Asset Management) (price/seat/month)', "0.45"); $fh->row($oDecl2, \'\fs20 Compliance (Asset Discovery & Asset +Management & Software Distribution) (price/seat/month)', "0.45"); $fh->row($oDecl2, \'\fs20 Productivity (Asset Discovery & Asse +t Management & Software Distribution & Patch Management) (price/seat/ +month)', "0.45"); $fh->row($oDecl2, \'\fs20 ', ""); $fh->row($oDecl2, \'\fs20\b Software Services (12 month)', "") +; $fh->row($oDecl2, \'\fs20 Asset Discovery (price/seat/12 month +)', "0.45"); $fh->row($oDecl2, \'\fs20 Asset Management (price/seat/12 mont +h)', "0.45"); $fh->row($oDecl2, \'\fs20 Software Distribution (price/seat/12 + month)', "0.45"); $fh->row($oDecl2, \'\fs20 Patch Management (price/seat/12 mont +h)', "0.45"); $fh->row($oDecl2, \'\fs20 Virus Protection (price/seat/12 mont +h)', "0.45"); $fh->row($oDecl2, \'\fs20 On-line Backup 5 GB (price/seat/12 m +onth)', "0.45"); $fh->row($oDecl2, \'\fs20 On-line Backup Additional 5 GB (pric +e/seat/12 month)', "0.45"); $fh->row($oDecl2, \'\fs20 Remote Control (price/seat/12 month) +', "0.45"); $fh->row($oDecl2, \'\fs20 Data Encryption', "0.45"); $fh->row($oDecl2, \'\fs20 ', ""); $fh->row($oDecl2, \'\fs20\b Software Service Bundles (12 month +)', ""); $fh->row($oDecl2, \'\fs20 Management Suite (Asset Discovery & +Asset Management) (price/seat/12 month)', "0.45"); $fh->row($oDecl2, \'\fs20 Compliance (Asset Discovery & Asset +Management & Software Distribution) (price/seat/12 month)', "0.45"); $fh->row($oDecl2, \'\fs20 Productivity (Asset Discovery & Asse +t Management & Software Distribution & Patch Management) (price/seat/ +12 month)', "0.45"); } FOOTER: { my $sFooterHead = qq{\\pard\\ql\\plain\\f0\\fs18\\'a9}; my $sFooterTail = qq/\\par}/; $fh->print(\qq'{\\footer $sFooterHead', " Copyright 2001-2007 Alinean, Inc. and rtf Marketing +, L.P. \t\t\t\t Page ", \q'\chpgn', \$sFooterTail, ); } # end of FOOTER block $fh->print(\$RTF_obj); $fh->close(); #--------------------------------------------------------------------- +--------------------------------------------------------------------- +-------# #------------------------------------------------------------- FUNCTIO +NS ------------------------------------------------------------------ +-------# #--> line breaks pass number of lines #--------------------------------------> sub lineBreak{ my $line = q'\par'; $fh->print(\$line); } #--> Creat Table #----------------> sub newtable(){ my(@tableProp, $tableColor, @localData) = @_; my $decl = RTF::Writer::TableRowDecl->new(@tableProp,); $decl->add_bgcolors($tableColor); $fh->row($decl, @$_) for @localData; } #--> Make image #---------------> sub newImage(){ $fh->paragraph( $fh->image( 'filename' => $_[0], ), ); } #--> Inches from twips #----------------------> sub inches{ my ($inches) = @_; my $twips = 1440; $inches = ($inches * $twips); return $inches; } #--> Write text Function #-----------------------> sub writeText(){ my($rtfFormat, $writeText) = @_; $fh->paragraph( \$rtfFormat, # 12pt, bold, italic "$writeText" ); } __END__

Replies are listed 'Best First'.
Re: Displaying RTF string to browser
by bart (Canon) on Aug 21, 2007 at 16:38 UTC
    You're just creating an RTF file (file name in $rtfReport). You're not actually doing anything with that file after it's done.

    You should print its contents to STDOUT.

      I tried
      $fh->close(); print STDOUT $RTF_obj;
      and once the file opens in word I get a error document name or path is not valid
        That's a start. But you're printing the file name, not the file contents.

        Try, hmm... How to make this easy? OK, let's stick to general cross-platform solution: RTF is text, so we'll just print the text of the file to STDOUT line by line.

        open IN, $RTF_obj or die "Oops?"; while(<IN>) { print; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://634124]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2024-04-18 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found