php 如何将外部文件的内容发送到打印机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5627035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:01:24  来源:igfitidea点击:

How to send the content of an external file to printer?

phpfileprinting

提问by Jugal Patel

I want to print (printer, not screen) the content of a file via a PHP script.

我想通过 PHP 脚本打印(打印机,而不是屏幕)文件的内容。

How do I do this?

我该怎么做呢?

回答by rockerest

Update

更新

php cannot easily access hardware. This is generally not considered "possible."

php 无法轻松访问硬件。这通常不被认为是“可能的”。

See:

看:

However, as the first link shows, this is usually done with Javascript. You can output Javascript in a way similar to the methods shown on the first link to force the browser to show the print dialog box.

但是,如第一个链接所示,这通常是使用 Javascript 完成的。您可以以类似于第一个链接中显示的方法的方式输出 Javascript,以强制浏览器显示打印对话框。

Original

原来的

You can use file_get_contentsto print files into a variable or to the output stream.

您可以使用file_get_contents将文件打印到变量或输出流中。

$filecontents = file_get_contents("myfilename.txt");
print $filecontents;

You can also includefiles into your PHP interpretation.

您还可以在 PHP 解释中包含文件。

回答by Blindy

A quick and dirty way to print on the client's computer is something like:

在客户端的计算机上打印的一种快速而肮脏的方式是这样的:

print file_get_contents("file.ext");
print "<script>window.print()</script>";

回答by mario

This is certainly not what your question intended, but on any Linux server with a connected printer you could use following:

这当然不是您的问题的意图,但是在任何带有连接打印机的 Linux 服务器上,您可以使用以下内容:

exec("lp file.pdf");   // send file to printer spooler

回答by caglaror

May be thiswill help you. It offers a java library to send print jobs via cmd over php scripts.

可能会帮助你。它提供了一个 Java 库,可以通过 cmd 通过 php 脚本发送打印作业。

回答by k to the z

// to open a local file use this
$file_handler = fopen("data.txt", "r"); 


// read the contents 
$contents = fread($file_handler, filesize($file)); 

// close the file 
fclose($file_handler); 

// print the contents on your page 
echo $contents;