IIS 使用 acrord32.exe 通过 CMD 打印 PDF
IIS Print PDF through CMD using acrord32.exe
我有一个 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 提示符 运行ning 命令(这也有效).
我还尝试 运行通过 Web 应用程序使用 powershell 命令,它列出了可用的打印机 - 我尝试打印的打印机也出现了。
如能提供任何有关纠正此问题的信息或肯定有效的替代方法,我们将不胜感激。
谢谢。
这不能用 Acrobat Reader 完成,因为它在打印时总是显示它是 UI,并且不允许您从 [=15= 显示 UI ] 服务。这就是您在 Windows 服务器上使用 IIS 时所做的事情。
正如@PeterHahndorf 在评论中提到的,您应该使用可以为您打印 PDF 的 PDF 组件。如果商业组件适合您,您可以尝试 Amyuni PDF Creator .Net (Disclaimer: I work for Amyuni Technologies). If a library/application with an AGPL license is ok, you can try calling ghostscript from the command line for printing the file.
我有一个 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 提示符 运行ning 命令(这也有效).
我还尝试 运行通过 Web 应用程序使用 powershell 命令,它列出了可用的打印机 - 我尝试打印的打印机也出现了。
如能提供任何有关纠正此问题的信息或肯定有效的替代方法,我们将不胜感激。
谢谢。
这不能用 Acrobat Reader 完成,因为它在打印时总是显示它是 UI,并且不允许您从 [=15= 显示 UI ] 服务。这就是您在 Windows 服务器上使用 IIS 时所做的事情。
正如@PeterHahndorf 在评论中提到的,您应该使用可以为您打印 PDF 的 PDF 组件。如果商业组件适合您,您可以尝试 Amyuni PDF Creator .Net (Disclaimer: I work for Amyuni Technologies). If a library/application with an AGPL license is ok, you can try calling ghostscript from the command line for printing the file.