Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ghostscript Batch file - execute and quit

Tags:

c#

ghostscript

I am trying to execute GhostScript in a batch file.

batch.bat

gswin32.exe ^
  -dNOPAUSE ^
  -sDEVICE=pdfwrite ^
  -sOUTPUTFILE=output1.txt ^
  -dBatch ^
   "file1.txt" "file2.txt"

gswin32.exe ^
  -dNOPAUSE ^
  -sDEVICE=pdfwrite ^
  -sOUTPUTFILE=output2.txt ^
  -dBatch ^
   "file1.txt" "file2.txt"

Problem is when first line is executed, it opens GhostScript window. i have to manually type quit or close the window to return the control to parent to execute the next command in batch. How can we modify batch file so it executes ghost script? Originally I am calling this file from C# program using Process.Start(ProcessInfo). Suggestions are welcome

like image 862
Nakul Manchanda Avatar asked Dec 12 '22 23:12

Nakul Manchanda


2 Answers

  1. To avoid Ghostscript opening a window, don't use gswin32.exe.
    Use gswin32c.exe instead. (The c in the name is to indicate it's for console only...)

  2. Also, be aware that Text files cannot serve as input to Ghostscript.
    Ghostscript can only process PostScript, Encapsulated PostScript or PDF files.

  3. You can name your output file as you please.
    But your -sDEVICE=pdfwrite will produce PDF output, so you are well advised to use a .pdf suffix for the output file if you want to avoid confusion later.

  4. The spelling for -dBATCH is case sensitive.
    -dBatch will not work.
    (-dBATCH causes Ghostscript to return after the last page of the job being completed -- otherwise it would switch to interactive mode and show its GS> prompt... It's not for processing 'batches of input files', as you appear to assume.)

  5. Your 2 commands in the batch file are using both the same input parameters.
    That means the 2 output files will be the same, just have different filenames.

I recommend the following commandline scheme to use:

gswin32.exe ^
  -dNOPAUSE ^
  -dBATCH ^
  -sDEVICE=pdfwrite ^
  -sOUTPUTFILE=output2.pdf ^
   "file1.ps" "file2.pdf" "file3.eps"
like image 183
Kurt Pfeifle Avatar answered Dec 27 '22 23:12

Kurt Pfeifle


Use gswin32c instead. This is a console application and won't create a window you need to manually close.

like image 44
Joey Avatar answered Dec 28 '22 01:12

Joey