我正在使用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
最佳答案
实际上,print_media_type 选项用于设置 pdf 文件的样式,而不是用于打印它。
要将 pdf 文件直接发送到打印机,您可以尝试以下操作:
生成 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
调用lpr命令打印保存的pdf文件
system("lpr", "pdfs/filename.pdf")
关于ruby-on-rails - 如何使用 Wicked PDF 自动打印生成的 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41585575/