How to print PDF to a network printer by default using the GhostScript shell command (gswin32c.exe)

I want to print a PDF file on a Windows network printer through GhostScript.
(I do not want to use Adobe Reader)

I read gswin32c.exe which can do the job.
I experimented with many commands and could not find a way to get gs to print PDF on my (default) network drive.

I do not need a dot network printer - by default, you can use the default. But if there is no such option, I will gladly pass the name of the printer. (I tried with the -SDevice = "\ server_IP \ terminal_name" option, but that didn't work either ...)

Command running under Windows cmd:

gswin32c -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile="\\spool\\\Server_Name\Printer_name" "C:\test.pdf" 

The method created by the database above does not work and excludes. (Error code = 1)

  /// <summary> /// Prints the PDF. /// </summary> /// <param name="ghostScriptPath">The ghost script path. Eg "C:\Program Files\gs\gs8.71\bin\gswin32c.exe"</param> /// <param name="numberOfCopies">The number of copies.</param> /// <param name="printerName">Name of the printer. Eg \\server_name\printer_name</param> /// <param name="pdfFileName">Name of the PDF file.</param> /// <returns></returns> public bool PrintPDF (string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\""; startInfo.FileName = ghostScriptPath; startInfo.UseShellExecute = false; Process process = Process.Start(startInfo); return process.ExitCode == 0; } 

Any idea how to make it work under C #?

+10
c # printing pdf ghostscript
Apr 08 '10 at
source share
3 answers

I finally made it work and easy to debug.
My final method code for those interested:

  /// <summary> /// Prints the PDF. /// </summary> /// <param name="ghostScriptPath">The ghost script path. Eg "C:\Program Files\gs\gs8.71\bin\gswin32c.exe"</param> /// <param name="numberOfCopies">The number of copies.</param> /// <param name="printerName">Name of the printer. Eg \\server_name\printer_name</param> /// <param name="pdfFileName">Name of the PDF file.</param> /// <returns></returns> public bool PrintPDF (string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\" "; startInfo.FileName = ghostScriptPath; startInfo.UseShellExecute = false; startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = true; Process process = Process.Start(startInfo); Console.WriteLine( process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd() ); process.WaitForExit(30000); if (process.HasExited == false) process.Kill(); return process.ExitCode == 0; } 
+13
Apr 08 '10 at 13:29
source share

You must first check your options from the command line, and then translate the successes into your code.

A PDF file usually already includes page margins. You often shorten page content, this may be the result of a PDF that is designed for A4 page size in letter format.

PDF also uses some internal fields that organize the content of the page (and object): MediaBox , TrimBox , CropBox , Bleedbox .

There are various control options for which the "media size" Ghostscript displays this input:

 -dPDFFitPage -dUseTrimBox -dUseCropBox 

With PDFFitPage Ghostscript will display the current page device size (usually this is the default page size).

With UseTrimBox it will use TrimBox (and it will at the same time set PageSize to this value).

With UseCropBox it will use CropBox (and it will set PageSize to this value at the same time).

By default (specify parameter) Ghostscript will be displayed using MediaBox .

Note that you can further control the overall size of your output with -sPAPERSIZE (select one of all the predefined Ghostscript values) or (for greater flexibility) use -dDEVICEWIDTHPOINTS=NNN -dDEVICEHEIGHTPOINTS=NNN to configure custom page sizes.

+3
Jun 05 2018-10-18T00:
source share

I'm not sure if this helps someone, but to add print documents to the queue instead of printing immediately, make changes to the above section using

 startInfo.Arguments = " -dPrinted -dNoCancel=true -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=mswinpr2 -sOutputFile=%printer%" + printerName + " \"" + pdfFullFileName + "\""; 

Prerequisites: Set your printer's job type to Hold Print: in our case, we have a Rico Aficio MP 4000 printer, and our use is to start night work to print a bunch of PDF files created through SSRS.

+3
Jan 24
source share



All Articles