Ghostscript PDF combine file limit? - pdf

If I have a bat file on windows to drive ghostscript PDF combine like:
C:\Program Files (x86)\GPLGS>gswin32c.exe -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=c:\copyfolder\combine.pdf -dBATCH c:\copyfolder\AgentLetter.pdf c:\copyfolder\iso.pdf c:\copyfolder\disclosure.pdf
In the above example I am combining 3 files. Is there a limit to the number of files I can list to be combined ?
Thanks
I just tried a test combining 3 files and it works. Wondering if there is a limit on number of files I can combine at once ?

The use of CLI programming is sometimes discouraged but is the basis of all top level machine OS code just same as running Pythonic Wheels or powershell instructions or scraping web pages, the aim is to do as much programatically in one line of instruction so as to be modular in computer applications.
[Edit]
As mentioned by KenS in comment use a #filelist (TL;DR its at the end) or for single command line usage you can shorten the total string length.
You will find on Windows it is the string length of a CMD console is the limit so abbreviate the programming commands as much as possible
traditionally you could rename the exe to same as Unix and use newer -o for output and keep names short without paths would go further to help
>gs -q -sDEVICE=pdfwrite -ocombine.pdf 01.pdf 02.pdf 03.pdf
example
>gs.exe -q -sDEVICE=pdfwrite -ocombine.pdf 01.pdf 02.pdf 03.pdf
C:\Apps\PDF\GS\gs1000w64\bin>dir 0*.pdf
Directory of C:\Apps\PDF\GS\gs1000w64\bin
01/02/2023 19:23 2,821,328 01.pdf
01/02/2023 19:23 2,821,328 02.pdf
01/02/2023 19:23 2,821,328 03.pdf
3 File(s) 8,463,984 bytes
C:\Apps\PDF\GS\gs1000w64\bin>dir co*.pdf
Directory of C:\Apps\PDF\GS\gs1000w64\bin
17/02/2023 16:32 2,831,780 combine.pdf
amazing the 27 page file is not much bigger than 1 of 9 pages, but I cheated they are the same file, thus same fonts.
if really string is an issue GS does not need .pdf simply .p will do but for windows sake use at least one letter as extension.
C:\bin>gs -q -sDEVICE=pdfwrite -oALL.pdf 01.p 02.p 03.p
using
"path\gswin##c.exe" "path to\#filesmerge.txt"
commands as program instructions are strung out vertically but any filename with spaces needs "quot ing"
filesmerge.txt
-sDEVICE=pdfwrite
-oALL.pdf
"C:\Any where with\a long name.pdf"
C:\Data\somewhere\blank.pdf
"C:\Data\some where\c me c you.pdf"
"C:\Data\some where else\done.pdf"
You can mix and match so if just the filesnames to merge are listed you can run
"C:\Apps\PDF\GS\gs1000w64\bin\gswin64c.exe" -sDEVICE=pdfwrite -o"c:\data\all my.pdf" "#C:\source folder\filenames.txt"

Related

Print multiple PDF page ranges

I have a PDF with 200+ editable pages and need to hardcode print to PDF them into smaller PDF files (ie page 1-2, 3-8, 9, 10-11, 12-14, etc..).
Is there a way to automate this since I do this exercise each month? Right now I have to manually print each sub section one at a time.
You can use Ghostscript to copy a range of pages from a PDF file to another.
For example, to write pages 3-8 from input.pdf to output.pdf you could run the following from the command prompt, using the command line options to specify the first and last page to process.
gswin64c.exe -sDEVICE=pdfwrite -dFirstPage=3 -dLastPage=8 -o output.pdf input.pdf

Use Ghostscript to convert each page of a PDF to images and the output is still PDF

I know that Adobe Acrobat Reader DC can select the Microsoft Print to PDF printer to output to a PDF file with Print As Image checked in the Advanced Print Setup dialog. However, I want to use a command to do this. I tried the following command, as a result it failed to convert each page to images (Note the output file is still PDF).
gs -o 0.999.watermask.compact.screen.pdf -sDEVICE=pdfwrite -dDetectDuplicateImages=true -dPDFSETTINGS=/screen 0.999.watermask.pdf
References
7.4 PDF file output
iText 7 iText 7 for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring.
itext-rups-7.1.14.jar iText RUPS iText® 7.1.14 ©2000-2020 iText Group NV (AGPL-version)
Your -switches include -dDetectDuplicateImages=true which under the circumstances should be superfluous and the device selection can be from one of four as pointed out by KenS.
gs -o 0.999.watermask.compact.screen.pdf -sDEVICE=pdfimage32 -dPDFSETTINGS=/screen 0.999.watermask.pdf
If you want to emulate MS Print As Image PDF on Windows you would find the result in some ways inferior (and often many times bigger). But for comparison it would be,
NOTE:- "%%printer%%... is for a batch file for a command line use "%printer%...
gswin64c.exe -sDEVICE=mswinpr2 -dNoCancel -o "%%printer%%Microsoft Print to PDF" -dPDFSETTINGS=/screen -f "0.999.watermask.pdf"

