简体   繁体   English

使用javascript打印txt文件

[英]Print txt file using javascript

I would like to know if it is possible to print a txt file located in the server using javascript. 我想知道是否可以使用javascript打印位于服务器中的txt文件。 I have noticed that window.print() just opens the print dialog for the current web page 我注意到window.print()只打开当前网页的打印对话框

You can only open the print dialog for the user, and that is as it should be. 您只能为用户打开打印对话框,这应该是应该的。 If you only want to print the text document, there are a couple ways you can trigger the print dialog for it. 如果您只想打印文本文档,可以通过几种方法触发打印对话框。 They require following the Same Origin Policy (your HTML and TXT files need to be in the same domain). 它们需要遵循同源策略 (您的HTML和TXT文件需要位于同一个域中)。

The simplest way is to open a popup window with the text file, and call print on the window handle returned: 最简单的方法是打开一个带有文本文件的弹出窗口,并在窗口句柄上调用print返回:

w = window.open('text.txt');
w.print();

If you want the user to preview the text file, you could use an iframe instead: 如果您希望用户预览文本文件,则可以使用iframe代替:
I recommend keeping JS out of HTML, this is just for example 我建议将JS保留在HTML之外,这只是一个例子

<iframe id="textfile" src="text.txt"></iframe>
<button onclick="print()">Print</button>
<script type="text/javascript">
function print() {
    var iframe = document.getElementById('textfile');
    iframe.contentWindow.print();
}
</script>

The JQuery option JQuery选项

<body>

    <div id="txtdiv"></div>

    <script type="text/javascript">
      $('#txtdiv').load('trial.txt', function()
      {
        window.print(); //prints when text is loaded
      });

    </script>
 </body>

You are correct that window.print() just opens the print dialog for the current web page. 你是正确的window.print()只是打开当前网页的打印对话框。

I would suggest that you write JavaScript code to open a new window, load the text into that window, and then call the print() function on that window. 我建议你编写JavaScript代码来打开一个新窗口,将文本加载到该窗口,然后在该窗口上调用print()函数。

If you just do not want to delete the contents of the page and print some text from a file, you can do so here: 如果您只是不想删除页面内容并从文件中打印一些文本,可以在此处执行以下操作:

<body>

....some tags....

<script type="text/javascript">
// or onclick function
$.load('test.txt', function( printContent ){
    history.pushState( printContent, 'Print title', '/print_page' );
    document.write( printContent );
    if( window.print() ){
         document.location = '/back_page/';
         // or history.go(-1);
    } else {
         document.location = '/history/';
    }
});
</script>

You can do this by creating a web service. 您可以通过创建Web服务来完成此操作。

  1. Create a web service, and do printing stuff in the webservice. 创建Web服务,并在Web服务中打印内容。

  2. Call the web service from JavaScript. 从JavaScript调用Web服务。

If you are wondering how to do printing using webservice there is a thread in stackoverflow which might help. 如果您想知道如何使用webservice进行打印,则stackoverflow中有一个可能有用的线程 Dont just look the question, browse the answer as well. 不要只看问题,也要浏览答案。

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

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