0

is there any way of printing a file on a printer in rails? Let's assume I have a

  def print_url
     "/system/tickets/#{print_url_dir}/print.png"
  end

and I want to call it with

  resource.print_url

I tried already

  <%= link_to 'Print', resource.print_url, :onclick => 'window.print();return false;'%>

But that just opened the printer dialog for the site I am on.

Sorry if it is a stupid question or I am missing something.

Thanks and all the best!

Ala Ha
  • 39
  • 4

1 Answers1

0

If you want to do this from a webpage - it's more of a Javascript task rather than Ruby/Rails one. You can do something like this:

<script type="text/javascript">
  var WinPrint = window.open('', '_blank', 'left=0,top=0,toolbar=0,scrollbars=0,status=0');
  WinPrint.document.write('<img src="<%= resource.print_url %>" />');
  WinPrint.document.close();
  WinPrint.focus();
  WinPrint.print();
  WinPrint.close();
</script>

It creates and open new window with content that contains only the image that would like to print, then print the page (which you already noticed that invokes the dialog) and the closes that new window. (I don't think there's a way to skip the dialog opening though).

You can check this answer for more information and examples.

Viktor Nonov
  • 1,472
  • 1
  • 12
  • 26