Rails: How to print model's details to A4 PDF page? - ruby-on-rails

In my Job Management application I would like to prepare an A4 page receipt to customer.
Thus, I need to print somehow Job model's details to a single A4 PDF page.
Are the any built-in tools in Rails for this purpose ?
If no, what would be the best way to go ?

Basically there is two options: (any PDF creation requires a gem - no default PDF creation for rails).
Create a pure PDF using Prawn, You have to do all the formatting using the Prawn API
Create a HTML version of your receipt and convert it to PDF, One of the better gems to do this is PDFkit. which uses a web-kit powered browser engine.
They both work good, For one page documents I usually use PDFkit to convert HTML and for larger documents that are going have lots of pages I use Prawn because it gives you a smaller file size and handles multiple pages better.
My suggestion would be to make a HTML receipt and display it on the screen and give the user an option to save a PDF version using PDFkit.
EDIT: windows install. (not tested - windows and I have parted company.)
Download the windows installer for wkhtmltopdf: win-wkhtmltopdf
now create an initializer file, e.g. config/intializers/pdfkit_config.rb
in pdfkit_config.rb set the absolute path to wkhtmltopdf on your local machine:
PDFKit.configure do |config|
if RAILS_ENV == 'development'
config.wkhtmltopdf = 'D:\path\to\your\wkhtmltopdf' #this bit i'm not sure about
else
config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
end
end
for your production ENV you can actually just have a copy of wkhtmltopdf in you repo, a unix version of course. (remember to chmod +x it before you git add it)

Theres a gem called prawn that helps with PDF generation. Here is a tutorial using it for some ideas:
http://railstips.org/blog/archives/2008/10/13/how-to-generate-pdfs-in-rails-with-prawn/

Related

Forbid printing on generated PDFs (via prawn + combine_pdf)

I need to take a template (a pdf file) and add elements in it. I used prawn gem to generate a pdf with the elements and combine_pdf gem to combine this prawn pdf and the template. It works fine!
Now I need to add permissions to disable printing options. Prawn has an option for that but if I add it, I can't use combine_pdf to combine my template and this prawn pdf. Prawn can't start with an existing pdf (template system has been removed a long time ago...).
So I'm pretty stuck here, I looked everywhere in the web for a solution but couldn't find anything!
instead of combine_pdf gem you can use pdf-toolkit. it supports templating, combine pdf and set permissions. the manual can be found from here.

Mixed orientations in wicked pdf

Currently I am using wicked_pdf gem to create pdf in my ROR (ruby on rails application).
My pdf have 4 pages. In which 1 is chart. I need that page alone to be landscape dynamically.
Is it possible using wicked_pdf or any other gem?
PS: I tried to generated each pdf and combining them but the image
changes pages based on the content of the previous pages.
wicked_pdf is a plugin that relies on the wkhtmltopdf project to generate the actual PDFs. wkhtmltopdf does not yet implement this feature, so until that is complete there is no easy way to get mixed-orientation PDF files.
There is an outstanding feature request to implement this, so watch that issue for any updates. It appears that at this time there is some work being done on this issue but it will take time for it to both make it into wkhtmltopdf and be picked up by wicked_pdf.
In the meantime there is a workaround that some people have attempted involving CSS rotations and media selection stylesheets.
Others have attempted to generate multiple PDFs separately and stitch them together with some programs like ghostscript. See the above workaround page for more details.

Ruby on Rails, Prawn - Repairing a corrupt PDF in Rails

