Command line print last page option
I want to print just the last page of some PDFs with command line, since the page count varies, is it possible with Sumatra? Or will you add some options to do it in the future?
How this command-line option should look like?
There are a few apps that have a CLI syntax to handle edge case of n-1, but there is no convention, the simplest way is to use a tool like MuPDF to count pages then subtract X so something like
mutool info input.pdf | find "Pages:"
Pages: 2
That needs CMD conversion to set "Range=2" but that's outside SumatraPDF scope and usually done via FOR loop syntax
Range can then be changed in CMD for a total Pages:=100
to for example "97-100" if only last 4 pages are needed
then recycle the desired output to print request SumatraPDF -print-to-default -print-settings "%Range%" input.pdf
How this command-line option should look like?
Maybe something like this (z denotes the last page and r2 denotes reverse page 2): SumatraPdf.exe -print-to-default -print-settings "z" "sample.pdf"
There are a few apps that have a CLI syntax to handle edge case of n-1, but there is no convention, the simplest way is to use a tool like MuPDF to count pages then subtract X so something like
mutool info input.pdf | find "Pages:" Pages: 2That needs CMD conversion to
set "Range=2"but that's outside SumatraPDF scope and usually done via FOR loop syntax Range can then be changed in CMD for a total Pages:=100 to for example "97-100" if only last 4 pages are neededthen recycle the desired output to print request SumatraPDF -print-to-default -print-settings "%Range%" input.pdf
This is a workaround.
I also needed to reference the last page in my use case. SumatraPDF is already powerful, but I believe having an elegant way to do this would make it an even more robust command line tool.
How this command-line option should look like?
I propose that negative numbers should specify pages relative to the last page. Positive numbers should continue to specify actual page numbers relative to the first page. This is a slightly modified version of the syntax suggested by @erwindon in Issue #4967. Here's how it would look:
-1 prints the last page
-3--1 prints the last 3 pages in order, with the last page of the document being printed last
1--2 prints all but the last page
The Parse function in StrUtil.cpp, which parses the page ranges specified on the command line, can actually already handle negative numbers like this.
But there's a UX decision to make here: if a user specifies -print-settings -3--1,odd on a 4-page document, should page -3 be skipped because it corresponds to actual page number 2, which is even? Or should page -3 be printed because -3 is odd? To me the most straightforward way seems to have odd and even always apply to actual page numbers, but then again if a user is only specifying pages relative to the last page, it may be that the user doesn't know the actual page numbers at the end of the document, so I'm not sure about this one.
I welcome all feedback and I would be happy to submit a pull request on this.
@antakij2 I appreciate the suggestions but if the user is not using heads up internal printing where choice is easy but using command line then the usages are limited
My own humble opinion is there are far better ways of command line print using Ghostscript than SumatraPDF which is often used for the wrong type of printing. Certainly not suited to smaller subjects nor precision like labels or barcodes as Crude WinPrn2 driver (WinImage Default) should never be used for those.
SumatraPDF prints are perfect for home users like I am now. But for commercial extraction, duplex, reversal, etc then Ghostscript (or Acrobat for PDF) was my goto printing application as they have dedicated drivers and are both aimed at PS/PDF print with quality control.