简体   繁体   English

在C#Web API Web服务中使用Process()调用GhostScript

[英]Calling GhostScript with Process() in C# Web API Web Service

I am trying to print a pdf file from a WebAPI Web Service by calling GhostScript using Process() 我试图通过使用Process()调用GhostScript从WebAPI Web服务打印pdf文件

This works perfectly when 'Debugging' an ASP.NET application on my local machine, but when I use the exact same code to try and print through a Web Service - also on my local machine - it doesn't work. 这在我的本地计算机上“调试”ASP.NET应用程序时非常有效,但是当我使用完全相同的代码尝试通过Web服务进行打印时 - 也在我的本地计算机上 - 它不起作用。

I don't get any kind of exception in VS - so far as the application is concerned, the application is working fine. 我在VS中没有任何异常 - 就应用程序而言,应用程序运行正常。

Here's the code I'm using to call the GhostScript printer: 这是我用来调用GhostScript打印机的代码:

Process process = new Process();

process.StartInfo.Arguments = @" -dPrinted -dBATCH -dNOPROMPT -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -dNoCancel -sPAPERSIZE=a4 -sOutputFile=\\spool\\\printserver\printer c:\\test\test.pdf";


process.StartInfo.FileName = @"C:\Program Files (x86)\GPLGS\gswin32c.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;

StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();

using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
    process.OutputDataReceived += (sender, e) => {
        if (e.Data == null)
        {
            outputWaitHandle.Set();
        }
        else
        {
            Console.Write(e.Data);
            output.AppendLine(e.Data);
        }
    };
    process.ErrorDataReceived += (sender, e) =>
    {
        if (e.Data == null)
        {
                errorWaitHandle.Set();
            }
            else
            {
                Console.Write(e.Data);
                error.AppendLine(e.Data);
            }
        };

    process.Start();

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
    if (process.HasExited == false) process.Kill();
    return process.ExitCode == 0;

I thought this might have something to do with printers not being installed for All Users so have added them for all users. 我认为这可能与没有为所有用户安装的打印机有关,所以已经为所有用户添加了它们。 The only other thing that I can see it being is Permissions - but can't figure out a solution. 我唯一能看到的是权限 - 但无法找到解决方案。

Does anyone have any ideas? 有没有人有任何想法?

Thanks in advance for your help. 在此先感谢您的帮助。 :) :)

As suspected, this turned out to be a Permissions issue. 怀疑,这被证明是一个权限问题。 After publishing the Web Service to IIS, I changed the Default Application Pool to use a User Identity, the printing suddenly started working correctly. 将Web服务发布到IIS后,我将默认应用程序池更改为使用用户标识,打印突然开始正常工作。

I will set up a user account specifically for printing - with no write permissions - and use this for now. 我将专门设置一个用于打印的用户帐户 - 没有写入权限 - 现在就使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM