Printing from command line


#1

Hi, we print our documents with command like this

sumatrapdf -exit-on-print -print-to “my-printer” my-document.pdf

and it works most of the time.

But suddenly sumatra displays “Cannot print file”.

Trying the very same command again works perfectly.

Is there a logfile where we can find the reason why the document was not printed ?

Or does sumatrapdf give a “returncode” that there was a problem with printing ?


#2

Looking at the code (https://github.com/sumatrapdfreader/sumatrapdf/blob/1f754eec0687e58c33400925d665240c3c6df3aa/src/Print.cpp#L798) “Cannot print this file” happens when we can’t open / process file.

If it’s intermittent I would blame anti-virus software for messing with file access.


#3

Hi,

thanks for your answer.

But is there a way that I can react to this problem ?

If my batchfile would know there was a problem it could wait a second and try to print the file again.


#4

When printing fails, the return code is 1. If it doesn’t, it’s 0.


#5

Hi,

perfect, works like a charm !

Here is the solution I came up with (suggestions for improvement are welcome):

@echo off

set count=1
set silent01=1

:start

if %silent01% == 1 goto silent

SumatraPDF.exe -exit-on-print -print-to %1 %2

goto finish

:silent

SumatraPDF.exe -silent -exit-on-print -print-to %1 %2

if errorlevel 1 goto error
goto finish

:error

timeout /t 3

set /a count=%count%+1

if   %count% geq 5 set silent01=0

goto start

:finish