Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Printing PDFs as a windows service

by stephanm (Sexton)
on Jun 23, 2010 at 13:56 UTC ( #846082=perlquestion: print w/replies, xml ) Need Help??
stephanm has asked for the wisdom of the Perl Monks concerning the following question:


I'm trying to run a service on Vista that prints any pdf file dropped into a directory. The program works when started by hand.

### print files dropped into this directory use strict; use File::Copy; while(1) { opendir(DIRH, "c:\\printpdf") or die "couldn't open: $!"; open(FH, ">log.txt") or die "Cannot open log file"; foreach (readdir DIRH) { ### only take pdf files next unless /pdf$/i; ### acroread needs full path my $file = "c:\\PrintPDF\\$_"; print FH "Found $file\n"; #system "c:\\apps\\AcroRd32.lnk /p \"$file\""; system "c:\\apps\\AcroRd32.lnk /N /T \"$file\""; ### not sure how long acroread keeps the file open sleep 10; move("$_", "Done") or die "move failed: $!"; } close(FH); closedir(DIRH); sleep 30; }
When installed as service the print does not happen. I can see that AcroRd32 gets started but then hangs.

I assume that is because a GUI program cannot be run as service.

I also tried a printing alternativ found on this site:

use Win32::API; my $shellopen = new Win32::API("shell32", "ShellExecute", ['N', 'P', 'P', 'P', 'P', 'I'], 'N'); $shellopen->Call(0, "print", $file, 0, 0, 0);
Same problem - works when started manually - hangs when started as service.

Does anyone known a way to print PDFs w/o having to start any type of GUI application?


Replies are listed 'Best First'.
Re: Printing PDFs as a windows service
by BrowserUk (Pope) on Jun 23, 2010 at 14:32 UTC

    You've failed to show us how you are "installing this as a service"?

Re: Printing PDFs as a windows service
by brap (Pilgrim) on Jun 23, 2010 at 14:55 UTC

    The folks at $work tried something similar to this (sans Perl, sadly), and, well, the results left something to be desired. One thing we found (and maybe this is what you're running into), is that the user profile that is going to run Acrobat Reader must have the "LocalLow" ({user-home}\AppData\LocalLow) directory created.

    I wonder if something like GhostScript wouldn't work better from a non-interactive standpoint? I haven't tried it, personally.

    Best of luck,

Re: Printing PDFs as a windows service
by petecm99 (Pilgrim) on Jun 23, 2010 at 18:47 UTC
    You may want to have a look at Win32::FileOp, using ShellExecute to print your pdf(s).

    For example, ShellExecute(print => 'C:/MyPdfFile.pdf');
      Thanks guys for your help.

      As to installing the service - I use a script I found on the net. It works so I dont worry about that.

      I have tried:

      ShellExecute(print => $file, 0, 0, 'SW_HIDDEN');
      This works manually and not as service. AcroRd32.exe is called by that class and it does never quit.

      I also installed ghostscript and tried:

      system "c:\\apps\\gswin32c.lnk -dPrinted -dBATCH -q -sDEVICE=mswinpr2 +-dNoCancel -dNOPAUSE \"$file\"";
      Works manually but not as service.

      Interestingly, a dialogue box comes up to OK or Cancel the print (which probably is the show stopper for the service to work).

      I assume if I can get the dialogue box to not appear I have a solution.

      According to some ghostscript forum the -dNoCancel options is supposed to remedy that but in my case it does not.

      I'll keep digging...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://846082]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-05-25 23:25 GMT
Find Nodes?
    Voting Booth?