Ghostscript to convert PDF 2 Back to PDF version 1.7

I need to build a PDF serverside by reading in a number of PDFs and inserting each page into a new multipage PDF. The problem is that the PDFs are provided in version 2.0 format, but my application can only read version 1.7. I would like to convert the version 2 files back into a version 1.7 file so that my application can read it.
I am using ghostscript version 9.27, and have tried several commands, but each time I end up with an empty PDF. Example:
/usr/local/bin/gs \
-q -dNOPROMPT \
-dBATCH \
-dDEVICEWIDTH=595 \
-dDEVICEHEIGHT=842 \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.7 \
-sFileName=pdf-version-2.pdf \
-sOutputFile=fileout.pdf
There is no error, just an empty PDF. The "file" command does give the expected output "PDF document, version 1.7" but that's not much good when the file is blank. Any help greatly appreciated!
OK so I think the problem is your command line (as pointed out to me by one of my colleagues). You've specified -sFileName=pdf-version-2.pdf, which looks like you're trying to specify the input file.
There is no Ghostscript switch -sFileName, you specify the input filename(s) by simply putting the name on the command line. So you really want:
/usr/local/bin/gs \
-q -dNOPROMPT \
-dBATCH \
-dDEVICEWIDTH=595 \
-dDEVICEHEIGHT=842 \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.7 \
-sOutputFile=fileout.pdf \
pdf-version-2.pdf
For reasons that were good 30 years ago, the Ghostscript command line switches are copied into the PostScript environment, where they can be accessed by PostScript programs. So while its true (and possibly the szource of your confusion) that some of the utility programs shipped with Ghostscript do use -sFileName, Ghostscript itself doesn't, it just defines a PostScript variable using that name, so that programs can read it.
Because you've specified BATCH and NOPROMPT, but haven't specified an input file, the interpreter starts up, erases the current page to white, then exits. Closing the pdfwrite device causes it to write out the current content of the page, which is, well, white, resulting in your empty PDF file.
The slightly modified command line above worked well for me, but as I noted in my comments specifying DEVICEWIDTHPOINTS and DEVICEHEIGHTPOINTS won't actually do anything.

Split multi-page PDF into JPG (or PNG...) files using ImageMagick

