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 | |
by kiteskitesyay (Initiate) on Jun 27, 2011 at 03:23 UTC | |
by Anonymous Monk on Jan 25, 2013 at 22:22 UTC |