Perl-Script: PDF 2 Comic Book Archive Converter

J

jakob

Neues Mitglied
1
Ich finde den HTC-G1 eigentlich sehr praktisch, um sich Vortragsfolien anzuschauen. Leider gibt es jedoch noch keinen PDF-Reader, mit dem dies einfach möglich wäre. Ich habe deshalb ein Skript geschrieben, dass aus einer PDF-Datei ein Comic Book Archive erstellt - dass ist eine einfache ZIP-Datei, in der die einzelnen Seiten sortiert als Bitmaps (PNG oder JPEG) enthalten sind.

Ich suche noch einen Open Source Comic Reader für Android, fürs erste tut es auch zum Beispiel der ACV (Droid Comic Viewer) von robotcomics.net. Das Skript benötigt Perl und Ghostview, ich habe es bisher nur unter Linux getestet. Praktisch wäre noch eine Server-Variante, wo man einfach das PDF hinschickt und die CBZ/ACV/ZIP o.Ä. Datei zurückbekommt. Als Standard wird ein PDF mit A4 Querformat angenommen und 480x320 Pixel Bilder in PNG erzeugt, aber das lässt sich per Kommandozeilenparameter auch anders angeben.

Wenn übrigens etwas auf den Folien nicht lesbar ist, weil es zu klein ist, liegt das nicht am Skript oder an der Auflösung des HTC-G1 sondern daran, dass die Folien schlecht gestaltet wurden, weil zuviel zu kleiner Text darauf gepackt wurde anstatt höchstens 3-4 Listenpunkte.

PHP:
#!/usr/bin/perl

=head1 NAME

pdf2comic - convert pdf and postscript files to comic book archives

=cut

use strict;
use warnings;

use File::Temp qw(tempdir);
use File::Spec;

my $debug = 1;

# parse command line parameters

my $force = $ARGV[0] && $ARGV[0] eq '-f' ? shift : 0;
my $infile = shift;
my $outfile = shift;

($infile and -f $infile and $outfile) 
    or die("Usage: pdf2comic [-f] <pdf> <outfile> [options]");

(not $force and -f $outfile)
    and die("output file already exists, use -f to overwrite");


my ($jpegq);
my $device = "png16m";
my $ext = "png";
my ($outx,$outy) = (480,320);     # output size in pixels
my ($width,$height) = (11.7,8.3); # paper size in inch

foreach (@ARGV) {
    if ( $_ =~ /^png$/i ) {  # png, jpg, jpeg
        $device = "png16m"; $ext = "png";
    } elsif( $_ =~ /^jpe?g$/i ) {
        $device = "jpeg"; $ext = "jpg";
    } elsif( $_ =~ /^(\d+)%$/ ) { # n% : JPEG quality
        $jpegq = $1 if $1 > 0 and $1 <= 100;
    } elsif( $_ =~ /^(\d+)x(\d+)$/) { # output size in pixels
        ($outx,$outy) = ($1,$2) if $1 > 0 and $2 > 0;
    } elsif( $_ =~ /^a4$/i ) {
        ($width,$height) = (11.7,8.3); # A4 format
    } elsif( $_ =~ /^letter$/i ) {
        ($width,$height) = (11.0,8.5); # letter format
    }
    # TODO: color depth (png16,png16m,png256)
    # TODO: Anti-aliasing (1=off,2,4)
}

# initialize

my $tempdir = tempdir( CLEANUP => 1 );
my $filepattern = 'page%03d.'.$ext;

# Anti-aliasing (1=off,2,4)
my $aa = 4; 

my @params;
push @params, "-SDEVICE=$device";

# output size and resulution in pixels per inch
push @params, sprintf('-g%dx%d',$outx,$outy);
push @params, sprintf('-r%0.2fx%0.2f', $outx/$width, $outy/$height);

# anti-aliasing
push @params, "-dTextAlphaBits=$aa", "-dGraphicsAlphaBits=$aa";

# JPEG Quality
push @params, "-dJPEGQ=$jpegq" if $jpegq;

# output and input file
push @params, "-sOutputFile=$tempdir/$filepattern";
push @params, "-dNOPAUSE", "-dBATCH", "--", $infile;


# run

print join("\n",@params) . "\n" if $debug;
system('gs',@params) == 0 or die("Failed to convert with ghostscript");

my $cwd = File::Spec->rel2abs(File::Spec->curdir());
chdir $tempdir;
my @files = <*>;

system (qw{zip -q -D}, File::Spec->rel2abs($outfile,$cwd), @files) == 0
    or die("Failed to create zip file");

print "created $outfile\n";

=head1 DESCRIPTION

This script converts a PDF document into a comic book archive file - that
is an archive of sequentially sorted image files. The script depends on
ghostscript and zip which must be on your environment.

pdf2comic was created to PDF files to easily view slides on a HTC-G1 mobile
device, but you can convert any other PDF. By default a resolution of 320x480
is used for output and A4 paper size for input. The HTC-G1 has a 3,2" display
with 180ppi so you should at least use 20pt font size.

=head1 EXAMPLES

Default settings, create zip file
  pdf2comic myslides.pdf myslides.zip

JPEG 90% quality, create cvz file
  pdf2comic myslides.pdf myslides.cbz jpeg 90%

Slides in A4, create PNGs high resolution acv file
  pdf2comic myslides.pdf myslides.acv 920x640 png a4

=head1 TODO

The paper size could automatically be determined because it is stored
in the PDF/PS file. This way we could also handle different orientation.

Support rar archives (cbr) in not included yet.

=head1 LICENSE

This is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
 
Zuletzt bearbeitet von einem Moderator:
Also um PDFs zu lesen, kann man Jetced PDF benutzen. Damit kann man auch Zoomen.
 

Ähnliche Themen

DOT2010
Antworten
5
Aufrufe
323
Klaus986
K
LuckyKvD
Antworten
7
Aufrufe
249
LuckyKvD
LuckyKvD
F
Antworten
1
Aufrufe
348
holms
holms
Zurück
Oben Unten