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

Hi all

Now I develop web page with calculations and I want that user will be able save results to computer in MS-Word or TXT document type. I try search on cpan and here for some modules but sources is much and diferent and I dont know which is the better for me. If someone know which module is better for me please help me.

Thanks

Programing in Embperl and all questions are related to Embperl.
Excuse my bad English !!!

Replies are listed 'Best First'.
Re: Save results on web page to doc or txt
by derby (Abbot) on Jul 13, 2004 at 11:56 UTC
    Well ... you don't say whether the word doc will look different (graphics, fonts sizes, etc) from the TXT doc so I'll assume they dont.

    Just generate the plain text page and then set the content-type (CGI header method) to text/plain or application/ms-word. MS-Word will open and display the plain text just fine.

    The only caveat being that some browsers (and we all know there really doesn't need to be an s on the end of browsers) totally ignores the content-type so you may have to also changed the extension of the file being delivered (.txt or .doc). There are ways to do this (mod-perl, re-directs, etc) but get the content-type approach working first and then jump the hoops only if you have to).

    -derby

    Update: I'm also assuming, you don't want the user to just use the File->Save As function of the browser after your results page is delivered to the browser - that would really be the easiest approach.

      You assume very well that I dont want usre to use File->Save As because it is very hard operation for many people who use IE . I want do it comfortably for users. I will place on page anchor which run script which save document on user comp. Now I see it will be little complicated as think.

      Programing in Embperl and all questions are related to Embperl.
      Excuse my bad English !!!

        I don't think it should be that difficult. Here's a snippet from one of my scripts that I use for serving up files but it shouldn't take too much to extract what you need from it.

        #!/usr/bin/perl -wT use strict; use CGI; $CGI::HEADERS_ONCE = 1; use constant BUF_SIZE => 4096; # Unbuffer the output stream: $| = 1; # Set the environment for Taint mode: BEGIN { $ENV{PATH} = "/bin:/usr/bin"; delete @ENV{qw (IFS CDPATH ENV BASH_ENV)}; } my $q = new CGI; my $source = &get_file_name($q); # IE is funky, doesn't seem to understand # "-type => 'text/octet-stream'", # wants a .whatever filename my $save_as_name = &get_saveas_name($source); if (open(IN, $source)) { print $q->header( -type => 'text/octet-stream', -attachment => $save_as_name ); my $buffer; binmode STDOUT; while (read(IN, $buffer, BUF_SIZE)) { print $buffer; } close IN; } else { print &get_error_page($q, $save_as_name, $!); } ...
Re: Save results on web page to doc or txt
by tomhukins (Curate) on Jul 13, 2004 at 12:09 UTC

    It's not clear what you're trying to do here: it would help if you broke the problem down into small parts and told us where you're stuck, maybe sharing some of the things you have tried. You might find How to ask questions the smart way. helpful for getting the most out of this site.

    Here's my guess at what you want to do:

    1. Display an HTML form to the user.
    2. Process the user's input.
    3. Generate feedback as plain text or a Word document.
    4. Return this feedback to the user somehow.

    Assuming I've guessed correctly, you can perform each of these stages as follows:

    1. Use a templating system (you mention Embperl) to generate the form, filling in data as required. Alternatively, use CGI's HTML output features.
    2. Write a handler using CGI, mod_perl or whatever you use.
    3. You will find plain text easier and more widely supported. If you want prettier layout, use RTF or PDF instead. Only use Word documents if you absolutely must: in general you'll find RTF an acceptable, better supported substitute.
    4. Either return the document directly from your handler, setting the appropriate MIME type in the Content-type HTTP header field, or write the file to disk and reference it with a hyperlink. If you reference the file, you should write a cron job to clear up old files.

      Your assume is perfect. Step 1. and 2. I have. Step 3. and 4. I must do as you write. Thanks for your possible solutions.

      Programing in Embperl and all questions are related to Embperl.
      Excuse my bad English !!!
Re: Save results on web page to doc or txt
by Joost (Canon) on Jul 13, 2004 at 12:00 UTC
    I'd recommend using plain txt over ms-word, because it's a lot easer. You don't have to save your results to a file first, because you can print the output directly "to the browser" as it were. Just use the text/plain content type instead of the usual text/html:

    #!/usr/bin/perl -wT # some processing of calculations here print "Content-type: text/plain\n\n"; # set content type to plain text print "Result 1 = $result1\n"; print "Result 2 = $result2\n";
Re: Save results on web page to doc or txt
by Happy-the-monk (Canon) on Jul 13, 2004 at 11:35 UTC

      These formats is not good for me. Users which will be use my web page use MS-WORD. And for them is better document type .doc or .txt. I find this script http://search.cpan.org/src/AWRIGLEY/html2text-0.003/html2text.pl. I try this and try find some better.

      Programing in Embperl and all questions are related to Embperl.
      Excuse my bad English !!!
Re: Save results on web page to doc or txt
by Scarborough (Hermit) on Jul 13, 2004 at 15:55 UTC
    If you wont to future engineer your web page use XML. Then Word, Excel can read it and soon database servers will too. Not to mention other applications. Where I work everything is now being done in XML and very easy and useful it is too.
      Future engineering shouldn't start with the most visible to end users part of a process, IMO.

      And Wampa clearly stated he didn't want XML.