如何使用 GhostScript (gswin32c.exe) shell 命令在默认网络打印机上打印 PDF [英] How to print PDF on default network printer using GhostScript (gswin32c.exe) shell command

查看:29
本文介绍了如何使用 GhostScript (gswin32c.exe) shell 命令在默认网络打印机上打印 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 GhostScript 在 windows 的网络打印机上打印 PDF 文件.
(我不想使用 Adob​​e Reader)

I'd like to print PDF file(s) on windows' network printer via GhostScript.
(I dont want to use Adobe Reader)

我读过 gswin32c.exe 可以完成这项工作.
我尝试了许多命令,但找不到如何强制 gs 在我的(Windows 默认)网络驱动器上打印 PDF 的方法.

I've read gswin32c.exe which can do the job.
I experimented with many commands and coudn't find the way how to force gs to print PDF on my (windows default) network drive.

我不需要点精确的网络打印机-可以使用默认值.但是如果没有这样的选项,我也很乐意传递打印机名称.(我试过使用参数 -SDevice="server_IPprinter_name" 但这并没有奏效......)

I don't need point exact network printer- default can be used. But if there is no such option I'm happy to pass printer name as well. (I've tried with param -SDevice="server_IPprinter_name" but this didnt work as well...)

Windows cmd 下的命令:

Command working under Windows cmd:

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

基于上述创建的方法 - 不起作用并且引发异常.(错误代码 = 1)

Method created base on above - doesnt work and thorws exception. (Error code = 1)

    /// <summary>
    /// Prints the PDF.
    /// </summary>
    /// <param name="ghostScriptPath">The ghost script path. Eg "C:Program Filesgsgs8.71ingswin32c.exe"</param>
    /// <param name="numberOfCopies">The number of copies.</param>
    /// <param name="printerName">Name of the printer. Eg \server_nameprinter_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;
    }

知道如何让它在 C# 下工作吗?

Any idea how to make it working under C#?

推荐答案

我终于让它工作并且易于调试.
我为感兴趣的人提供的最终方法代码:

I've finally made it working and easy for debugging.
My final method code for those interested:

    /// <summary>
    /// Prints the PDF.
    /// </summary>
    /// <param name="ghostScriptPath">The ghost script path. Eg "C:Program Filesgsgs8.71ingswin32c.exe"</param>
    /// <param name="numberOfCopies">The number of copies.</param>
    /// <param name="printerName">Name of the printer. Eg \server_nameprinter_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;
    }

这篇关于如何使用 GhostScript (gswin32c.exe) shell 命令在默认网络打印机上打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