IIS使用acrord32.exe通过CMD打印PDF

时间:2015-05-20 00:11:23

标签: c# asp.net pdf iis printing

我有一个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提示符运行命令(这也有效)。

我还尝试通过网络应用程序运行一个powershell命令,其中列出了可用的打印机 - 我正在尝试打印的打印机也出现了。

任何纠正这一点的信息,或者肯定有效的替代方法,都将不胜感激。

感谢。

1 个答案:

答案 0 :(得分: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