【问题标题】:IIS Print PDF through CMD using acrord32.exeIIS 使用 acrord32.exe 通过 CMD 打印 PDF
【发布时间】:2015-08-01 00:32:11
【问题描述】:

我有一个 Web 应用程序,我正在尝试直接打印到打印机。 该代码在本地工作,但是,我似乎无法让它在测试服务器上工作。

方法如下;

internal void PrintStockTransactionPdf(string documentNumber, EnStockTransactionType transType)
{
    Guid fileName = Guid.NewGuid();
    string tempFilePath = String.Format("{0}{1}.pdf", Path.GetTempPath(), fileName.ToString());
    string adobePath = this._blSettings.GetSettingByName<string>("adobeReaderPath");
    string printerPath = this._blSettings.GetSettingByName<string>("printerPath");
    string url = "";

        // Retrieve the correct PDF
     if(transType == EnStockTransactionType.StockAdjustment)
         url = string.Format(this._blSettings.GetSettingByName<string>("stockadjustmentreporturl"), "PDF", documentNumber);
     else if (transType == EnStockTransactionType.StockRelocation)
         url = string.Format(this._blSettings.GetSettingByName<string>("stockrelocationreporturl"), "PDF", documentNumber);
     else if (transType == EnStockTransactionType.StockCheckout)
         url = string.Format(this._blSettings.GetSettingByName<string>("stockcheckoutreporturl"), "PDF", documentNumber);
     else if (transType == EnStockTransactionType.StockReturn)
         url = string.Format(this._blSettings.GetSettingByName<string>("stockreturnreporturl"), "PDF", documentNumber);

    // Save temp file
    File.WriteAllBytes(tempFilePath, this._blStock.GetStockTransactionFile(url));
    Process p = new Process()
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "cmd.exe",
            RedirectStandardInput = true,
            UseShellExecute = false
        }
    };
    p.Start();

    using (StreamWriter sw = p.StandardInput)
        if (sw.BaseStream.CanWrite)
        {
            // Print command
            sw.WriteLine(@"""{0}"" /h /t ""{1}"" ""{2}""", adobePath, tempFilePath, printerPath);
            // Delay 5 seconds to allow print to complete before killing Adobe
            sw.WriteLine("ping 1.1.1.1 -n 1 -w 5000 > nul");
            // Kill Adobe
            sw.WriteLine("taskkill /F /IM acroRD32.exe");
        }

    // Delay 5 seconds server side, before removing the temp file
    if (p.HasExited == false)
        p.WaitForExit(5000);
    p.Close();

    // Remove the temp file
    File.Delete(tempFilePath);

我尝试重定向标准输出,结果如下;

Microsoft Windows [Version 6.2.9200](c) 2012 Microsoft Corporation. All rights reserved.
c:\windows\system32\inetsrv>"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /h /t "C:\Windows\TEMP\b15df292-8fd5-46fb-8f8e-3427b6cc37b4.pdf" "\\<loc>\<printer>"
c:\windows\system32\inetsrv>ping 1.1.1.1 -n 1 -w 5000 > nul
c:\windows\system32\inetsrv>taskkill /F /IM acroRD32.exe
SUCCESS: The process "AcroRd32.exe" with PID 9652 has been terminated.
c:\windows\system32\inetsrv>

这是正确的,如果我直接通过命令提示符运行它就可以工作。

Web 应用程序模拟用户(在服务器上,而不是本地),我已经登录到服务器,并尝试通过 cmd 提示符运行命令(这也有效)。

我还尝试通过 Web 应用程序运行 powershell 命令,该命令列出了可用的打印机 - 我尝试打印的打印机也会出现。

任何纠正此问题的信息,或肯定有效的替代方法,将不胜感激。

谢谢。

【问题讨论】:

  • 为什么它可以在我的本地机器上运行? (即,如果我从 vs 运行代码,并且它通过 IIS Express 运行)。有没有其他方法可以实现将 pdf 文档从 web 应用程序打印到特定打印机的最终结果?
  • 因为 IIS Express 是一个不同的野兽,它是一个用户进程,而不是一个服务器进程(我仍然很惊讶它在那里工作)。您应该使用支持打印的 PDF 组件,不要在您的网页中启动 Acrobat reader 等其他进程。即使这样的组件(如果存在)也应该位于 IIS 进程之外的 Windows 服务中,并打印您的网站要求的 PDF。
  • 那么,我可以将现有代码移动到 Windows 服务中,然后从 Web 应用程序启动该服务吗? (对不起,我真的没有任何创建 Windows 服务的经验)。再次感谢您提供任何信息。

标签: c# asp.net pdf iis printing


【解决方案1】:

Acrobat Reader 无法做到这一点,因为它会在打印时始终显示其 UI,并且不允许您从 Windows 服务显示 UI。这就是您在 Windows Server 上使用 IIS 时所做的事情。

正如@PeterHahndorf 在评论中提到的,您应该使用可以为您打印 PDF 的 PDF 组件。如果商业组件适合您,您可以尝试使用Amyuni PDF Creator .Net免责声明:我为 Amyuni Technologies 工作)。如果带有AGPL license 的库/应用程序没问题,您可以尝试calling ghostscript from the command line for printing the file

【讨论】:

  • 感谢您的帮助。我最终使用 ghostscript 来使打印方法起作用。将其放入 Windows 服务有什么好处?为什么不能直接从网站调用呢?
  • 关于在您的网站上启动外部进程,具体取决于您的网站将有多少并发请求,将这个任务从您的网页中移除确实是一个好主意。
猜你喜欢
  • 2021-01-13
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
相关资源
最近更新 更多