在Ruby中将文件打印到打印机

我需要帮助,使用Ruby on Ruby on Rails将格式化文本发送到打印机,或者从Ruby程序将pdf文件发送到打印机。 我可以编写代码来从Rails应用程序创建一个pdf文件,但不知道如何将该pdf文件打印到默认打印机。 我正在尝试使用Ruby on Rails编写一个小型票务应用程序。

这是Windows环境中的解决方案:Foxit! http://www.foxitsoftware.com/pdf/reader/

您可以调用foxit.exe(独立可执行文件)以在后台静默打印PDF。

语法:foxit.exe / t“your file.pdf”“打印机名称”

shell = WIN32OLE.new('Shell.Application')

shell.ShellExecute("foxit.exe","/t \"#{filename}\" \"#{printer}\"")

在Linux和Mac OS X上,您可以使用“lpr”命令行程序,将其传递给PDF文件的名称(虽然不确定Windows)。 例如:

 def print_to_paper your_code_to_write_a_pdf_file("file.pdf") system("lpr", "file.pdf") or raise "lpr failed" end 

我不确定是否有办法在网上打印默认打印机上的东西。 在我的银行,当我想要付款确认时,他们给我pdf,我必须手动打印。 我认为这是很好的解决方案。 只需添加一些有关它的信息。

我有一个内部应用程序用于创建计算机标签。 我从我从戴尔获得的文件导入计算机或手动输入它们,我将它们导出为CSV,我可以将其导入MS SCCM。 我可以打印标签放在电脑上。 标签上有公司徽标,计算机名称,MAC地址和服务台联系信息。

我用gLabel打印它。 你在gLabels中设计一个标签,输入动态字段并向其提供一个CSV,它会发出一个PDF然后我用lpr打印到我的Dymo Labelwriter。

我把它放在我的计算机模型中因为我不知道在哪里放它。

 # Print all computers with printed = false def self.print printed_labels = 0 csv_file = Tempfile.new(["computers", ".csv"]) logger.debug("Writing #{csv_file.path}") begin Computer.transaction do Computer.unprinted.each do |computer| csv_file.puts "\"#{computer.mac(' ')}\",\"#{computer.hostname}\"" computer.printed = true computer.save printed_labels += 1 end end ensure csv_file.close if csv_file.length > 0 pdf_file = Tempfile.new(["computers", ".pdf"]) begin pdf_file.close system '/usr/bin/glabels-batch', "--input=#{csv_file.path}", "--output=#{pdf_file.path}", AssetBase::Application.config.computer_label system '/usr/bin/lpr', '-P', 'LabelWriter-450', pdf_file.path ensure pdf_file.unlink end end csv_file.unlink end printed_labels end 

这在Fedora Linux上运行,因此打印机后端是CUPS,它处理PDF的部分我不知道。 它可以是CUPS或打印机驱动程序或打印机驱动程序本身。

还有其他方法可以为PDF创建结构化文本,但对于标签,gLabels非常棒。