且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

开始通过PHP服务器端打印作业

更新时间:2023-11-27 21:48:16

感谢所有为您的意见。不幸的是这个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使用一个解决方案,我仍愿意接受它作为一个答案。