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

First allow me to apologize for not having an example of the script in question and for my inexperience in HTML, CGI, and Perl (Hey, you gotta start somewhere). The script is on my PC at work, and since we are having problems due to one of Sprint's routers according to SWBell, I am having to try to post this from home. I am writing a CGI script in which a user is able to choose a report (.TXT file) generated on a particular day by our OS/390 that has been ftp'ed to a certain location. This report will then be opened in the user's browser. I am able to identify the user, show the user a list of allowed reports, access the network drive, and retrieve this report. When the report appears in the browser it is not formatted. All (or most of) the spacing is taken out. I am wanting the ASCII file to appear the same (or better) as it would if I were opening it in Notepad. Also, most of these reports are printed on paper that is 14 7/8 inches by 11 inches. Is there a way to format the text in the browser to where the user will be able to see most if not all of the (width of the) file with out scrolling far to the right (or left)? Thanks for the advice in advance.
Blacksmith.

Replies are listed 'Best First'.
Re: Formatting a text file for browser
by wog (Curate) on Aug 31, 2001 at 07:09 UTC
    Also, you should be able to do this by telling the browser that the file is of mime type text/plain, not the default text/html. If you are using the CGI module (as you should be), then you just need to add some arguments to set the content type to the header method, changing code like this:

    print $q->header;

    to something like this:

    print $q->header(-type=>'text/plain');

    (If you are using the function-oriented interface to CGI it's the same, of course, just omitting the $q->.)

    (As always the CGI.pm docs are your friend.)

Re: Formatting a text file for browser
by Zaxo (Archbishop) on Aug 31, 2001 at 07:10 UTC

    PRE tags will have the browser follow the 'typewritten' format of the text. To adjust format, you might look at Text::Wrap.

    Update: Tilly's suggestion of Text::Autoformat sounds good. I've never used it but I expect he knows what he's talking about ;-)

    After Compline,
    Zaxo

Re: Formatting a text file for browser
by rrwo (Friar) on Aug 31, 2001 at 08:40 UTC

    Have your CGI script return a content type of "text/plain". You don't need to do any formatting whatsoever.

    Code is as follows:

    print "Content-type: text/plain\n\n"; # print text file here

    Or if you're using CGI.pm...

    my $q = CGI->new(); # handle any CGI parameters, set other headers, etc. print $q->header( { -type=>'text/plain' } ); # print text file here
Re: Formatting a text file for browser
by jryan (Vicar) on Aug 31, 2001 at 07:04 UTC

    You want text to display in the browser just like it would display in notepad? Wrap your ascii file in <pre> tags, and you'll be set... almost too easy :)

      Actually no. You will need to escape all the HTML special characters for them to display. Here is an example. The text is "Wrap your ascii file in <pre> tags, and you'll be set... almost too easy :)"

      "Wrap your ascii file in 
       tags, and you'll be set... almost too easy :)"

      Hmmm, not actually the text I wanted. I'll close that unmatched pre now

      CGI.pm has an escapeHTML method to do this $html = $q->escapeHTML($text) As a browser will only render 1 space even if you have 10 in a row or tabs you need to escape these to. I have posted a solution in this thread - see below.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Formatting a text file for browser
by tachyon (Chancellor) on Aug 31, 2001 at 16:49 UTC

    Here is the bare bones of what you need to do. You need to escape HTML special. If you want to use something other than <pre> tags you also need to change multiple spaces and tabs into &nbsp; and replace \n with <br>

    In this example we wrap the result in <pre> tags to get a fixed width font accurate display of the original text. Tabs are rendered as 4 spaces.

    Update

    The escapeHTML method this example did use is documented as an internal CGI.pm method so I have re-coded it here as it is naughty to use internal methods. I have also added the PerlMonks square bracket escape. It does not hurt anything.

    #!/usr/bin/perl -w my $text = "c:/text.pl"; open TEXT, $text or die "Oops can't open $text $!"; open HTML, ">$text.htm" or die "Oops can't write $text.htm $!"; print HTML "<pre>\n"; while (<TEXT>) { $_ = escapeHTML($_); print HTML $_; } print HTML "</pre>\n"; close HTML; close TEXT; sub escapeHTML { local $_ = shift; # make the required escapes s/&/&amp/g; s/"/&quot;/g; s/</&lt;/g; s/>/&gt;/g; # make the whitespace escapes - not required within pre tags s/\t/&nbsp;&nbsp;&nbsp;&nbsp;/g; s/( {2,})/"&nbsp;" x length $1/eg; # make the brower bugfix escapes; s/\x8b/&#139;/g; s/\x9b/&#155;/g; # make the PERL MONKS escapes (if desired) s/\[/&#091;/g; s/\]/&#093;/g; # change newlines to <br> if desired # s/\n/<br>/g; return $_; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Formatting a text file for browser
by Anonymous Monk on Aug 31, 2001 at 15:10 UTC
    "<PRE>" tags will not work reliably!
    Try this in an HTML document:
    <PRE> x is less than y: x<y y is greater than x: y>x </PRE>
Re: Formatting a text file for browser
by blacksmith (Hermit) on Sep 07, 2001 at 01:15 UTC
    Thanks for the input. I have tried all these methods. I am also looking at converting the text over into .pdf format. I just can't decide whether I want the easiest or the most visually attractive approach to all of this.

    Blacksmith.