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

Hail fellow monks,
I have a question as for the best way to open/display and excel or a pdf file in my browser. I have several directories that I search through and when I come to a report, I grab the extension to see what type of report it is(excel/pdf). Then I tried to open the file a couple of ways unsuccessfully. By unsuccessfully, I mean that the adobe acrobat reader tries to open the excel files. When you try and save the file, it comes up as a .pl filetype. I assume that is because my program is a .pl type and it just assumes that the file being opened is the same????

Here is some of my code...Any enlightenment would be appreciated.
$format = &Check_Format($report); &Display_Report($format); sub Check_Format { my $report = $_[0]; my ($name,$format) = split(/\./,$report); return uc($format); } sub Display_Report { my $type = $_[0]; my ($offset,$bytes,$buffer,$contents,$print_pdf); if ($type eq 'XLS') { $print_xls = 'path/to/data/reports'; open(XLS, "$print_xls") || Write_Error($print_xls); $offset = 0; $contents = ''; binmode XLS; until(eof(XLS)) { $bytes += read(XLS,$buffer, 1048576, $offset); $offset += 1048576; $contents .= $buffer; } close XLS; print header(-type => 'application/vnd.ms-excel'); print $contents; } else { if ($type eq 'PDF') { $print_pdf = '/path/to/data/reports'; open(PDF, "$print_pdf") || Write_Error($print_pdf); $offset = 0; $contents = ''; binmode PDF; until(eof(PDF)) { $bytes += read(PDF,$buffer, 1048576, $offset); $offset += 1048576; $contents .= $buffer; } close PDF; print header(-type => 'application/pdf'); print $contents; }
I also tried a simple method of:
print `echo "Content-type: application/pdf"`; print `echo`; print `cat /path/to/data/reports/file.pdf`;
Any help you could offer would be appreciated. Is there a better way to open/display report files.
Prince99

Too Much is never enough...

Replies are listed 'Best First'.
Re: displaying excel/pdf files in browser
by VSarkiss (Monsignor) on Feb 27, 2002 at 16:57 UTC

    Well, I don't know if this is the whole problem, but you're doing a lot of unnecessary work. If you want to read a complete file into a scalar, then print it, you can do it all in one go:

    binmode XLS; { print header(-type => 'application/vnd.ms-excel'); local $/; print <XLS>; close XLS; }
    The same applies to your PDF file.

    HTH

Keep The Document Extensions - Re: displaying excel/pdf files in browser
by metadoktor (Hermit) on Feb 27, 2002 at 17:06 UTC
    I'm not sure if I understand what you're doing but it seems like you're using Perl on your Windows machine to look for Excel and PDF files?

    Well, are you trying to open the excel or pdf files with their extension intact? If you use IE it should autodetect the file type from the extension and open Excel inside of your browser but if you rename a spreadsheet from mydoc.xls to mydoc then you will probably have the troubles that you have right now.

    metadoktor

    "The doktor is in."

      I am actually trying to use perl on a *nix box to search for excel and pdf files. I have not modified the extenstions, just check them to see what type of report it found.
      Prince99

      Too Much is never enough...
Re: displaying excel/pdf files in browser
by Prince99 (Scribe) on Feb 27, 2002 at 19:13 UTC
    I found a little line that seems to be helping the reports display correctly.

    print "Content-type: application/vnd.ms-excel\n"; # The Content-Disposition will generate a prompt to save the fil +e. If you want # to stream the file to the browser, comment out the following l +ine. print "Content-Disposition: attachment; filename=$name\n";


    Has anybody used this before? If so, is there any documentation available on its proper use?
    Prince99

    Too Much is never enough...
      I've used Content-Disposition for both .ZIP and .EXE. Works like a charm. The user will be prompted to download the file, the the filename will be set to the value of $name.

      Of course, this doesn't help any with the original problem of displaying .XLS and .PDF files in a web browser...

        I have found Content-Disposition specific to certain browsers - it is usually only Explorer that ignores the Content-Type in favour of the file extension ...

        /J\

Re: displaying excel/pdf files in browser
by cwsaylor (Acolyte) on Feb 28, 2002 at 15:01 UTC
    I struggled with the same problem not too long ago. It finally dawned upon me to just do a header redirect and let the browser handle the mime type. No need to read the file into a buffer.
    $url = "http://mypdf.pdf"; print "Location: $url\n\n"; exit;

    Chris Saylor

      Being the very novice perl programmer that I am, I've been looking for this solution for a couple months now, off and on. Would anyone have an idea why the following procedure would be used (buffering the file, then printing it to the page) instead of the above?
      open (FILE, "$path"); while (<FILE>) { print $_; }
      Hugh