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
Related
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
inputPdf
Use gswin32c.exe -o nul -sDEVICE=bbox bbox.pdf,I'v hnow the BoundingBox of this pdf is
%%BoundingBox: 6292 6865 8108 7535
%%HiResBoundingBox: 6292.907808 6865.505790 8107.091753 7534.493770,
I want to get a pdf with the content in the BoundingBox.
I am using the following command to crop a PDF:
gswin32c -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1 -o croped.pdf -dDEVICEWIDTHPOINTS=1815 -dDEVICEHEIGHTPOINTS=670 -dFIXEDMEDIA -c "6292 6865 translate 6292 6865 8107 7534 rectclip" -f bbox.pdf
or
gswin32c -dQUIET -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1 -o croped.pdf -dDEVICEWIDTHPOINTS=1815 -dDEVICEHEIGHTPOINTS=670 -dFIXEDMEDIA -c "<</PageOffset [6292 6865]>> setpagedevice" -f bbox.pdf
i'v a blank pdf file.
this command
gswin32c.exe -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=croped.pdf -c "[/CropBox [6292.907808 6865.505790 8107.091753 7534.493770] /PAGES pdfmark" -f bbox.pdf
i'v a original file.
How can i crop this pdf correctly.
Thanks very much!
The BoundingBox looks suspicious to me.
In any event you cannot trivially do what you are trying to do with Ghostscript, because the PDF interpreter uses the information in the PDF file to set the media size.
The first two command lines 'might' work, but you've translated the CTM in the wrong direction. You've moved the origin (0,0) from the bottom left, up and right. That's moved the content of the page further off the media, which is why you get a blank page. You could try using the same values, but negated, so that the origin moves down and left. From the BoundingBox you quoted, that's the correct direction.
gswin32c -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1 -o croped.pdf -dDEVICEWIDTHPOINTS=1816 -dDEVICEHEIGHTPOINTS=670 -dFIXEDMEDIA -c "-6292 -6865 translate" -f bbox.pdf
You don't need the rectclip, because the content is already clipped to the page.
The third command line would also work, except that you've set the CropBox before processing the PDF file, so the PDF interpreter reads the CropBox from the PDF file and overwrites the one you set. Try setting it after the input file.
gswin32c.exe -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=croped.pdf bbox.pdf -c "[/CropBox [6292.907808 6865.505790 8107.091753 7534.493770] /PAGES pdfmark" -f
[EDIT]
OK so the reason the first command lines doesn't work is (as I suspected) because the PDF interpreter resets the graphics state before running the PDF, so it simply throws away the 'translate'.
The second command line works perfectly well for me if you negate the operands in the array for PageOffset:
gswin32c -sDEVICE=pdfwrite -sOutputFile=\temp\out.pdf -dDEVICEWIDTHPOINTS=1815 -dDEVICEHEIGHTPOINTS=670 -dFIXEDMEDIA -c "<</PageOffset [-6292 -6865]>>setpagedevice" -f D:\Users\ken\Downloads\bbox.pdf
The third command line doesn't work because it sets the CropBox for all Pages, which is a default and can be overridden by setting a CropBox on each page. Your original PDF file contains a CropBox (identical to the MediaBox) which is preserved by the PDF interpreter, so the PAGES CropBox is overridden by the CropBox specific to the page.
But the command line above worked fine for me.
I want to write a script for sending a fax with Asterisk,
but many faxes have an error when sending.
I use Spandsp for Asterisk and I use gs for converting PDF to TIFF.
I think it may be a problem of converting PDF to TIFF.
I very try to convert's command for this, such as:
gs -q -sDEVICE=tiffg3 -dNOPAUSE -dBATCH -sOutputFile=Sample.pdf.tif -f Sample.pdf
/usr/bin/acroread -toPostScript -size a4 <filename>
/usr/bin/gs -q -sDEVICE=tiffg32d -sPAPERSIZE=a4 -r204x196 -sOutputFile=<outputfile> -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dBATCH -f <inputfile.ps>
and I tested anyways into link for converting, but it doesn't work fine, and most of sending fax have issue.
Also, I have another problem in answer detection beep in my Asterisk when the trunk with Newrock gateway(analog), this is cause some fax doesn't send correctly!
Check hylafax code. It have working script
I use code like this:
gs -sDEVICE=tiffg3 -sPAPERSIZE=$pagesize -r204x196 -dNOPAUSE -dNOPAGEPROMPT -dBATCH -sOutputFile=dest.tif \
-c save pop -f input.pdf -c quit
But actualy it doesn't metter. More important how setup your routes/codecs.
Other problem is for sure offtopic here. You have read more books or hire expert.
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Ă
I'm using Ghostscript library API (wrapping from C#) to print PDF documents from my application.
With the '-dFirstPage' and '-dLastPage' parameters I'm able to select an range of pages to be printed, but how about the total number of a PDF's pages?
It is not very nice to allow a user to select a page interval from 2 to 10 when, let me say, the PDF document has only 4 pages.
Consider that I'm using Ghostscript library through the gsapi_init_with_args API library call.
Ghostscript can count and display the number of pages of a PDF on stdout. The commandline is
gswin32c ^
-q ^
-dNODISPLAY ^
-c "(input.pdf) (r) file runpdfbegin pdfpagecount = quit"
Here all the -c "..." stuff is a PostScript commandline snippet (using a few GS internal command extensions). And input.pdf is the PDF filename (could also be a full path like (c:/path/to/my.pdf)).
However, a better and faster tool for this kind of job would be to use pdfinfo (part of the XPDF-utilities, also available on Windows).
Update:
#ebyrob wants to know if one can modify my example command line so that it also displays the PDF in a single operation. Try this:
gswin32c ^
-q ^
-c "(input.pdf) (r) file runpdfbegin pdfpagecount =" ^
-f input.pdf
Well, it's not a single operation -- it's just two different operations in a single commandline.
For people having issues in ghostscript >9.50 add --permit-file-read=input.pdf
I tried to make this script:
gswin32c ^
-q ^
-c "(input.pdf) (r) file runpdfbegin pdfpagecount =" ^
-f input.pdf
work in a c# wrapped solution and kept getting error "/undefinedfilename". In this case ensure that your filepath has Slashes "/" as DirectorySeperator and not Backslashes "\". I know Kurt Pfeifle already wrote it, but it happened to me i just overlooked it.
In Windows systems:
"path to gs exec" -q -dNODISPLAY -dNOSAFER --permit-file-read="path to
your file" -c "(""path to your file"") (r) file runpdfbegin
pdfpagecount = quit"
Remarks:
Just change where is 'path to...' with your path, leave the rest as is.
On the -c path you must use double slashes or unix like ones. Ex: C:\\youfile.pdf (good), C:/youfile.pdf (good), C:\yourfile.pdf (bad).
Example:
path: C:\Temp\Some Folder\myFile.pdf
gs path: C:\Temp\Some Folder\gs\bin\gswin64c.exe
path -c 1: C:\\Temp\\Some Folder\\myFile.pdf
path -c 2: C:/Temp/Some Folder/myFile.pdf
Commands:
"C:\Temp\Some Folder\gs\bin\gswin64c.exe" -q -dNODISPLAY -dNOSAFER --permit-file-read="C:\Temp\Some Folder\myFile.pdf" -c "(""C:\\Temp\\Some Folder\\myFile.pdf"") (r) file runpdfbegin pdfpagecount = quit"
"C:\Temp\Some Folder\gs\bin\gswin64c.exe" -q -dNODISPLAY -dNOSAFER --permit-file-read="C:\Temp\Some Folder\myFile.pdf" -c "(""C:/Temp/Some Folder/myFile.pdf"") (r) file runpdfbegin pdfpagecount = quit"
To sum up some of the above separate comments for windows users to avoid needing to alter between / and \\ , to show the total number of pages can be set as a shortcut for drag and drop or "sendTo", by first switching to a working directory.
#echo off & cd /d "%~dp1" & "C:\path to gs\bin\gs.exe" -q --permit-file-read="%~nx1" -c "(%~nx1) (r) file runpdfbegin pdfpagecount = quit" & pause
where gs.exe is one of the windows c(onsole) variants gswin32c.exe or gswin64c.exe
The cd /c "%~dp1" will switch console to quoted file drive path
The full quoted path to "GSwin..c.exe" calls it safely and remotely
-q will suppress (not show) the start message
since version 9.5+ the --permit-file-read="file name" is advised / required
-c "(%~nx1) does not need the quotes for name.xtension
if running a cmd as a shortcut, pause is required to see the result
beware only use on files you trust as your overriding GS -dSAFER restrictions.