Skip to content

Commit c1de50f

Browse files
improved pre-release handling
1 parent 2d22a0e commit c1de50f

File tree

2 files changed

+176
-15
lines changed

2 files changed

+176
-15
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
@Mode 60,17 & Color 9F & Title SumatraPDF addin ExportPng [.eXt2.png(s)] v'21-03-18--07
2+
@echo off & SetLocal EnableDelayedExpansion & pushd %~dp0 & goto MAIN
3+
Do not delete the above two lines since they are needed to prepare this script.
4+
5+
Potted version history v'21-02-18--01 first public release (default is -r 96 dpi)
6+
v'21-02-18--06 very minor edits addins (..was wrong way round..) also 1-N
7+
is prefered and better acceptance of multiple ranges such as “6-10,12,18,21-25”
8+
9+
Read Me 1st (you can strip out most of these comments in your working copy)
10+
Note: Later lines that start with :LETTERS are branches that need to be kept BUT
11+
any Later lines that start with : And a space, are inline comments that may be REMoved
12+
13+
SumatraPDF script to EXPORT file page(s) as PNG using MuTools whilst viewing.
14+
TODO Optional use of current related GhostScript to allow for other formats.
15+
TODO Allow for use when -p password is required
16+
TODO allow for -R angle or -h -w (scaling)
17+
18+
The aim is to not affect original file. When testing they appear to be unaffected.
19+
But I give no guarantees, as on occasion MuTool may attempt to fix a bad file.
20+
TODO use a temporary copy and delete once done.
21+
22+
Current MuPDF tools are available from https://www.mupdf.com/downloads/index.html
23+
https://mupdf.com/downloads/archive/mupdf-1.18.0-windows-tesseract.zip
24+
or https://mupdf.com/downloads/archive/mupdf-1.18.0-windows.zip (recommended)
25+
26+
Methodology
27+
28+
This script pases the current FileName and PageRange to MuPDF\MuTool.exe
29+
ONLY .PNG OUTput is offered (NO Jpg, but others are possible, Read the Man)
30+
Input of many types including ePub or (o)xps and GIF/JPG/TIFF are allowed but
31+
no guarantee they will all be processed. Intended as an addin to SumatraPDF, but...
32+
When installed in the correct addins location you can right click this file and "SendTo
33+
Desktop as shortcut" where you can ALSO use it for drag and drop (max=ONE file)
34+
35+
CAUTION due to reflow differences, ePub and FB2 page numbers and appearance will
36+
vary wildly from MuPDF compared to those in SumatraPDF. To get a better sync (but
37+
output will never be the same) you can vary font size / name in SumatraPDF-settings.txt.
38+
39+
Presumptions (letter case should not matter, but relative positions do)
40+
41+
1) THIS FILE (ExportPng.CMD) is located in a folder ...\SumatraPDF\Addins\ExportPng\
42+
2) there is an %addins% location stored OR set in the users Environment Variables
43+
44+
A reminder about %addins% (skip this section if you have already added other "addins")
45+
This cmd script is intended to be stored in a folder relative to SumatraPDF-settings.txt
46+
However when run, that location can be different for every user. Thus in order that multiple
47+
"addins" can be found together they are stored in subfolders of ...\SumatraPDF\Addins.
48+
When run, the system needs to know where the addins folder is, so we need to SET a
49+
system-wide environment variable e.g. SET addins=D:\location of\SumatraPDF\addins
50+
There is no need to add " " marks but it needs to be SET PRIOR to starting SumatraPDF.
51+
For many "portableapps" that function may be done as part of their start-up mechanism.
52+
The simplest way to set user env settings in a static system is to start Edit Env...
53+
and accept "Edit Environment variables for your account" where you can use Edit > New
54+
Variable name: (key in) addins
55+
Variable value: (key in or SAFER is browse directory) e.g. c:\myapps\sumatrapdf\addins
56+
Once done don't forget to select OK
57+
For a USB start-up batch file use something like SET addins=%~d0Apps\sumatrapdf\addins
58+
59+
3) A recent copy of mutool.exe must be in ...\SumatraPDF\Utils\MuPDF\
60+
Note that there are additional files supplied in both of the 2 latest windows zip
61+
files from the download links above. However, you should ONLY need one, mutool.exe
62+
63+
4) The SumatraPDF-settings.txt is in the folder ...\SumatraPDF\
64+
65+
5) Most important THAT is NOT C:\Program Files\SumatraPDF\ However,
66+
%LOCALAPPDATA%\SumatraPDF\ or A:\PORTABLE\ folder SHOULD be ok,
67+
for multi-user you would need to change ..\..\Utils\ to a common fixed location.
68+
69+
6) An entry in advanced SumatraPDF-settings.txt is needed as similar to this
70+
71+
ExternalViewers [
72+
[
73+
CommandLine = c:\windows\system32\cmd.exe /c ""%addins%\ExportPng\ExportPng.cmd" "%1" page=%p"
74+
Name = Save current page &Graphics to PNG
75+
Filter = *.*
76+
]
77+
]
78+
79+
If you wish to always modify a range of pages and on occasion just single pages then
80+
a) Remove the %p from the command line (remember to keep " at end e.g. %1" page=")
81+
b) Change in Name = from "current page" to "page range"
82+
alternative is to have 2 different "viewer" blocks but then you would need a second shortcut
83+
84+
7) The shortcut for above will be ALT + F + G (Note Alt+F+many others = used or reserved)
85+
86+
End of readme / notes
87+
----------------
88+
:MAIN
89+
: First check for the required mutool support file and that filename is valid
90+
: if you have not placed mutool where recommended as is relative to this
91+
: file which was set at start then you may need to adjust both here and later
92+
: TL;DR this test should not be needed but for those users that don't RTFM
93+
if "%addins%"=="" set addins=%~dp0..
94+
if not exist "..\..\Utils\MuPDF\mutool.exe" echo: & echo Either MuTool.exe or this file are not in correct location & goto HELP
95+
if not exist "%~f1" echo: & echo "%~dpn1%~x1" & echo Appears NOT to exist as a valid file & goto HELP
96+
97+
: IF you wish to add or restrict input to only certain extensions then edit the file
98+
: extensions in the next line. NOTE .bmp, .jpg and .png are NOT multi-page
99+
for %%X in (bmp,cbz,epub,fb2,gif,jpg,oxps,pdf,png,tif,tiff,xps,zip) do IF /I .%%X.==%~x1. goto ALLOWED
100+
echo %~x1 Appears to be unacceptable & goto HELP
101+
102+
:ALLOWED
103+
: Very basic check if %2=Page[s] , case should not matter, with/without s
104+
for %%p in (-P, -Page, -Pages, Page, Pages) do if /i %2.==%%p. goto PASS
105+
: For use with drag and drop a file, we also pass for input of range
106+
if %2.==. goto RANGE
107+
108+
:HELP
109+
echo:
110+
echo Example Usage : ExportPng "C:\pdfs\in.pdf" pages=2-5
111+
echo:
112+
echo You can specify just one single page, e.g. Page=15 or
113+
echo a range like Pages=5-10 etc. or page=1-N (ALL pages)
114+
echo:
115+
echo See https://www.mupdf.com/docs/manual-mutool-draw.html
116+
echo for other options.
117+
echo:
118+
echo Call page without values to manually enter a page-range
119+
echo:
120+
echo ^> ExportPng "C:\pdfs\in.pdf" page (Note s^&= are optional)
121+
echo:
122+
pause & exit
123+
124+
:PASS
125+
: There should be no error if the file is held open in SumatraPDF
126+
: but later may need to add an info or similar test for a standalone file
127+
: There is little check if the Pages=range is valid so beware what is acceptable,
128+
if %3.==. goto RANGE
129+
: However, if given, lets remind user which page(s) was / were requested
130+
set pages="%3"
131+
for %%R in (All,1-N) do if /i %3.==%%R. set pages=1-N
132+
echo:
133+
echo Page Range requested: page(s) = %pages% & goto RUN
134+
135+
:RANGE
136+
echo:
137+
echo Enter page number or page-range or "r-a,n-g,e-s" (abort=0)
138+
set /p pages="Range = "
139+
if %pages%.==0. exit /b
140+
141+
:RUN
142+
: All should be now OK to run MuTool with <filename> <pages>
143+
echo:
144+
echo Exporting page(s)=%pages% as
145+
echo "%~dpn1-Page-####.png"
146+
echo:
147+
148+
: IMPORTANT default for png images output is highly recommended as -r 96
149+
: BUT, if intended use is for OCR later, then it should be higher e.g. -r 300
150+
:
151+
"..\..\Utils\MuPDF\mutool.exe" draw -r 96 -o "%~dpn1-Page-%%4d.png" "%~f1" "%pages%"
152+
echo:
153+
: pause
154+
: Optional, you can comment, change or delete timeout if not wanted (currently 5 seconds)
155+
timeout /t 5
156+

