问题
since Adobe released a new Update for the Adobe Reader (11.09 on 16.09.2014), we encountered an problem with printing pdf files through the command line.
Our formally script looks like this:
:job
@for /F "tokens=1,* delims= " %%p in ('dir /b/a-d /s "Y:\*.pdf"') do (
echo Printing file: "%%p" on %date%
::prints the pdf document with adobe reader
"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /p /h "%%p"
::move file to destination
echo wait 10 seconds
::wait 10 seconds then continue
ping 127.0.0.1 -n 10 > nul
echo move file: "%%p" to "C:\PDF"
::move file to destination
move "%%p" "C:\PDF"
echo moved successfully!
echo proceeding next file
)
echo waiting for files to print...
echo wait 7 seconds
ping 127.0.0.1 -n 7 > nul
::clear console
cls
::repeat with job (endless loop)
goto job
The script was working as expecting before. Since the update the script often hangs or is mega slow. the script got more or less useless.
Cause the adobe reader is probably not the smallest, we also considered to use an lightweight alternativ like sumatrapdf or foxitreader. This would be a great point to start using one of those alternatives.
So I wanted to try it with sumatrapdf. Their documentation said:http://blog.kowalczyk.info/software/sumatrapdf/manual.html
=> -print-to-default $file.pdf prints a PDF file on a default printer
so ive come up to this, analog to the adobe reader example
:job
@for /F "tokens=1,* delims= " %%p in ('dir /b/a-d /s "Y:\*.pdf"') do (
call "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
start "" "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
"C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
... the rest
But nothing works. If have also tried: "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe -print-to-default" "%%p" for call,start, and so on...
Does anyboy know how to make the printing work with sumatrapdf? I also wouldn't be averse by making it work with foxit reader.
It would be a pleasure to get some advices :) Maybe someone got a similar problem to solve.
回答1:
Adobe Reader
According to How do I use the Windows command line with Acrobat and Adobe Reader? (which is for Adobe Acrobat/Reader 10 and not XI) the command to use for printing with displaying a dialog box is:
start "Print PDF" /wait "%ProgramFiles(x86)%\Adobe\Reader 11.0\Reader\AcroRd32.exe" /p "%%p"
I think, it is not useful to use option /h
to start Adobe Reader with a minimized window if the print dialog box should be displayed.
To print to a specific printer the command line to use would be something like
start "Print PDF" /wait /min "%ProgramFiles(x86)%\Adobe\Reader 11.0\Reader\AcroRd32.exe" /t "%%p" "printername" "drivername" "portname"
/wait
halts execution of batch job until Adobe Reader terminated itself. And option /min
results in executing the GUI application with a minimized window if the application does not override it and displays nevertheless the window maximized or in restored mode.
SumatraPDF
I'm not sure if SumatraPDF Manual is up-to-date because it links at bottom to a Wiki documentation for Sumatra PDF with Command Line Arguments page. This Wiki page was recently updated and contains different information regarding to printing options.
- -print-to-default
Prints all files indicated on this command line to the system default printer. After printing, SumatraPDF exits immediately (check the error code for failure).
So it should be possible to print even multiple files specified on command line with one call of SumatraPDF which would make your batch file more efficient.
As I have not installed SumatraPDF, I suggest to try first a batch file with just the 3 lines
"%ProgramFiles(x86)%\SumatraPDF\SumatraPDF.exe" -print-to-default "Full Path and Name of a PDF file"
echo Exit code of SumatraPDF is: %ERRORLEVEL%
pause
Next look on output of batch file. Is an error displayed or is the PDF file printed as expected?
Is the second line output in console window before printing finished?
If this is the case, SumatraPDF is started as a separate process and exit code evaluation is not really possible. Otherwise you have already the command to use.
But if SumatraPDF is started as a separate (GUI) process, it is most likely necessary to use command start
with option /wait
and perhaps also /min
as shown above for Adobe Reader.
Note: There is no @echo off
at top of the small batch file for testing to see also the commands on execution of the batch file.
FOR loop
Looking on your code for the FOR
loop there is something wrong as you specify the space character as delimiter which is not good for file paths/names with a space inside. Also the space character is by default a delimiter for command FOR
and therefore it is not necessary to explicit define it as delimiter if that is really wanted.
Therefore I suggest to use
@for /F "delims=" %%p in ('dir "Y:\*.pdf" /b /a-d /s' 2^>nul) do (
"%ProgramFiles(x86)%\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
)
With "delims="
the delimiters list is changed to newline only. %%p
has therefore always the entire path and name of a file even if 1 or more spaces exist on a line output by command dir
.
2^>nul
suppresses the error message of command dir
if no PDF file could be found at all on entire drive Y: by redirecting the error message to device NUL.
来源:https://stackoverflow.com/questions/25936650/batch-print-pdf-through-commandline-with-sumatra-pdf