Batch-Printing on Command-line via Print-Driver

Situation
  1. You have: a PDF containing about 10.000 pages.
  2. You need: a file that can be sent to your printer (Postscript, PCL, whatever)
Condition
batch-command, no manual interaction with Acrobat

Possible solutions:
Ghostscript
gswin64 -sDEVICE=pswrite -res300 -sPAPERSIZE=a4 -dBATCH -dNOPAUSE -sOutputFile=C:/temp/new.ps input.pdf
A very good solution for PDFs of "normal" count of pages. A conversion of 10.000 pages may last one hour or more depending on the complexity of PDF file

pdftops.exe from foolabs (http://foolabs.com/xpdf/)
Very powerful tool for converting a pdf to postscript with many setting possibilities. Very fast! Fulfils your needs in most cases. I had one problem: my PDFs contained some thousand fonts. Pdftops.exe stores ALL fonts in the postscript file before the first page. My printer had problems to rip this amount of font in one slurp and stopped working. My solution was to divide the mass of fonts at the beginning and spread them over the subsequent pages. Perl was an excellent tool for this. Unfortunately I got more and more „foreign“ PDFs from customers their creation I could not influence. So it happend, that in some pdfs some characters and symbols were not transferred in a correct way. Instead of the symbol „trademark“ I got a black rectangle.

Building postscript by macro recorder.
I installed a software that records all mouse- and keyclicks that are necessary to print a PDF into a postscript file. This is a complete simulation of the manual work you normally do when you print a PDF via printerdriver. You select “Print to file” in the printer interface. The postscript produced that way is by normally the best for your printer and gives you the best results. BUT it can be slow and it is not secure. Automatism in this way may or may not work.

Benchmark
PDF-File containing 326 pages, filesize 20MB.
Program                                 Duration         Size of PS-File
Ghostscript (version 9.04)        2:59 min            490 MB
Acrobat (Acrobat X)                 1:01 min            149 MB
pdftops.exe (3.03)                   10 sec(!)              61 MB

This benchmark shows the excellent performance of pstotext.exe of foolabs. There are many other solutions that produce a postscript file in some way. Some use ghostscript behind the scenes, some use pdftops.exe and some use a kind of simulation of manual printing process via Acrobat. It goes without saying that the result will be about the same as shown above.

What to use?
Ghostscript – not in this case. To make it clear: I love ghostscript and I use it on many occasions. If you have to handle PDFs with some pages it is by far the best solution. In the real print production with 100 thousands of pages it is not the right tool.
Pdftops.exe – best product, very fast! But I had problems in converting some characters. It has to be said, that my PDFs do not contain the “easy-cheese” PS-Type1 fonts but Truetype (CIT) and coding Identity-H.
In UNIX environment (Linux, Solaris) you could use acrobat.exe.
Started on the commandline in the way
/opt/Adobe/Reader9/bin/acroread –toPostScript –rotateAndCenter –saveVM –optimizeForSpeed –size a4 –start 1 –end 9999 PDF-File
would give you a postscriptfile of good quality. But we are here in the windows world.

My solution: PERL

Use perl, Win32::OLE. Reading “Win32 Perl Programming” of Dave Roth (http://www.roth.net) opens a new set of tools of automatism. Using Win32::OLE you can remote control nearly everything. Manipulate word-, excel-files? you can do it. Also Acrobat can be remote controlled like in this code:
(program print.pl)

#!perl
use strict;
use warnings;

use Win32::OLE;

my $pdfin = "C:/temp/test.pdf";
my $psout_js = "/C/temp/new.ps"; # Syntax for javascript
my $psout_std = "C:/temp/new.ps"; # Syntax for perl
my $printername = "Xerox iGen4 PS"; # Exact description of printer seen in controlpanel

# You have to delete the output before
unlink( $psout_std );

# Open Acrobat
my $pddoc = Win32::OLE->new("AcroExch.PDDoc");
if( !$pddoc->Open( $pdfin ) ) {
&error_out();
exit;
}
my $jso = $pddoc->GetJSObject;

# Give information about the document
print STDERR "Filename:", $jso->documentFileName, "\n";
print STDERR "Filesize:", $jso->filesize, " Bytes\n";
print STDERR "Pages :", $jso->numPages, "\n";
print STDERR "Path :", $jso->path, "\n";
print STDERR "Info-Producer:" . $jso->info->{'Producer'} . "\n";

# Prepare for printing into a file
# Get printparams and change them according to your needs
my $pp = $jso->getPrintParams();
$pp->{'printerName'} = $printername;
$pp->{'fileName'} = $psout_js;

# Print from page 3 to end
#$pp->{'firstPage'} = 2;

# Suppress user interface and progress indicator
$pp->{'interactive'} = $pp->{'constants'}->{'interactionLevel'}->{'silent'};

# Font on every page
$pp->{'fontPolicy'} = $pp->{'constants'}->{'fontPolicies'}->{'jobStart'};

# Give me only even pages
# $pp->{'pageSubset'} = $pp->{'constants'}->{'subsets'}->{'even'};

my $ret = $jso->print($pp);
if( $ret == 1 ) {
print "Output has been written to $psout_std\n";
} else {
print STDERR "Problems on printing:\n";
&error_out();
}
$pddoc->Close();

sub error_out {
print STDERR "LastError=" . Win32::OLE->LastError;
}
After starting with:
c:\mydir>perl print.pl
you can watch the printprogress in the queue-window of the according printer you set at variable $printername.

The perl-OLE-program is fast enough. Not as fast as pdftops.exe. Of course you can not only create postscript files but also PCL-files. Whatever printer you choose the according print file will be produced.

All settings can be seen in the documents: Javascript Developer Guide
and more detailed in Javascript API Reference (look for method print)

Leave a comment

About Rudolf

user-pic Perl and the experiences I get everyday working with it. I use perl for everything. Web-developement, GUI, PDF manipulation, printing etc.