Category: Win32
Author/Contact Info Courage, the Cowardly dog
Description: This utility uses Win32::OLE module to manipulate Acrobat to print group of pdf files.

Here is what could be, in Courage's opinion, could be usefull from that code:

  • Those who sometimes need to perform output of several filesets (as, say, some multivolume manual could be managed to be printed in a single call), can use it for their purposes
  • Those who want to manipulate PDF files could see some possibility to do this in other way than some incomplete pure Perl solutions or more complicated C+Perl solutions
  • some Win32::OLE example will not be bad
  • ... and anything else I've forgot to mention about :)
Note: this script requires Acrobat to be installed, Acrobat Reader is not enough!
sub BEGIN {
$App::Author = 'Vadim V. Konovalov';
$App::Name = 'print-pdf';
$App::Version = '0.1';
$App::Description = <<'EOS';
prints PDF file(s) via Acrobat via OLE technology
EOS
$App::Arguments = <<"EOS";
  --no-print -- to skip printing
  --no-log -- to skip log file creation
  --log=[ofile] -- output log, default=log-print.txt
  --dir=[idir] -- directory, where required files are; default=current
  files to be processed
EOS
$App::TODO = <<"EOS";
1. implement --quit, which will optionally quit acrobat.
EOS
$App::ChangesHistory = <<'EOS';
0.1: first version
EOS
}

use strict;
use Cwd;

if ($#ARGV==-1) {
  print STDERR <<"EOS";
$App::Name version $App::Version

Arguments:
$App::Arguments
$App::Description
EOS
  exit;
}

my %opts = (
  # %known_opts enumerates allowed opts as well as specifies default
  #   and initial values
  my %known_opts = (
     'debug' => 0,
     'dir' => '.',
     'verbose' => 1,
     'print' => 1,
     'log' => 'log-print.txt',
  ),
  #options itself
  my %specified_opts = (
    (map {/^--([\-_\w]+)=(.*)$/} @ARGV),                            # 
+--opt=smth
    (map {/^no-?(.*)$/i?($1=>0):($_=>1)} map {/^--([\-_\w]+)$/} @ARGV)
+,  # --opt --no-opt --noopt
  ),
);
die "option '$_' is not recognized" for grep {!exists $known_opts{$_}}
+ keys %specified_opts;
@ARGV = grep {!/^--/} @ARGV;

my $fhlog;
if ($opts{log}) {
  unless (open $fhlog, ">>$opts{log}") {print STDERR "Can not open log
+ file $opts{log}: $!\n"}
  print $fhlog "# date=".scalar(localtime)."\n";
  print $fhlog "# dir=$opts{dir}\n";
}

chdir $opts{dir} or die "can't chdir to '$opts{dir}'";

my @files = map{s/\//\\/g;$_} map {!/^\w:[\\\/]/?cwd.'/'.$_:$_} map {g
+lob} @ARGV;

use VK::Acrobat;

my $acro = new VK::Acrobat(-verbose=>0);
my $abat = $acro->{acro};
my $adoc = $acro->{acrodoc};
for my $fn (@files) {
  unless (-e $fn) {
    print STDERR "file $fn does not seem to exist!";
    #next;
  }
  #$abat->Show;
  (my $tit=$fn) =~s/^.*?([^\\\/]+)$/=$1=/;
  my $rc = $adoc->Open($fn,$tit);
  #print STDERR "rc=$rc\n";
  if ($rc==0) {
    print STDERR "ERROR!!!\n";
    next;
  }
  my $pddoc = $adoc->GetPDDoc;
  my $pp = $pddoc->GetNumPages;
  #print "pp=$pp\n";
  print STDERR "[$fn][$pp] printing ...\n";
  if ($fhlog) {
    print $fhlog "fn=$fn    pp=$pp\n";
  }
  if ($opts{print}) {
    $adoc->PrintPages( #Silent
      0,      # long nFirstPage, 
      $pp-1,  # long nLastPage, 
      3,      # long nPSLevel, 
      0,      # long bBinaryOk, 
      0,      # long bShrinkToFit, 
      #0,      # long bReverse, 
      #0,      # long bFarEastFontOpt, 
      #0,      # long bEmitHalftones, 
      #0,      # long iPageOption);
    );
  }
  $adoc->Close(1);
}
Replies are listed 'Best First'.
Re: pdf files print utility on Win32
by Courage (Parson) on Aug 02, 2002 at 21:36 UTC
    ... and here is contents of my VK::Acrobat module which is needed to this utility.
    package VK::Acrobat; use strict; use Win32::OLE qw(EVENTS); use Win32::OLE::Const; use Win32::OLE::Variant; use Cwd qw(cwd abs_path); use File::Basename; use File::Path; sub new { my $class = shift; # || SUPER::??????? как тут правильно? my $self = bless { # defaults verbose => 1, # some predefined (and overridable) constants 'ole-name' => 'AcroExch.App', 'ole-doc-name' => 'AcroExch.AVDoc', 'typelib-name'=>'Adobe Acrobat 5.0 Type Library', # some predefined constants true => Variant(VT_BOOL,1), false => Variant(VT_BOOL,0), # initial values #... # specified values @_, }, $class; my $acro = Win32::OLE->new($self->{'ole-name'}, sub { #print STDERR "quitting Acrobat...\n" if $self->{verbose}; ##$_[0]->Exit; #print STDERR "done.\n" if $self->{verbose}; }) or die "Can't init $self->{'ole-name'}; please re-install Acrobat +"; my $acrodoc = Win32::OLE->new($self->{'ole-doc-name'}, sub { #print STDERR "quitting Acrobat-doc...\n" if $self->{verbose}; #print STDERR "done.\n" if $self->{verbose}; }) or die "Can't init $self->{'ole-name'}; please re-install Acrobat +"; #$self->{OLEconst} = Win32::OLE::Const->Load($acro); $self->{OLEconst}->{true} = $self->{true} unless exists $self->{OLEc +onst}->{true}; $self->{OLEconst}->{false} = $self->{false} unless exists $self->{OL +Econst}->{false}; $self->{OLEconst} = Win32::OLE::Const->Load($self->{'typelib-name'}) +; $self->{warner} = sub { print STDERR "ACROBAT Warner: [[".(join '===',@_)."]]\n"; }; $self->{acro} = $acro; $self->{acrodoc} = $acrodoc; Win32::OLE->Option(Warn=>$self->{warner}); #Win32::OLE->WithEvents($self->{acro}, \&Event); return $self; } sub Event { my ($Obj,$Event,@Args) = @_; print "Event triggered: '$Event'\n"; } =ignore sub AUTOLOAD { print STDERR "AUTOLOAD:[[".(join "+",@_)."]]\n"; &SUPER::AUTOLOAD; } =cut 1;

    Courage, the Cowardly Dog.