AppNotes/SumatraPDF/Addins/ExportPng/ExportPng.cmd

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Do not delete the above two lines since they are needed to prepare this script.
55
Potted version history v'21-02-18--01 first public release (default is -r 96 dpi)
66
v'21-02-18--06 very minor edits addins (..was wrong way round..) also 1-N
77
is prefered and better acceptance of multiple ranges such as “6-10,12,18,21-25”
8+
v'24-03-20--07 very minor edits recommend use 1.20 for last known 32bit compatability
9+
improve file handling for "filenames with spaces"
10+
* simplify to mutool be in this folder or vise versa
11+
Allow for current Pre-release prefers """ in cmd lines as opposed to normal "
812

913
Read Me 1st (you can strip out most of these comments in your working copy)
1014
Note: Later lines that start with :LETTERS are branches that need to be kept BUT
@@ -20,25 +24,25 @@ Do not delete the above two lines since they are needed to prepare this script.
2024
TODO use a temporary copy and delete once done.
2125

2226
Current MuPDF tools are available from https://www.mupdf.com/downloads/index.html
23-
https://mupdf.com/downloads/archive/mupdf-1.18.0-windows-tesseract.zip
24-
or https://mupdf.com/downloads/archive/mupdf-1.18.0-windows.zip (recommended)
27+
https://mupdf.com/downloads/archive/mupdf-1.20.0-windows-tesseract.zip (recommended as last known 32 bit version)
28+
or https://mupdf.com/downloads/archive/mupdf-1.20.0-windows.zip (without OCR)
2529

2630
Methodology
2731

2832
This script pases the current FileName and PageRange to MuPDF\MuTool.exe
29-
ONLY .PNG OUTput is offered (NO Jpg, but others are possible, Read the Man)
33+
ONLY .PNG OUTput is offered (NO Jpg, but others are possible, Read the Manual)
3034
Input of many types including ePub or (o)xps and GIF/JPG/TIFF are allowed but
3135
no guarantee they will all be processed. Intended as an addin to SumatraPDF, but...
3236
When installed in the correct addins location you can right click this file and "SendTo
3337
Desktop as shortcut" where you can ALSO use it for drag and drop (max=ONE file)
3438

35-
CAUTION due to reflow differences, ePub and FB2 page numbers and appearance will
39+
CAUTION due to reflow differences, ePub and FB2 page numbers and appearance may
3640
vary wildly from MuPDF compared to those in SumatraPDF. To get a better sync (but
3741
output will never be the same) you can vary font size / name in SumatraPDF-settings.txt.
3842

3943
Presumptions (letter case should not matter, but relative positions do)
4044

41-
1) THIS FILE (ExportPng.CMD) is located in a folder ...\SumatraPDF\Addins\ExportPng\
45+
1) THIS FILE (ExportPng.CMD) is located in same folder with Mutool.exe in the same folder
4246
2) there is an %addins% location stored OR set in the users Environment Variables
4347

4448
A reminder about %addins% (skip this section if you have already added other "addins")
@@ -56,28 +60,28 @@ Presumptions (letter case should not matter, but relative positions do)
5660
Once done don't forget to select OK
5761
For a USB start-up batch file use something like SET addins=%~d0Apps\sumatrapdf\addins
5862

59-
3) A recent copy of mutool.exe must be in ...\SumatraPDF\Utils\MuPDF\
63+
3) A recent copy of mutool.exe must be in same folder
6064
Note that there are additional files supplied in both of the 2 latest windows zip
6165
files from the download links above. However, you should ONLY need one, mutool.exe
6266

6367
4) The SumatraPDF-settings.txt is in the folder ...\SumatraPDF\
6468

6569
5) Most important THAT is NOT C:\Program Files\SumatraPDF\ However,
6670
%LOCALAPPDATA%\SumatraPDF\ or A:\PORTABLE\ folder SHOULD be ok,
67-
for multi-user you would need to change ..\..\Utils\ to a common fixed location.
71+
for multi-user you would need to change addins to a common fixed location.
6872

6973
6) An entry in advanced SumatraPDF-settings.txt is needed as similar to this
7074

7175
ExternalViewers [
7276
[
73-
CommandLine = c:\windows\system32\cmd.exe /c ""%addins%\ExportPng\ExportPng.cmd" "%1" page=%p"
77+
CommandLine = c:\windows\system32\cmd.exe /c " """C:\Some one\Downloaded\Apps\SumatraPDF\Plus\ExportPng.cmd""" """%1""" page=%p "
7478
Name = Save current page &Graphics to PNG
7579
Filter = *.*
7680
]
7781
]
7882

7983
If you wish to always modify a range of pages and on occasion just single pages then
80-
a) Remove the %p from the command line (remember to keep " at end e.g. %1" page=")
84+
a) Remove the %p from the command line (remember to keep " at end e.g. """%1""" page=")
8185
b) Change in Name = from "current page" to "page range"
8286
alternative is to have 2 different "viewer" blocks but then you would need a second shortcut
8387

@@ -90,8 +94,9 @@ End of readme / notes
9094
: if you have not placed mutool where recommended as is relative to this
9195
: file which was set at start then you may need to adjust both here and later
9296
: TL;DR this test should not be needed but for those users that don't RTFM
93-
if "%addins%"=="" set addins=%~dp0..
94-
if not exist "..\..\Utils\MuPDF\mutool.exe" echo: & echo Either MuTool.exe or this file are not in correct location & goto HELP
97+
98+
if "%addins%"=="" set "addins=%~dp0"
99+
if not exist "%addins%mutool.exe" echo: & echo Either MuTool.exe or this file are not in correct location & goto HELP
95100
if not exist "%~f1" echo: & echo "%~dpn1%~x1" & echo Appears NOT to exist as a valid file & goto HELP
96101

97102
: IF you wish to add or restrict input to only certain extensions then edit the file
@@ -107,7 +112,7 @@ if %2.==. goto RANGE
107112

108113
:HELP
109114
echo:
110-
echo Example Usage : ExportPng "C:\pdfs\in.pdf" pages=2-5
115+
echo Example Usage : "%~nx0" "C:\pdfs\in.pdf" pages=2-5
111116
echo:
112117
echo You can specify just one single page, e.g. Page=15 or
113118
echo a range like Pages=5-10 etc. or page=1-N (ALL pages)
@@ -117,9 +122,9 @@ echo for other options.
117122
echo:
118123
echo Call page without values to manually enter a page-range
119124
echo:
120-
echo ^> ExportPng "C:\pdfs\in.pdf" page (Note s^&= are optional)
125+
echo ^> "%~nx0" "C:\pdfs\in.pdf" page (Note s^&= are optional)
121126
echo:
122-
pause & exit
127+
pause & exit \b
123128

124129
:PASS
125130
: There should be no error if the file is held open in SumatraPDF
@@ -148,7 +153,7 @@ echo:
148153
: IMPORTANT default for png images output is highly recommended as -r 96
149154
: BUT, if intended use is for OCR later, then it should be higher e.g. -r 300
150155
:
151-
"..\..\Utils\MuPDF\mutool.exe" draw -r 96 -o "%~dpn1-Page-%%4d.png" "%~f1" "%pages%"
156+
"%addins%mutool.exe" draw -r 96 -o "%~dpn1-Page-%%4d.png" "%~f1" "%pages%"
152157
echo:
153158
: pause
154159
: Optional, you can comment, change or delete timeout if not wanted (currently 5 seconds)

0 commit comments

Comments
 (0)