简体   繁体   中英

RoR render PDF and print it automatically

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>

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.

window.print(); will worked

But the browser should support pdf files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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