2016-02-11 241 views
-4

我有一個pdf文件,由wicked_pdf gem生成。通過默認打印機打印軌道生成的pdf

使用紅寶石1.8.7,有什麼辦法可以自動連接到Ubuntu的默認打印機,用於打印生成PDF。

我很喜歡你的回答。

+0

哪個操作系統? –

回答

0

假設你的PDF文件保存到一個文件,這可能工作:

pdf_file_path = "/tmp/pdf_file.pdf" 
`lpr "#{pdf_file_path}` 

如果內容是一個Ruby字符串:

pdf_file = WickedPDF.create_the_pdf_somehow 
IO.popen('lpr', 'r+') do |pipe| 
    pipe.print pdf_file 
    pipe.close_write 
end 
0

您可以系統輕鬆完成( 'lpr')命令,如果你的操作系統是Ubuntu的(或者Mac)。這是我的應用程序,它的作品就像一個魅力。

def general_receipt_export 
    pdf = render_to_string pdf: "#{@ids.map(&:inspect).join(',').to_s + '_receipt.pdf'}", :template => 'officials/general_receipt_export.html.erb', encoding: 'utf8',orientation: 'Landscape',page_size: 'A4' 
    render layout: false 
    save_path = Rails.root.join('public','pdfs', "#{@ids.map(&:inspect).join(',').to_s + '_receipt.pdf'}") 
    File.open(save_path, 'wb') do |file| 
     file << pdf 
    end 
    system("lpr", "public/pdfs/#{@ids.map(&:inspect).join(',').to_s + '_receipt.pdf'}") 
end 
+0

欣賞你的答案 – Qmr

相關問題