Merged PDFs Blank - pdf

Trying to merge all pdfs in a directory using GhostScript 9.06 64bit in a .bat file
The following, makes merged.pdf, but it is 1 page and blank
call gswin64c -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf *.pdf
If I actually specify which PDFs to merge it works fine. What gives?
call gswin64c -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf 1.pdf 2.pdf 3.pdf

You can't specify wildcards on the Ghostscript command line, simple as that.
Since GS didn't find a file called '*.pdf' it didn't execute any marking operations, in this case you get a blank file.

Ghostscript cannot do wildcard expansions by itself.
If you call gs ... *.pdf from inside a shell which can do wildcard expansion, it will work nevertheless.
There is a difference with the site you linked to and the code you used above:
Your code is DOS batch and uses call gswin64c .... But as said, Ghostscript cannnot expand wildcards itself.
The code in the linked web page is Unix shell, which does the wildcard expansion before Ghostscript gets to see its own commandline. When Ghostscript gets to see it, the wildcard expansion has happened already.
You have to find a solution for your batch file where you first store your (expanded) *.pdf file names in a variable %mypdfs% and then do call gswin64c ... %mypdfs%.

you can't specify the wildcard from the command line, but you can make gswin32c run a command file.
as the 'command file' just requires switches to be separated by any amount of white space (space, tab, line break), and there is no limit on the size of the file, we can make a file that does what you need
echo -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf > files.gsx
dir *.pdf /b >> files.gsx
once this file files.gsx has been created, then you can make your file using
gswin32c #files.gsx
and all the files will be merged

I did the following to solve this:
1.) dir /B *.pdf > do.bat
2.) opened do.bat with notepad to replace \r\n with spaces
3.) inserted: c:\Programs\gs\gs9.07\bin\gswin64 -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=merged.pdf at the beginning
and then executed do.bat
VoilĂ 

Related

When adding a -c option to a Ghostscript command the printer popup appears even though it has been suppressed

