Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Object Label Badly Formatted

by Anonymous Monk
on Mar 05, 2005 at 00:12 UTC ( #436820=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got an application that accepts PDF file uploads and stores them in a BLOB column in MySQL. Another application pulls data from the database and sends it to the browser. When the database is queried the BLOB column is written to a file in a public HTML directory and a link is sent to the browser.

My problem is that when users click on the link the Acrobat plug-in loads and then errors out with "Object Label Badly Formatted". If I move the PDF to the directory myself then it loads correctly in the browsers. ls -Fla on the directory shows that the two files (uploaded through the application and copied in with Samba) have sizes that differ by 8 bytes (the uploaded file is larger).

Any ideas of where to look? Here's the code that is accepting the upload:
my $pdf_name = $cgi_query->param('event_pdf'); my $pdf = 'EventPDF'; if ($pdf_name){ #A file (supposedly a PDF) has been sent in the + post data. Load it into $pdf for storage in events table binmode($pdf_name); while (<$pdf_name>){ $pdf .= $_; } $pdf = $db->quote($pdf); } my $mime_type; #(Not very robust) type checking for uploaded file. This is depen +dent on the browser behaving $mime_type = $cgi_query->uploadInfo($pdf_name)->{'Content-Type'} i +f $pdf_name; if ($mime_type){ return $self->display_error("Attached file is not a PDF\n") un +less $mime_type eq 'application/pdf'; }


Replies are listed 'Best First'.
Re: Object Label Badly Formatted
by digger (Friar) on Mar 05, 2005 at 02:46 UTC

    You are telling perl to read your pdf line by line, and then putting the line into a variable. With binary data, a byte sequence that looks like a line ending (0A on unix, 0D0A on Windows) could appear anywhere in your file. What you end up with is binary data that is, for lack of a better term, mangled.

    You can get around the problem by doing this

    my $readbuf; while ( read (INF, $readbuf, 32767) # read in (up to) 32k chunks ) {$pdf .= $readbuf}

    Now, instead of reading to a newline, which is probably a random byte somewhere inside the pdf, you are reading in 32k bytes at a time. Your pdf stays in tact, and you shouldn't have any trouble.

    HTH, digger

Re: Object Label Badly Formatted
by Anonymous Monk on Mar 05, 2005 at 01:00 UTC
    I fixed the problem by changing:
    while (<$pdf_name>){ $pdf .= $_; }

    $pdf = do {local($/) ; <$pdf_name> };

    but I don't quite understand why that fixes the problem. Any explanation would really be appreciated.

      I'm not sure that while (<$pdf_name>) works properly in binmode. You should be using read or sysread. In other words, don't read line-by-line when you've just told perl that it's a binary file. The second (new) code snippet doesn't read by line - it slurps the entire file as-is, with no translation. To get your while to work, just set local $/ = \1024 to read in 1024-byte chunks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://436820]
Approved by Tanktalus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-11-21 14:32 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (302 votes). Check out past polls.