Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

decode_base64 MIME PDF Failing

by kiteskitesyay (Initiate)
on Jun 26, 2011 at 23:34 UTC ( [id://911483]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to pull a PDF file out of a gmail message via IMAP, and write it to file on local disk.

I can successfully connect to gmail, search (and find) the message in question, grab it in its entirety as a string, and navigate to the attachment. Now I need to decode it, and write it to file. Here is that portion of my code:

my $str = $client->message_string($id) or die "$0: message_string: $@" +; Email::MIME->new($str)->walk_parts(sub { my($part) = @_; #Skip non-attachment parts return unless $part->content_type =~ /\bname="([^"]+)"/; #Keep the filename from the MIME info my $name = "$1"; #No spaces in file names we're writing. $name =~ s/ /_/g; print "Writing $name...\n"; #Open the file for writing open my $fh, ">", $name or die "$0: open $name: $!"; my $blob = $part->body_raw; my $cleanPDF = decode_base64($blob); print $fh "$cleanPDF"; close $fh or warn "$0: close $name: $!"; });

This writes a file, and it opens as a PDF. However, it's 3 blank pages.

Even more frustrating, if I just print '$blob' as a txt file, and copy the contents, I can use an online base64 decoder like this one: http://www.opinionatedgeek.com/dotnet/tools/base64decode/

And the resultant file opens as a PDF and has my content.

Any reason that decode_base64() is not returning a readable file? The file it does write is very close in size, (402KB vs 398KB for a good file), and kdiff reports that they're ascii-identical, but not binary-identical. All the readable parts match. I'm on Windows, using Perl 5.10.1 and MIME::Base64 version 3.13.

Replies are listed 'Best First'.
Re: decode_base64 MIME PDF Failing
by BrowserUk (Patriarch) on Jun 26, 2011 at 23:55 UTC

    Change the open to:

    open my $fh, ">:raw", $name or die "$0: open $name: $!";

    Or use binmode on it before writing the data to the file.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Seriously? That's what it was? 95% of the time I'm on Linux, so I guess it makes sense that this is what would trip me up.

      That fixed it immediately. Thanks a lot.

        how to set the text in binary mode?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://911483]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found