简体   繁体   English

使用javascript打印本地文件

[英]Print a local file with javascript

Today I have a hard challenge with php+javascript+html+css: 今天,我在php + javascript + html + css方面遇到了艰巨的挑战:

I'm creating an application to "authorization+print it": the app must run in a local apache(under windows preferably), and need a conection to a DB in the cloud. 我正在创建一个“授权+打印”应用程序:该应用程序必须在本地apache中运行(最好在Windows下),并且需要连接到云中的数据库。 That's easy. 这很容易。 With the user authenticated, a list of authorized files must be shown, another easy task too. 通过用户身份验证后,必须显示授权文件列表,这也是另一项简单的任务。 It's working. 工作正常 But now comes the crazy: I want to print them without let the user select and see any print option, only a button "Print". 但是现在变得疯狂了:我想打印它们而不让用户选择并查看任何打印选项,而只是一个按钮“打印”。 The print configuration comes in a .txt file and I need to configure the printing, sending the file and the configuration to the printer. 打印配置位于.txt文件中,我需要配置打印,然后将文件和配置发送到打印机。

I searched a lot, but I only see the "print this page" buttons or shell solutions (gsview and gsprint for windows, but I cannot use that because I cannot configure the print options). 我进行了很多搜索,但我只看到“打印此页”按钮或外壳解决方案(用于Windows的gsview和gsprint,但由于无法配置打印选项而无法使用)。 I need anything more complex. 我需要更复杂的东西。 Could you help me? 你可以帮帮我吗? (Im trying now fpdf, but...omg, I cannot understand If this could be used to do that I want. (我现在正在尝试fpdf,但是...哎呀,我不明白是否可以用它来实现我想要的功能。

Non-free/installed solutions could be in help too. 非免费/已安装的解决方案也可能会有所帮助。

In addition, I need to print multiple files, but that's optional (I can do anything like "while") 另外,我需要打印多个文件,但这是可选的(我可以执行“ while”之类的操作)

PD: sorry for my english level. PD:对不起,我的英语水平。

Print from client-side = form the browser via javascript 从客户端打印=通过javascript形成浏览器

It's not possible to do this from client-side (= from inside the Browser). 不可能从客户端(=从浏览器内部)执行此操作。 There are hackish solutions out there, which might work for IE, like the one here: HTML / Javascript One Click Print (no dialogs) , but in general "if you try to print, the dialog will pop up" = default behaviour of "window.print()". 那里有一些骇人听闻的解决方案,可能适用于IE,例如: HTML / Javascript一键打印(无对话框) ,但通常“如果您尝试打印,该对话框将会弹出” =默认行为“ window.print()”。

Print from server-side 从服务器端打印

Basically you use the server-side (PHP) to print the document, instead of the client. 基本上,您使用服务器端(PHP)而不是客户端来打印文档。 So you might use an Ajax request (user clicks on the print button) handing the filename or the content to print over to the "print.php" file on the server, which does the job of pushing the content to the printer. 因此,您可以使用Ajax请求(用户单击打印按钮)将文件名或内容打印到服务器上的“ print.php”文件,该工作将内容推入打印机。

Of course, you'd have to know which printer the user wants the content to be printed at... 当然,您必须知道用户希望在哪个打印机上打印内容...

There are several ways to print from PHP. 有几种从PHP打印的方法。

One option would be to use the php_printer extension: 一种选择是使用php_printer扩展名:

$handle = printer_open(); 
printer_set_option($handle, PRINTER_MODE, "raw"); 
printer_write($handle,$myfile); 
printer_close($handle);

Or just copy or print to the printer: 或者只是copyprint到打印机:

exec('copy C:\file.txt com1');
exec('copy C:\file.txt lpt1');
exec('print /d:LPT1: C:\file.txt');

If you have network printer, you could try to send your content to the network address. 如果您有网络打印机,则可以尝试将内容发送到网络地址。 There are some PHP utils around to work with LPR: https://github.com/Craswer/PhpNetworkLprPrinter 有一些PHP实用程序可以与LPR一起使用: https : //github.com/Craswer/PhpNetworkLprPrinter

Referencing: https://stackoverflow.com/a/5695181/1163786 引用: https : //stackoverflow.com/a/5695181/1163786


Question from comment: How can i set printer options from PHP on Windows? 来自评论的问题:如何在Windows上的PHP中设置打印机选项?

This very easy on Linux because lpr accepts options lpr <options> - but that's not the case on Windows. 这在Linux上非常容易,因为lpr接受lpr <options> -但是在Windows上不是这样。 So, here are some Windows specific tricks to configure the printer: 因此,这里有一些Windows特定的技巧来配置打印机:

Windows7 has PRINTUI.EXE - a shorthand for RUNDLL32 PRINTUI.DLL,PrintUIEntry Windows7具有PRINTUI.EXE - RUNDLL32 PRINTUI.DLL,PrintUIEntry的简写

Please see PrintUI Reference for examples. 有关示例,请参见PrintUI参考

You can configure your printer manually, for instance activate duplex mode, then save the settings file and re-use it, when printing from PHP. 从PHP打印时,您可以手动配置打印机,例如激活双面打印模式,然后保存设置文件并重新使用它。 This allows to work with multiple printer configuration files. 这允许使用多个打印机配置文件。

The easiest way is to configure the printer in your env and then access it by name, "Printer-HP-XY-DuplexOn-2PagesOn1". 最简单的方法是在环境中配置打印机,然后按名称“ Printer-HP-XY-DuplexOn-2PagesOn1”进行访问。 In other words: it's configured outside and not from within PHP, only accessed from there. 换句话说:它是在外部配置的,而不是从PHP内部配置的,只能从那里访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM