如何使用Wicked PDF自动打印生成的PDF?

Afs*_*fda 2 ruby ruby-on-rails wicked-pdf

我正在使用Wicked PDF,并且在单击按钮时在新选项卡中生成了pdf。但是我想通过单击按钮自动打印pdf 。我添加了这个print_media_type并将值设置为true,我希望我的打印机选项可以开始工作,但是什么也不会发生!有什么想法吗?

respond_to do |format|
        format.html
        format.pdf do
          render pdf: "file_name", :template => 'officials/individual_receipt_export.html.erb', encoding: 'utf8',page_size: 'A4',:print_media_type => true
        end
end
Run Code Online (Sandbox Code Playgroud)

小智 5

实际上,print_media_type选项用于设置pdf文件的样式,而不是用于打印。
要将pdf文件直接发送到打印机,您可以尝试以下操作:

  1. 生成pdf并将其保存到文件中:

    # create a pdf
    pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8"
    
    # then save to a file
    save_path = Rails.root.join('pdfs','filename.pdf')
    File.open(save_path, 'wb') do |file|
      file << pdf
    end
    
    Run Code Online (Sandbox Code Playgroud)
  2. 调用lpr命令以打印保存的pdf文件

    system("lpr", "pdfs/filename.pdf")
    
    Run Code Online (Sandbox Code Playgroud)