Consider the following ghostscript command invoked form the command line on windows.
"C:\Program Files\gs\gs9.23\bin\gswin64c.exe" -sDEVICE=mswinpr2 -dORIENT1=false -dNOPROMPT -dNOPAUSE -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=398 -dDEVICEHEIGHTPOINTS=157 -dNOPAGEPROMPT -dQUIET -dNumCopies=1 -sOutputFile="\\spool\BWLAB05" "c:\print\download\133679.pdf"
This command works great. The print comes out and there is no pop up box. Now I add the following.
-c "<</Orientation 2>>setpagedevice"
to the above command to make
"C:\Program Files\gs\gs9.23\bin\gswin64c.exe" -sDEVICE=mswinpr2 -c "<</Orientation 2>>setpagedevice" -c "quit" -dORIENT1=false -dNOPROMPT -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=398 -dDEVICEHEIGHTPOINTS=157 -dNumCopies=1 -sOutputFile="\\spool\BWLAB05" "c:\print\download\133679.pdf"
the print window appears. I have changed no other part of the command. What is causing this to happen? How can I stop the print window appearing?
The order of operands to Ghostscript is important. Especially when using the -c switch, which introduces PostScript to be executed.
Effectively you are running two commands here:
-sDEVICE=mswinpr2 -c "<</Orientation 2>>setpagedevice" -c "quit"
-dORIENT1=false -dNOPROMPT -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=398 -dDEVICEHEIGHTPOINTS=157 -dNumCopies=1 -sOutputFile="\\spool\BWLAB05" "c:\print\download\133679.pdf"
As soon as it hits the -c Ghostscript stops processing the command line, and runs the PostScript. At this point we have not yet encountered the -dNOPROMPT, and as you haven't (yet) set the printer, Ghostscript doesn't know what printer to use so , unsurprisingly, the printer popup appears.
Ghostscript carries on processing the remainder of the command line as PostScript until it reaches a -f, or in fact any switch beginning -. You haven't put a -f in there, but I would very strongly recommend that you do. You also don't need to put a secopnd -c, once you've started processing the command line as PostScript it continues, until you stop it. I also suspect that you really don't want the quit in there. That terminates the interpreter, which means the changes you've introduced via setpagedevice will be discarded, because the interpreter returns to the default state.
After processing the content of the -c, Ghostscript carries on and processes the remainder of the command line. This time there's a -dNOPROMPT so you don't get prompted.
I would expect that this:
"C:\Program Files\gs\gs9.23\bin\gswin64c.exe" -sDEVICE=mswinpr2 -dORIENT1=false -dNOPROMPT -dPrinted -dBATCH -dNOSAFER -q -dFIXEDMEDIA -dDEVICEWIDTHPOINTS=398 -dDEVICEHEIGHTPOINTS=157 -dNumCopies=1 -sOutputFile="\\spool\BWLAB05" -c "<</Orientation 2>>setpagedevice" -f "c:\print\download\133679.pdf"
would work much better. Note your initial command line has a duplicate NOPAUSE and specifies both NOPROMPT and NOPAGEPROMPT (you don't need NOPAGEPROMPT if you set NOPROMPT, and you don't need either if you set NOPAUSE).
Finally I would urge you not to use -dNOSAFER, while it currently has no effect (because that's the default setting) we will soon be making SAFER the default and setting -dNOSAFER will substantially reduce your security when running files.
You should really use -dSAFER right now. There are a number of CVEs against this, and proof of concepts circulating right now which can have undesirable effects on your computer (running arbitrary executables, opening, writing, deleting files etc) if you don't use -dSAFER. If you don't know why you want -dNOSAFER, then use -dSAFER instead.
Oh, you should also upgrade to the current version, 9.27, the version you are using is a year old.

How to merge all PDF's in a directory with ghostscript

How can I read the files contained in the directory d:\ with batchscript not one by one files like that. I have tried the following:
#echo off
"C:\Program Files\gs\gs9.25\bin\gswin32c.exe" -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/printer -dColorImageResolution=90 -dAutoRotatePages=/None -dBATCH -dNOPAUSE -sOutputFile=d:\d\koran.pdf *d:\a\01.pdf d:\a\02.pdf d:\a\03.pdf d:\a\04.pdf d:\a\05.pdf d:\a\06.pdf d:\a\07.pdf d:\a\08.pdf d:\a\09.pdf d:\a\10.pdf d:\a\11.pdf d:\a\12.pdf d:\a\13.pdf d:\a\14.pdf d:\a\15.pdf d:\a\16.pdf d:\a\17.pdf d:\a\18.pdf d:\a\19.pdf d:\a\20.pdf d:\a\21.pdf d:\a\22.pdf d:\a\23.pdf d:\a\24.pdf*
exit
Ghostscript doesn't 'merge' PDF files. It creates new PDF files by interpreting the contents of its input, this is not the same thing. You should read the documentation here
You haven't said what the problem is with the command you have tried, its going to be hard to help you if you don't do that.
The most likely problem is that you have put * characters at the start and end of the input filenames. Ghostscript itself doesn't match wildcards, it expects you to tell it each file you want to process individually. So in order to process a directory of files is to first get a list of all the files, and then tell Ghostscritp to use each of those files in turn.
You can use the Ghostscript #filename syntax (documented here)to tell Ghostscript to use the contents of a file as if it were the command line.
So all you need to do is come up with a shell script which will write the filenames from a folder into a file. That's not a Ghostscript question, and depends totally on the operating system you are using.
For Windows something like:
dir /B *.pdf >> files.txt
gswin32c -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=\temp\out.pdf #files.txt
del files.txt
might be sufficient for your needs.
I could not make it work using "files.txt" but I am using this and everything works just fine.
gswin64c -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile="out.pdf" (Get-ChildItem -Path .\*.pdf)
You can execute like below:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf *.pdf
Reference:
https://gist.github.com/moaazsidat/b94185e9cfdba9e3cfb5bc90407e6397

Redirect ghostscript Output

I use the following command (in a Windows cmd) to decrypt pdf files stored under the directory C:\Users\David\Desktop\BS1999\ and write the output into the same folder using ghostscript:
FOR %x IN (C:\Users\David\Desktop\BS1999\*.pdf) DO gswin64c -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%x_converted.pdf -c .setpdfwrite -f %x
So in short I have
For %x IN (*.pdf) DO my-ghostscript-function %x
What modifications do I have to make to redirect the output ("name-of-file"_converted.pdf) to another file path (for example C:\Users\David\Desktop\test)?
Thanks in advance!
Best, David

Ghostscript loses font while extracting the page from PDF

I split PDF into pages with help of usable command line:
for G in $(seq 1 $(pdfinfo 47.pdf | sed -n 's/Pages:[^0-9]*\([0-9]*\).*/\1/p')) ; do
gs \
-dSAFER \
-sDEVICE=pdfwrite \
-dBATCH \
-dNOPAUSE \
-dFirstPage=$G \
-dLastPage=$G \
-o $G.pdf \
47.pdf ;
done
But some pages appears without text (Graphics are still present)
So, I have tried to extract embedded font from PDF:
gs -q -dNODISPLAY extractFonts.ps -c "(47.pdf) extractFonts quit"
These fonts I have installed in system Fonts folder.
After that, I have repeat splitting and no changes were happened.
How-to be sure that pages will be extracting correctly, I have no idea now.
Ghostscript and pdfwrite are not actually intended for the purpose of splitting PDF files up, there are other tools which will probably work better, why not try pdftk ?
If you really want to use Ghostscript then I would advise you to get hold of the latest bleeding-edge code from the Git repository, in that code the pdfwrite device will accept an output file name containing a '%d' and will write one file per page.
Beyond that, it seems most likely to me that you are simply experiencing a bug, rather than 'losing the font', if the font was missing the text would still be ther but in a differnt font. Which version of GS are you using ?

GhostScript removes images from some processed PDF files

I'm using this command with gs 9.01:
gs -q -dAutoRotatePages=/None -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -sOutputFile=out.pdf in.pdf in.pdfmarks
and on some processed files (out.pdf) I noticed that images are missing, although present in input file (in.pdf).
Why is this and how can I assure that images are retained after processing with gs?
1) Update to the current version, 9.04.
2) If you still experience problems, report a bug at http://bugs.ghostscript.com. You will need to attach a sample PDF file