Perl - display file in browser

Viewed 33

Absolute beginner in Perl programming but I need to use it as part of a project. I'm trying to display JPGs and PDFs but when i visit the URL the only thing I get is the file as plain text (both for images and pdfs).

Here's what my browser returns when I visit the pdf URL:

%PDF-1.6 %äüöß 2 0 obj <> stream xœ5‹± 1†÷> stream xœå9ktÕ™÷›É’-YÈŽe%ÑHÂ!Á9qcÅÙ‰ vb¤@bÉ’l‰Ø’”„ÀÒ¸Ô)Mʦ¼Ê’´XÊú,cXÓ¥ÄhK_v—GŠÏ)ÐölÒxiʲËûÝ;#Ç6žîÙ;òùî÷¾ßãÞ‘œIíŠ"<ñ„ƒI·µX$„üœ0‡vg

Obviously I'd prefer to read the PDF content or view the image in my browser..

The code I'm testing is this:

FOR PDF:

open(PDF, \<path-to-pdf\>) or die "could not open PDF [$!]";

binmode PDF;

my $output = do { local $/; \<PDF\> };

close(PDF);

print "Content-Type: application/pdf\n";

print "Content-Length: " .length($output) . "\n\n";

print $output;

FOR JPG:

open (IMAGE, \<path-to-img\>);

my $size = -s "\<path-to-img\>";

my $size =  do { local $/; \<IMAGE\> };

my $data;

print $q->header(-type=>'image/jpeg'), $data;

read IMAGE, $data, $size;

close (IMAGE);

This is the head of my script:

#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use File::Basename;

use POSIX qw(strftime);

$| = 1;

my $q = CGI->new;
print $q->header;

I don't know if I'm missing modules or if I need to include the files in a web page or anything else..

Any advice?

Grazie!

0 Answers
Related