开始通过PHP服务器端打印作业 [英] Starting serverside print job via PHP
问题描述
这是最有可能不容易的,但这里的情况是:
This is most likely not an easy one but here is the situation:
我写了一个C#命令行应用程序,其中:
I have written a C# command line application which:
- 创建一个使用iTextSharp的一个PDF
- 将其写入到磁盘
- 使用
Acrord32.exe
(这是Acrobat Reader软件),通过的System.Diagnostics.Process
以默默打印生成的PDF
- creates a PDF using ITextSharp
- writes it to disk
- uses
Acrord32.exe
(this is Acrobat Reader) viaSystem.Diagnostics.Process
in order to silently print the generated PDF
如果我建立我的解决方案,然后双击 pdfGen.exe
,它按预期工作。该PDF创建和打印。
If I build my solution and double click the pdfGen.exe
, it works as expected. The PDF is created and printed.
现在,我的应用程序已被部署Windows Vista中运行的内部服务器上的IIS 7,该服务器有一些PHP Web应用程序的运行。它会通过PHP使用了shell_exec()
使生成的PDF将连接到服务器的打印机上进行打印。
Now, my app has to be deployed on a internal server with Windows Vista running IIS 7. This server has some PHP webapp running. And it will be called via PHP using shell_exec()
so that the resulting PDF will be printed on the printer attached to the server.
所以我的PHP页面基本上是这样的:
So my PHP page looks basically like this:
shell_exec('/path/to/pdfGen.exe');
但在这里出问题。会发生什么事,根据任务管理器等是:
But here things go wrong. What happens is according to task manager etc.:
-
pdfGen.exe
启动 - 创建PDF
-
Acrord32.exe
启动 -
pdfGen.exe
永远挂(也是如此的PHP脚本),并没有打印
pdfGen.exe
starts- the PDF is created
Acrord32.exe
startspdfGen.exe
hangs forever (and so does the PHP script) and nothing is printed
我敢肯定这是一些的许可相关的问题的。我已经给了 IIS_IUSRS
访问默认打印机,到 Acrord32.exe
所在的目录。但结果还是没有打印。不过,如果我开始了我的pdfGen.exe手动它的作品。
I am pretty sure it is some permission related problem. I already gave IIS_IUSRS
access to the default printer, and to the directory where Acrord32.exe
is located. But still, no printing. However, if I start my pdfGen.exe manually it works.
任何想法,我缺少什么?
Any idea what I am missing?
编辑:
我不一定要使用Acrobat Reader软件,以打印PDF。如果有另一种方式,以默默的打印创建的PDF服务器端,我不会介意。
I am not bound to use Acrobat Reader in order to print the PDF. If there is another way in order to silently print the created PDF serverside, I would not mind at all.
推荐答案
感谢所有为您的意见。不幸的是这个PHP启动的PrintJob的事情是,今天取消了,因为,嗯......我不知道......政治原因,一个更大的项目的一部分。猜猜这个项目是非常死。
Thanks all for your comments. Unfortunately this "php start printjob" thing was part of a larger project that was cancelled today because of, well... I dont know... political reasons. Guess the project is pretty much dead.
不管怎样,我想我自己在最后的日子几次,不能让它在IIS下工作。我的解决方案,我实施和测试已经:删除IIS,与当地的Apache和PHP与的管理员访问权限运行安装XAMPP或WAMPP包。
Anyway, I tried myself a few more times in the last days and could not get it to work with IIS. My solution that I implemented and tested already: remove IIS, install a XAMPP or WAMPP package with a local apache and PHP that runs with admin access rights.
这做的伎俩。我用函数,pclose(popen这('...命令','R'));
在PHP以启动 .exe文件
键,使PHP不等到PDF完成。这一切伟大的工作。
This did the trick. I used pclose(popen('...command...', 'r'));
in PHP in order to start the .exe
and so that PHP does not wait until the PDF is finished. It all worked great.
下面是开始使用Acrobat Reader软件打印作业我的C#代码
Here is my C# code which starts the print job using Acrobat Reader
public void Print(string pathname, string acrobatDirectory)
{
var proc = new Process
{
StartInfo =
{
Arguments = String.Format("/t \"{0}\"", pathname),
FileName = acrobatDirectory,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = false,
RedirectStandardError = false,
}
};
proc.Start();
}
第一个参数是路径的PDF应打印,第二参数是对 AcroRd32.exe
。
剩下的唯一问题是绝对路径 AcroRd32.exe
开始,印刷和从未被再次关闭。所以每PrintJob的开始(我使用的Acrobat Reader 9.0) AcroRd32.exe
的新实例。所以,如果你印刷10次,10 Acrobat Reader软件创建实例。
The only problem left was that AcroRd32.exe
was started, printed and never got closed again. So every printjob started a new instance of AcroRd32.exe
(I am using Acrobat Reader 9.0). So if you printed 10 times, 10 acrobat reader instances were created.
我所做的就是启动打印作业,然后等待X秒,希望打印机已经完成,然后杀害所有 AcroRd32.exe
实例:
What I did was starting the print job, then waiting X seconds, hoping that the printer was finished and then killing all AcroRd32.exe
instances:
public void Print(string pathname, string acrobatDirectory)
{
Debug.WriteLine("Printing...");
Printer.Print(pathname, acrobatDirectory);
Thread.Sleep(30000);
try
{
Debug.WriteLine("Trying to kill runnung AcroRd32.exe's ");
FindAndKillProcess("AcroRd32");
}
catch (Exception)
{
Debug.WriteLine("AcroRd32.exe could not be killed...");
}
}
private bool FindAndKillProcess(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.StartsWith(name))
{
clsProcess.Kill();
return true;
}
}
return false;
}
这结果很不错。
请注意,以上(全部遇难 AcroRd32.exe
并运行与管理privilegs PHP)仅为可行这是因为:整件事一次只能使用一个用户,拥有使用非常有限的区域
Note that the above (killing all AcroRd32.exe
and running PHP with admin privilegs) was only doable because: The whole thing is only used by one user at a time and has a very limited area of use.
它应该被用来在部署在客户端POS触摸屏的应用程序。一位售货员将使用PHP应用程序,以配置产品,然后PHP将打电话给我的.exe这将创建并在后台打印PDF。然后打印的文档被传递给客户机。 所以安全性等是不是真的在这种情况下一个问题。
It should be used on a touchscreen application deployed at the clients POS. A salesman would use the PHP app in order to configure a product, and then PHP would call my .exe which would create and print a PDF in the background. The printed document is then handed to the client. So security etc. was not really a concern in this case.
如果有人为了与IIS使用一个解决方案,我仍愿意接受它作为一个答案。
这篇关于开始通过PHP服务器端打印作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!