Pdf Writer for Rails 3

Does anyone know of a PDF writer who works easily and well with Rails 3? If so, can someone point me to it and instruct how to configure it to work with Rails 3?

+4
source share
5 answers

There are two ways to create a PDF file in Rails.

Either you create it from scratch, or use something to create a pdf file from html. the second is simpler, but it may not be enough if what you are trying to embed in the PDF is more complex.

If you need full control over your pdf file, you must create it from scratch. To do this, use a combination of prawn and prawnto .

For the second option, which is simpler, you can use PDFKit , which uses wkhtmltopdf to create pdf directly from the html page. I recommend starting with this and see if it is suitable for your purposes; then if you think you need more control and this is not enough, use shrimp.

+2
source

For those of you who, like me, several times ago, came here to find a way to make PDF :: Writer work in rails 3 application, perhaps redirecting an existing Rails 2.x application, I finally reached PDF-writer , version 1.1.8, to work in the rails 3.2 application.

In the gemfile :

gem 'pdf-writer' 

I have this in config / initializers / pdfwriter_template.rb

 module ActionView module Template::Handlers class PDFWriter def call(template) require_engine "pdf = ::PDF::Writer.new( :paper => 'A4' );" + "pdf.compressed = true if Rails.env.to_s == 'production';" + template.source + ";pdf.render" end protected def require_engine @required ||= begin require 'pdf/writer' require 'pdf/simpletable' true end end end end end ActiveSupport.on_load(:action_view) do ActionView::Template.register_template_handler :rpdf, ActionView::Template::Handlers::PDFWriter.new end 

then in the controller action I can write something like:

 def pdf_test @record = Record.find(params[:id]) fileout = render_to_string :layout=>false send_data(fileout, :type => "application/pdf", :filename => 'test_record.pdf') end 

having the pdf_test.rpdf template file :

 pdf.text "Hello from record number #{@record.number}!!" 

!!!!! CAUTION: This is not the most efficient way to render PDF in Rails !!!!!! Use PDFKit or Shrimp Instead !!!!!

+2
source

I would recommend shrimp. More information about this can be found at this link http://prawn.majesticseacreature.com/

+1
source

Released a small pearl that makes pdf files using shrimp in Rails https://github.com/rtsinani/gambas

+1
source

I suggest you check out the Railscasts PDF episodes , there are a lot of resources.

0
source

Source: https://habr.com/ru/post/1336613/


All Articles