Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] How to get a PDF file in memory to a users Web browser

Michael Lake wrote:
> I wish to create some PDF docs using pdflib on the server and send them
> to a users browser.

I have added the following bit of code...

my $p = PDF_new(); # must be matched with a PDF_delete !
# Generate PDF entirely in memory. See the manual.
PDF_open_file($p, ""); # Dont forget to call PDF_close($p) later;

# Save the PDF data contained in the buffer then clear the buffer.
$document = PDF_get_buffer($p);

# Send the doc to the users browser.
my $length = length ($document); print length;
my $filename = "out.pdf";
my $q = new CGI;               # create new CGI object

# create the HTTP header
print $q->header(-type=>"Content-Type: application/pdf",
                 -disposition=>'inline; filename="$filename"');

print $q->start_html('hello world'), "\n"; # start the HTML
#print $q->h1('hello world'), "\n";         # level 1 header
print $document;

Now I am getting binary data returned to the users browser as text :-(
I prob have something wrong with the HTML headers.

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml";
lang="en-US"><head><title>hello world</title>
5 0 obj
<</Length 6 0 R



Michael Lake
Active caver, Linux enthusiast and interested in anything technical.