1

I'm using wicked_pdf to render a PDF-File. The Web-App runs in chrome with kiosk-mode (--kiosk --kiosk-printing). The problem is that i have to open the print-dialog when the PDF is rendered, so that the PDF is printed automatically via kiosk.

I tried this with the Javascript-function window.print(); in my PDF-/HTML-Template. But the function is not executed and no print-dialog is opened.

Here my controller-method (materials_stocks_controller.rb):

def create
@material = Manufacturer::MaterialsStock.new(params[:manufacturer_materials_stock])

respond_to do |wants|
  if @material.save
    @materials    = Manufacturer::MaterialsStock.ordered_collection
    wants.js { render :layout => false }
    wants.pdf {

      render :pdf => "Material-Barcode",
             :template => 'manufacturer/material_info',
             :show_as_html => !params[:debug].blank?,
             :page_height => 62,
             :page_width  => 110,
             :margin => {
                 :top     => 3,
                 :bottom  => 3,
                 :left    => 0,
                 :right   => 3
             },
             :use_xserver => false,
             :print_media_type => true,
             "load-error-handling"=> "ignore"
    }
  else
    raise "Unable to create material in stock"
  end
end
end

The view (material_info.pdf.erb):

<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script>
    function printing_dialog() {
      window.print();
    }
  </script>

</head>
<body onload="printing_dialog()">

  <!-- stuff ... -->

</body>
</html>

Has anyone an idea how to open the print-dialog?

Thanks!! :)


Update:

I replaced the template-code completely with the page-number-JS-Code from the wicked_pdf-Docu and the JS is not running was well:

new Code in material_info.pdf.erb (for testing):

<html>

<head>
  <script>
      function number_pages() {
          var vars={};
          var x=document.location.search.substring(1).split('&');
          for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
          var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
          for(var i in x) {
              var y = document.getElementsByClassName(x[i]);
              for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
          }
      }
  </script>
</head>
<body onload="number_pages()">
Page <span class="page"></span> of <span class="topage"></span>
</body>
</html>
Regolith
  • 2,944
  • 9
  • 33
  • 50
levitas111
  • 109
  • 2
  • 11
  • other ideas are welcome as well ;) – levitas111 Oct 21 '14 at 14:52
  • Have you tried invoking it in JS console? (like Chrome Dev Tools at F12) – D-side Oct 21 '14 at 18:38
  • yes and window.print(); worked in the console – levitas111 Oct 22 '14 at 08:11
  • the only main-problem is that the JS-Script is not executed and i don't know why :/ it's simple as hell, but i have no idea where the problem is ... – levitas111 Oct 22 '14 at 09:12
  • in debug-mode (as HTML) the JS-script runs without problems – levitas111 Oct 22 '14 at 09:15
  • 2
    Ah, so you want to execute JS right from PDF? I'm afraid it's only executed during generation in order to reflect changes caused by it. After that, no JS might be available in the document. There is a certain possibility that you could use JS for Adobe Acrobat API, but I wouldn't rely on it too much. – D-side Oct 22 '14 at 14:56
  • 1
    I think you'll need to load a plain HTML page, and load the PDF in an iframe or hidden element, then use a ` – Unixmonkey Oct 22 '14 at 19:24
  • Thanks D-side and Unixmonkey for your answers. Indeed my solution was to load a normal HTML-page with an iframe that has the PDF inside. @D-side: Yes, you're right of course ;) i will post my solution in the answer :) – levitas111 Oct 23 '14 at 08:46

2 Answers2

2

So here is my solution:

A new plain HTML-File (create.html.erb) with the following code:

<html>
<head>

  <%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
  <%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>

  <script>
      $(document).ready(function() {
          pdf = document.getElementById('pdf_source');
          pdf.focus();

          setTimeout(function(){
              pdf.contentWindow.print();
          }, 3000);

      });
  </script>

</head>
<body>


<% url = "#{request.protocol}#{request.host_with_port}/admin/printing_manufacturer_materials_stock?file=#{@file_name}" %>
<iframe id ="pdf_source" src="<%=url %>" style="width:700px;height:500px;"></iframe>


</body>
</html>

In the controller i created the PDF and saved it. The printing-method is sending the saved file (send_file) depending on the param :file.

levitas111
  • 109
  • 2
  • 11
0

window.print(); will worked

But the browser should support pdf files.

Amit Agarwal
  • 396
  • 3
  • 12