I'm facing a little problem with Image Magick, which I found a marvellous tool so far, but here it doesn't achieve what I expect (N.B: I work in Windows 7)
I read that, to split a 3 pages (for example) pdf file, you just have to do:
img2img My3pageFile.pdf SplittedImage.jpg
and then, ImageMAgick would automatically create SplittedImage-1.jpg, SplittedImage-2.jpg and SplittedImage-3.jpg.
Well instead of this, I obtain an error message like this: (let me hope you'll believe me if I say that I have no doubt here under that the file "benef.pdf" does exist on D:).
D:\>img2img benef.pdf benef.jpg
img2img: `%s': %s "gswin32c.exe" -q -dQUIET -dSAFER -dPARANOIDSAFE -dBATCH
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=0
"-sDEVICE=pnmraw" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-g595x842" "-r72x72"
"-sOutputFile=C:/Users/ADM-A2~1/AppData/Local/Temp/magick-o3McMMZQ" "-fC:/Users
/ADM-A2~1/AppData/Local/Temp/magick-EOLT_ZO2" "-fC:/Users/ADM-A2~1/AppData/Local
/Temp/magick-mUWMMcc0".
img2img: Postscript delegate failed `benef.pdf'.
img2img: missing an image filename `benef.jpg'.
The answer is simply to install and download GhostScript at the following address, after what the instruction I gave at the beginning works perfectly well.
So here's the link:
http://downloads.ghostscript.com/public/

Error in Converting PDF to PostScript with GhostScript, Access is denied Unable to open command line file _.at

I installed ghostscript and updated the appropriate path variables ... however, I'm getting an error when I try to execute this command:
C:\PROGRA~1\gs\gs8.64\lib>pdf2ps mydocument.pdf mydocument.ps
Access is denied.
Unable to open command line file _.at
Is this the right command? Did I miss some configuration or path setting? Otherwise, is there a sane method of doing this conversion?
Access is denied suggest something to do with access to paths etc. I'd suggest rechecking the folder permission (although I'm sure you've done that). Also, you might want to try running the gswin32c.exe instead of the pdf2ps to see if you still get the error, you might get something a little more specific.
gswin32c.exe ^
-dNOPAUSE ^
-dBATCH ^
-sDEVICE=pswrite ^
-sOutputFile=mydocument.ps ^
mydocument.pdf
Using pdf2ps runs a batch file, really named pdf2ps.bat or pdf2ps.cmd. You can easily look up and understand its "source code". If you do, you'll see it tries to write some of its commandline options into a temporary file named _.at, in order to overcome the 128 character limit for DOS/cmd commandline length that exist on some Win/DOS platforms.
Since you are invoking pdf2ps from the %programs% directory where Ghostscript is installed, you don't seem to be using an account that is permitted to write stuff in there. :-)
With Ghostscript version gs9.10 the method pswrite didn't worked for me instead I tried using ps2write instead, and it worked for me, so the command worked for me is as below:
gswin32c.exe ^
-dNOPAUSE ^
-dBATCH ^
-sDEVICE=ps2write ^
-sOutputFile=mydocument.ps ^
mydocument.pdf
and if this thing doesn't even works, then one can do this :
try getting help by typing gswin32c.exe -h and then it will list all the available devices as shown below:
Default output device: display
Available devices:
bbox bit bitcmyk bitrgb bj10e bj200 bjc600 bjc800 bmp16 bmp16m bmp256
bmp32b bmpgray bmpmono bmpsep1 bmpsep8 cdeskjet cdj550 cdjcolor cdjmono
cp50 declj250 deskjet devicen display djet500 djet500c eps9high eps9mid
epson epsonc epswrite ibmpro ijs inkcov jetp3852 jpeg jpegcmyk jpeggray
laserjet lbp8 lj250 ljet2p ljet3 ljet3d ljet4 ljet4d ljetplus m8510
mswindll mswinpr2 necp6 nullpage pamcmyk32 pamcmyk4 pbm pbmraw pcx16
pcx24b pcx256 pcxcmyk pcxgray pcxmono pdfwrite pgm pgmraw pgnm pgnmraw pj
pjxl pjxl300 pkmraw plan planc plang plank planm plib plibc plibg plibk
plibm png16 png16m png256 pngalpha pnggray pngmono pngmonod pnm pnmcmyk
pnmraw ppm ppmraw **ps2write** psdcmyk psdrgb pxlcolor pxlmono r4081 spotcmyk
st800 stcolor svg t4693d2 t4693d4 t4693d8 tek4696 tiff12nc tiff24nc
tiff32nc tiff48nc tiff64nc tiffcrle tiffg3 tiffg32d tiffg4 tiffgray
tifflzw tiffpack tiffscaled tiffscaled24 tiffscaled32 tiffscaled4
tiffscaled8 tiffsep tiffsep1 txtwrite uniprint xpswrite
Search path:
C:\Program Files (x86)\gs\gs9.10\bin ;
C:\Program Files (x86)\gs\gs9.10\lib ;
C:\Program Files (x86)\gs\gs9.10\fonts ; %rom%Resource/Init/ ;
%rom%lib/ ; c:/gs/gs9.10/Resource/Init ; c:/gs/gs9.10/lib ;
c:/gs/gs9.10/Resource/Font ; c:/gs/fonts
Initialization files are compiled into the executable.
As one can see only for the convenience only I have placed star(*) around the ps2write
use gimp open PDF file.
file -> export -> postscript.
If you want to use the gs executable you have to change the permissions.In the command prompt go to the location where gs executable is located and then use chmod 755 gs.
What you are doing is you are not writing command line at right place first you have to find out the instillation exe of ghostscript which is by default located at
c:\Program Files(x86)\gs\gs9.20(your ghostscript
version)\bin\gswin32c.exe
there are two exe
1- gswin32.exe
2- gswin32c.exe
you have to use the second one because it execuit commmands at cmd not in gs cmd
ok now what you have to do is write command like
...bin\gswin32c.exe -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=mydocument.ps mydocument.pdf
note please check the file path correctly and one more thing
file path like
"D:\htmltopdf\document.ps"
should be write as
"D:/htmltopdf/document.ps"
yes exactly replace backward slash with foreword slash only in file path
and the command line is case sensitive also so be carefull with cases