7

I would like to know if it is possible to print a txt file located in the server using javascript. I have noticed that window.print() just opens the print dialog for the current web page

2
  • It is not possible with only javascript. You should use also ajax with or without some server scripting language. Commented Jun 27, 2012 at 14:21
  • 2
    You can't force someone to print a page if they don't want to. The print dialog is how users decide how to print something, or decide if they don't actually want to print anything. Commented Jun 27, 2012 at 14:56

5 Answers 5

15

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).

The simplest way is to open a popup window with the text file, and call print on the window handle returned:

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

If you want the user to preview the text file, you could use an iframe instead:
I recommend keeping JS out of HTML, this is just for example

<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>
Sign up to request clarification or add additional context in comments.

2 Comments

In his question, he is saying that he wants the file to be printed not just show the print dialog.
@sv_in, you're right, i forgot to add the disclaimer that you can't force a user to print something they don't want to.
3

The JQuery option

<body>

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

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

    </script>
 </body>

Comments

1

You are correct that window.print() just opens the print dialog for the current web page.

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.

Comments

1

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>

Comments

-3

You can do this by creating a web service.

  1. Create a web service, and do printing stuff in the webservice.

  2. Call the web service from JavaScript.

If you are wondering how to do printing using webservice there is a thread in stackoverflow which might help. Dont just look the question, browse the answer as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.