Batch print pdf through commandline with sumatra pdf

余生颓废 提交于 2019-12-04 16:00:16

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!