I'm using the Prawn gem (v 0.12.0, we can't use 2.0.0 because of compatibility issues) in a Ruby on Rails project. A user uploaded a PDF that they got directly from an official government website, so this is a PDF that we will have to handle for other users as well, but it is corrupt and crashes when Prawn tries to render it as a string. You can find the PDF in the "Certificate of Rent Paid" link on this page.
The error I'm getting with this PDF:
NoMethodError - undefined method 'size' for nil:NilClass:
And it's coming from this method in our Prawn gem:
def finalize
if dictionary.data[:Contents].is_a?(Array)
dictionary.data[:Contents].each do |stream|
stream.compress_stream if document.compression_enabled?
stream.data[:Length] = stream.stream.size # THIS LINE!!!
end
else
content.compress_stream if document.compression_enabled?
content.data[:Length] = content.stream.size # IT DOESN'T GET TO THIS
end
end
I'm stuck. The only tools I've been able to find through my searches to "fix" PDFs are Adobe Acrobat and PDFtk. PDFtk can be used in a server, but it's only for Windows and our server is running on a Linux server. I can't find any gems or any way around it. I was also able to "fix" the PDF by "Saving as PDF" from Google Chrome (v41) on a Mac (neither Windows nor Ubuntu Google Chrome worked). I want to be able to "fix" the PDF from within my Rails project though. Any thoughts/suggestions?
[EDIT] Using Ghostscript from the command line worked for me. This line: gs -o repaired.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress crp_14.pdf, "fixed" the PDF. Looking into the RGhost gem now to see if I can do the same thing from within a controller or model.
After Prawn dropped template support, I wrote a Ruby native gem called combine_pdf.
I can't test it with your file, but you might be able to load the PDF using the gem, render it to a string (or temporary file) and reload within Prawn.
OR, maybe it could help you move on to Prawn 2.0 (you can create new PDF data with Prawn and stamp it over an existing file with combine_pdf).
It's just a thought, don't kill me if it doesn't work ;-)
Good luck!

generate PDF from html document in Rails

anyone knows about some gem or app in rails to create pdf docs from html5 and css3 with some client side programming??
Thanks ;)
Thank you for your responses. I need this for a possible project wich will use jquery for box positioning and then, will export the resultant html to pdf. Roughly, ¿is this possible?
PDFKit and WickedPDF for Client side generation, as suggested by Raphael and jcadam
Flying Saucer with JRuby: http://xhtmlrenderer.java.net
Personally, I got more accomplished by using Prawn PDF generator. It's capabilities are much more extensive, IMHO.
Prawn for Ruby PDF generation: https://github.com/prawnpdf/prawn
WickedPDF: https://github.com/mileszs/wicked_pdf
I'm using it now and rather like it:
"Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked take care of the hard stuff."
If you need more control, also check out Prawn: https://github.com/prawnpdf/prawn. Though Prawn is not an HTML -> PDF converter and is more useful for creating PDFs from scratch.
I'm using both Prawn and WickedPDF on my current project: WickedPDF to generate 'printable' versions of web pages, and Prawn for generating detailed PDF reports.
I believe the PDFKit gem may be what you're looking for.
https://github.com/pdfkit/PDFKit
There's also IMGKit if you want images instead of PDFs.
https://github.com/csquared/IMGKit

Pdf generation with latex in rails 3

is there a way to generate pdf documents from latex in rails 3? We've been using rtex (http://rtex.rubyforge.org/) in a rails 2 application, however it doesen't seem to work with rails 3.
Our rails application generates invoices using a latex template which we also use to create invoices by hand. Hence we would have to maintain two templates if we had to find a different solution for the pdf generation in rails 3.
Best I found to do such things was to create the .tex files on the server, then call a rake task that ran a "pdflatex" system command.
It is pretty poor in performances I guess, but it's designed for a single admin and works fine for me, on my local machine, and I can use the same latex templates for my letters
Old question, but I'm sure this'll help anyone coming to this page now.
Take a look at the rails-latex (LatexToPdf) gem.
The LatexToPdf.generate_pdf method takes in two arguments:
tex content
a configuration hash
...and returns the pdf binary, which you'll have to write to a file.
I suggest reading through the source to if you need to add configuration.
Note that under the hood, the rails-latex gem still depends on a TeX extension (which you'll need to download) to generate the pdf. The default is pdflatex, and I've personally used xelatex.
As of the writing of this answer, this gem is described as a renderer for rails 3; though it now includes support for rails 4 and 5.

Resources