Prawnto won't display pdf inline - ruby-on-rails

I'm trying to use prawn and prawnto (both installed via gems) in order to generate pdf's for my app. I want the pdf to display in the browser, but it won't, it automatically downloads instead.
I have the following prawnto options in my controller:
prawnto :inline => true, :filename => "results.pdf", :prawn => {:top_margin => 75}
The filename and margin arguments both work, but the document won't display inline. I'm pretty sure this is a prawnto issue and not a prawn issue. The prawn gem seems pretty old. Someone has created a new gem (prawnto_2) to update for rails 3.1, but I'm still using rails 3.0.7.
Has anyone else had this issue? How can I get prawnto to show the pdf inline (ideally in a new tab or window)?

The :inline option uses the Content-Disposition HTTP header, which relies on a browser plugin to interpret the content.
This means that the results can vary depending on the browser/OS combination you're using, Linux especially doesn't seem very good at handling this.

#benoit
Linux / Mozilla and Opera both open all pdfs on websites I visit - except on my site using this method. I get "open in application" or "save" as the only choices - it will not render inline.

Related

Rails Prawn Pdf gem suggestions needed

I have been using wkhtmltoppdf in my application for generating pdf's.Since now I have observed it's not supporting files over 1000+ pages. I am interested to give try to prawn pdf, as it seems faster than other gems.
Could anyone tell, anyway we have in prawn gem to convert html to pdf?
If not like pdfkit andd wkhtmltopdf do, then any way to debug pdf generated by prawn for styling like we have way for wkhtmltopdf?
Could anyone tell, anyway we have in prawn gem to convert html to pdf?
Prawn is not a html to pdf generator, they point this at official site http://prawnpdf.org/api-docs/2.0/
"One thing Prawn is not, and will never be, is an HTML to PDF
generator. For those needs, consider looking into FlyingSaucer via
JRuby, or one of the webkit based tools, like Wicked or PDFKit. We do
have basic support for inline styling but it is limited to a very
small subset of functionality and is not suitable for rendering rich
HTML documents."
If not like pdfkit andd wkhtmltopdf do, then any way to debug pdf
generated by prawn for styling like we have way for wkhtmltopdf?
To debug Prawn generated pdf I think the easiest way is to generate a new pdf or make your controller respond with your pdf:
Prawn::Document.generate("path/to/pdf/example.pdf") do
pdf.text "Hello"
end
Controller respond the pdf:
format.pdf do
pdf = PdfGenerator.new(#invoice)
send_data pdf.render,
filename: "pdf_test",
type: 'application/pdf',
disposition: 'inline'
end
And refresh your page every time that you make a change.
To test your generated pdf, pdf-inspector gem is good.
These tutorials may help you:
https://grzegorowski.com/using-prawn-gem-for-generating-pdfs-in-rails-5/
https://rubyplus.com/articles/3891-PDFs-with-Prawn-in-Rails-5

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

wicked_pdf and wkhtmltopdf to generate PDFs from files with a lot of Javascript content?

Has anyone used wicked_pdf and wkhtmltopdf to generate pdf files from rails?
wkhtmltopdf ( http://code.google.com/p/wkhtmltopdf/ ) is an c++ executable that the wicked_pdf gem ( https://github.com/mileszs/wicked_pdf ) essentially wraps. It works great for basic pdf generation of tables and reports.
We're now looking to print some pages that have a lot of javascript content. Since the processing is all done server-side, there's obviously no browser to interpret the javascript.
I've been looking around and can see that wkhtmltopdf supports some Javascript, but I'm also seeing that a lot of people are running into problems using it for complex javascript rendering.
Actually there is a browser engine in there. The WK in wkhtmltopdf stands for webkit which is the browser engine that is used to render HTML and javascript - Chrome uses that engine too.
Do you have a specific thing that does not work? I use javascript and I have seen some quite complex javascript being used as well, for example generation of charts and they have seemed to work good enough. The headers and footers often used with wkhtmltopdf rely heavily on javascipt as well.
I haven't used the ruby gem though.

Rails: How to print model's details to A4 PDF page?

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/

Resources