print only last page with command line parameter
The problem
We use signature pads to sign some pdfs. Typically the fields to sign are on the last page. So we looked for a solution to just print out the last page after signing with the pad.
There are already lots of possibilities for the command line
-print-settings "1-3,5,10-8,odd,fit,bin=2"
but
-print-settings "last"
is missing
The solution
So i had a look at the code and added the missing parameter:
In SumatraDialogs.h change: enum class PrintRangeAdv { All = 0, Even, Odd, Last }; // ### LH added Range Last
In Print.cpp add:
line 468:
for (DWORD pageNo = pd.ranges.at(i).nFromPage; pageNo != pd.ranges.at(i).nToPage + dir; pageNo += dir) {
if ((PrintRangeAdv::Even == pd.advData.range && pageNo % 2 != 0) ||
(PrintRangeAdv::Odd == pd.advData.range && pageNo % 2 == 0) ||
(PrintRangeAdv::Last == pd.advData.range &&
pd.ranges.len == 1 &&
pageNo != pd.ranges.at(0).nToPage))
{
continue;
}
and then line 1157:
} else if (str::EqI(s, "odd")) {
advanced.range = PrintRangeAdv::Odd;
} else if (str::EqI(s, "last")) { // ### LH fuer SumatraPDF.exe -print-settings "last" -print-to "PDFCreator" test.pdf
advanced.range = PrintRangeAdv::Last;
} else if (str::EqI(s, "noscale")) {
Files are attached with extension .txt, as attaching .h and .cpp is not allowed
So if you think this new option is useful, just implement it.
SumatraPDF will "print as image" and thus similar without print would be something like "mutool/cpdf/ghostscript/pdfcpu convert to image last or end".
For "printing PDF as vectorised" I would always suggest the most efficient is GhostScript -firstpage=# -lastpage=# -ooutput -f input.pdf but you will first need to use pdfinfo to detect last page # .
Here is a proof of concept, based on my answer to https://stackoverflow.com/a/75070228/10802527
NOTE "as it is" will not export/print a -lastpage if there is only 1 in the source.pdf
@echo off
REM probably don't need setlocal for this cmd file but may be needed for other downstream command manipulation
setlocal enabledelayedexpansion
set "GS_BIN=C:\Users\WDAGUtilityAccount\Desktop\Apps\PDF\gs\10.05.1\bin"
"%GS_BIN%\gswin32c.exe" -q -dBATCH -dPDFINFO "%~dpn1.pdf" 2>"%temp%\output.txt"
for /f "usebackq tokens=3" %%f in (`type "%temp%\output.txt" ^| find "pages"`) do (set /a "PAGES=0+%%f")
"%GS_BIN%\gswin32c.exe" -q -sDEVICE=pdfwrite -dFirstPage=%PAGES% -dLastPage=%PAGES% -o"%~dpn1-lastpage.pdf" -f "%~dpn1.pdf" 2>nul
pause
del "%temp%\output.txt"
This solution is very specific for a certain use-case.
What if the last 2 pages are needed?
How about -print-settings "-1-3,...", just signal that the page numbers are counted from the end with a prefix ('-').
-1 = last page
-1-3 = last 3 pages
-2 = second-to-last page