How to properly use variable substitutions in windows cmd? - windows

I'm using Ghost Script to operate PDF. I'm using numered variables as arguments. My input file name is "x.pdf". I want the output file to be "x-processed.pdf". Curently I have the above mentrioned code:
gswin64c.exe -q -dNOPAUSE -dBATCH -dSAFER -dPDFA=2 -dPDFACompatibilityPolicy=1 -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile=%1%-processed.pdf -c .setpdfwrite -f %1
So the output file name is currently inputfilename.pdf-processed.pdf.
I want to remove that extension. But I can't figure out how.
I've tried to use variable substitutions form here: http://cplusplus.bordoon.com/cmd_exe_variables.html
That is to say, I've changed -sOutputFile=%1-processed.pdf to -sOutputFile=%~pn1-processed.pdf
That did not work.
So I've tried to look how to use these substitutions at first place. I've tried to copy selected file name to the clipboard by adding this command via regedit:
cmd.exe echo "%~n1"|clip
That does not work.
But if I put simply:
cmd.exe /c echo "%1"|clip
That works properly and copy file path with extention to the clipboard.
I'm really stuck as I don't understand what I'm doing wrong here. Please help me.
No error messages. No messages at all. Nothing happens.

In fact there is only few variables available when wring a shell command from context menu.
More info is in https://superuser.com/a/473602.
So the only way is to create a batch file with the above mentioned code:
C:\Program Files (x86)\gs\gs9.27\bin\gswin32c.exe" -q -dNOPAUSE -dBATCH -dSAFER -dPDFA=2 -dPDFACompatibilityPolicy=1 -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile="%~dpn1-processed.pdf" -c .setpdfwrite -f "%~1"
And add the following command REG_SZ line instead of putting the command directly:
"Path_to_bat_file/file_name.bat" "%1"
And it should work.

Related

A ghostscript scipt no longer working: pdfwite needs an output file

I have been using this file for quite some time, today it stopped working:
C:\"Program Files"\gs\gs9.53.2\bin\gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dPrinted=false -dPassThroughJPEGImages=false -dDownsampleColorImages=true -dColorImageResolution=400 -dDownsampleGrayImages=true -dGrayImageResolution=400 -dDownsampleMonoImages=true -dMonoImageResolution=400 -dNOPAUSE -dBATCH -sOutputFile=FalterinLenzkirchV2_Compressed.pdf FalterInLenzkirchV2.pdf
I am very surprised; the error message is
'pdfwrite' requires an output file but no file was specified
The output file is there
Since this a batch sript not being changed, I wonder what else has changed which I am not aware of?
"C:/Program Files/gs/gs9.53.2/bin/gswin64c.exe" -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dPrinted=false -dPassThroug hJPEGImages=false -dDownsampleColorImages=true -dColorImageResolution=400 -dDownsampleGrayImages=true -dGrayImageResolution=400 -dDownsampl
eMonoImages=true -dMonoImageResolution=400 -dNOPAUSE -dBATCH -sOutputFile=annots.pdf examples/annots.pdf
WORKSFORME -- Please try with that input file, and if that doesn't work we will need to get your input file.

.bashrc function with arguments

I often use the following command to merge PDFs:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=output_name.pdf /location/of/plots/*.pdf
I tried setting up an equivalent function in my .bashrc file:
function(){
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=$1 $2
}
Now, $ function output.pdf input.pdf only takes one input. How can I change that? Ideally I'd like to be able to pass *.pdf as input.
The following function works fine for me:
pdf_merge() {
output=$1
shift
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="$output" "$#"
}

How should I get UN-rotated JPEG image file from ghostscript?

I am running with crazy issue in ghostscript 9.15.I have two commands for converting PDF to JPEG images,which has difference in height and width only.
Command 1st
-q -dUseCropBox -dBATCH -dDOINTERPOLATE -dMaxBitmap=300000000 -dAlignToPixels=0 -dNOPAUSE -dSAFER -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dPDFFitPage -dPrinted=false -r144 -g200x200 -sDEVICE=jpeg -dJPEGQ=100 -sOutputFile="output.jpg" "input.pdf" -c quit
Command 2nd
-q -dUseCropBox -dBATCH -dDOINTERPOLATE -dMaxBitmap=300000000 -dAlignToPixels=0 -dNOPAUSE -dSAFER -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dPDFFitPage -dPrinted=false -r144 -g200x199 -sDEVICE=jpeg -dJPEGQ=100 -sOutputFile="output.jpg" "input.pdf" -c quit
As you can see, command has difference in -g parameter only.
In first command I have used -g200x200 and in second command -g200x199.
First command gives rotated JPEG while second command gives UN-rotated JPEG file.Is there any utility inside ghostscript which differentiate my command from height and width perspective.
How should I get UN-rotated JPEG image file by using same height and width(-g200x200) while converting PDF to JPEG image file ?
The main answer is 'don't use PDFFitPage'. That feature automatically rotates the output to fit 'best' on the declared media. If that isn't an option then you will have to do some PostScript programming.
You can fnd the code which does the resizing (and rotation) in ghostpdl/gs/Resource/Init/pdf_main.ps, the function /pdf_PDF2PS_matrix

Render a Blank Page with Ghostscript

How can I use ghostscript to create a blank page? I would like to do this when merging multiple PDFs together-- something like:
`gs -dNOPAUSE -o /path/to/output input1.pdf <blank-page-here> input2.pdf
To spell out more explicitely what KenS suggested:
gs \
-o new.pdf \
-sDEVICE=pdfwrite \
-f input1.pdf \
-c showpage \
-f input2.pdf \
-c showpage \
-f input3.pdf \
-c showpage
will insert an additional blank page into the new.pdf after the data of each input{1,2,3}.pdf has been processed.
Just send some PostScript, the 'showpage' operator terminates a page if there's nothing on it, it will be blank.
You can either stick that in a file or use the -c -f switches.
Please note that the pdfwrite device does not merge files. It interprets the content of the input to create marking operations which are fed to the device. The device then takes action on those operations, for rendering devices it renders to a bitmap, in the case of pdfwrite it reassembles them into a PDF file.
So the output from your command line is not a 'merge' of the input files, its a brand new file whose only relationship with the input files is that the marks made on the page are the same.

Ghostscript doesn't generate pdf

I've a problem with that command line :
gswin32c.exe -sDevice=pdfwrite -sOutputFile=out.pdf -dNOPROMPT -dNODISPLAY -dNOPAUSE doc.prn-q -c quit
The program runs without any error.If I enable the display option, i can see the Ghostscript image of my document but my PDF is not generated.
Any Ideas ?
Solved
gswin32c -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=doc.pdf doc.prn -c quit
Your command line includes 'doc.prn-q' possibly you meant 'doc.prn -q'. Hint, if you are trying to debug a problem, don't turn off all the messages which might be trying to give you information! (-q is 'quiet' mode).
I finally found the solution, maybe parameters where not well placed in my command line.
gswin32c -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=doc.pdf doc.prn -c quit
Thank you for all guys !
Have a nice day
Fabien

Resources