Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Problem Encoding/Decoding PDF with MIME::Base64

by DamnDirtyApe (Curate)
on Apr 28, 2004 at 20:57 UTC ( [id://348952]=perlquestion: print w/replies, xml ) Need Help??

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

G'day, monks. I've been playing with SQLite alot lately, and I'm trying to find a way to add binary attachments to the database. The SQLite docs recommend Base64 encoding, so I thought I'd experiment with that a bit.

Here's my program to encode files in Base64:

#! /usr/bin/perl use strict; use MIME::Base64 qw( encode_base64 ); use IO::All; encode_base64( io( $ARGV[0] )->slurp ) > io( $ARGV[1] ); __END__

And the matching decode program:

#! /usr/bin/perl use strict; use MIME::Base64 qw( decode_base64 ); use IO::All; decode_base64( io( $ARGV[0] )->slurp ) > io( $ARGV[1] ); __END__

Here's what I tried to test it:

Z:\>perl encode_base64.pl yearsinservice.pdf yis.b64 Z:\>perl decode_base64.pl yis.b64 yis.pdf

When I try to open yis.pdf with Acrobat Reader, it says there's an error in the file. The original PDF works fine. Any suggestions?

UPDATE: I moved these PDFs to my Linux box and used hexdump -C to look at them - it looks like an endline issue (yis.pdf has Windows-style endlines). I tried adding local $/ = "\n"; to each file, but that didn't seem to help.


_______________
DamnDirtyApe
Those who know that they are profound strive for clarity. Those who
would like to seem profound to the crowd strive for obscurity.
            --Friedrich Nietzsche

Replies are listed 'Best First'.
binmode binmoding binmode again
by PodMaster (Abbot) on Apr 28, 2004 at 21:21 UTC
    Practically everytime I've seen someone release a module with a slurp function it has always lacked binmode (at first), and IO::All is no different. Know thy tools

    update: granted you could use open IN => ":raw", OUT => ":raw"; to set default disciplines for input and output, but a good slurp function should binmode.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Thanks PodMaster, that was it precisely. I rewrote the programs as follows:

      encode_base64.pl:

      #! /usr/bin/perl use strict; use MIME::Base64 qw( encode_base64 ); open INFILE, '<', $ARGV[0]; binmode INFILE; open OUTFILE, '>', $ARGV[1]; my $buf; while ( read( INFILE, $buf, 60 * 57 ) ) { print OUTFILE encode_base64( $buf ); } close OUTFILE; close INFILE; __END__

      decode_base64.pl:

      #! /usr/bin/perl use strict; use MIME::Base64 qw( decode_base64 ); open INFILE, '<', $ARGV[0]; open OUTFILE, '>', $ARGV[1]; binmode OUTFILE; my $buf; while ( $buf = <INFILE> ) { print OUTFILE decode_base64( $buf ); } close OUTFILE; close INFILE; __END__

      The PDF decoded correctly this time. Thanks!


      _______________
      DamnDirtyApe
      Those who know that they are profound strive for clarity. Those who
      would like to seem profound to the crowd strive for obscurity.
                  --Friedrich Nietzsche

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found