2
votes

I'm looking for a way for a Rails action to automatically render to a printer using a defined template (or view + partials, like .html.erb or similar), so that the user/client can print without the printer being setup locally. The server is running Linux and it has the ability to print to the user-chosen printer on-site. And it's ok if it first generates a temporary PDF if needed. So in summary, we're looking for something similar to "render to CUPS" (and/or lpr) in Rails.

Here's what I've found so far:

According to Ruby Toolbox, Prawn (and prawn-labels looks nice), Wicked PDF, or PDFKit are some good ways, among others to generate PDFs in Ruby, with the latter two being more oriented toward rendering HTML in Webkit and generating a PDF from that.

However, another team member looked into it more and noticed that wkhtmltopdf (that both Wicked PDF and PDFKit use) hasn't been released since 2010, and, even though there continues to be active work on it, it uses Webkit via Qt 4.4 vs. directly, and we'd like to avoid that dependency if it isn't needed. He noticed an alternative might be Shrimp that uses PhantomJS which includes Webkit (and here's a post about it).

Our other concern is page breaking. We want to make sure that we have a way to indicate that some content belongs together so that some sections of the templates or views/partials do not get broken during PDF creation or printing. It was indicated that there may be some problems with some versions of Webkit related to this.

(Note: I also found this old question with some info.)

For printing those PDFs directly to one or more printers, I'm told that CUPS can print PDF files directly without conversion to PS or anything. Ruby Toolbox shows that the cups gem (quick usage guide here) may be a good choice.

(Note: I also found an example for Ruby->lpr via Open3, but we'll probably use CUPS.)

1
Hi Gary up vote 2 down vote favorite Trying to print prn file using "cupsffi" ruby gem , But its treating prn file as simple text file. I am following github.com/nehresma/cupsffi/blob/master/README.rdoc link Please suggestuser3928330

1 Answers

1
votes

wkhtmltopdf is using the older version of Qt because they depend on certain patches for hyphenation support, multiple columns, and other specific printing-related needs.

I've switched to using PrinceXML to generate PDFs from HTML+CSS, which has the advantage of being very fast, very reliable, and has support for every printing-related need, from margin boxes to robust hyphenation, to widow, orphan, page-break control. It's commercial and expensive, which is the only downside I can find.

I've used HP's ePrint service to email PDFs to network connected printers, which has been reliable. (Disclosure: I work for HP.)