.bashrc function with arguments - bash

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" "$#"
}

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.

How to properly use variable substitutions in windows cmd?

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.

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

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

How to transform an high def PDF to low def using command line tools?

I've a unix server (mac osx in fact) which transform actually PS files to PDF files. It does this through ps2pdf, with those parameters:
ps2pdf14 \
-dPDFSETTINGS=/prepress \
-dEPSCrop \
-dColorImageResolution=72 \
-dColorConversionStrategy=/LeaveColorUnchanged \
INPUT_FILE \
OUTPUT_FILE
But now I've to adapt this script to have a PDF file as input instead as PS.
So I guess that ps2pdf will not work anymore, and I need something which can reduce the quality of the pdf.
Do you know a tool like this?
The ps2pdf14 script just runs the ps2pdfwr script with -dCompatibilityLevel=1.4, which in turn uses gs with various parameters. You can examine that script to see the options.
You could run gs directly, putting in the various options added by the scripts and your own -d options (which are passed directly to gs). I.e. try:
gs \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
-dEPSCrop \
-dColorImageResolution=72 \
-dColorConversionStrategy=/LeaveColorUnchanged \
-q \
-dNOPAUSE \
-dBATCH \
-sOutputFile=OUTPUT_FILE \
INPUT_FILE
Your command should works with PDFs: Ghostscript (backend for ps2pdf) accept PDF as input file. I just tested ps2pdf from Ghostscript 9.04 and it works

Resources