In my current work i want to generate pdf and pre store into my application and whenever user wants to download pdf user can download pdf easily. I can't generate pdf on the fly because of some limitation.
Previously we are using prawn but as complexity increases and time decreases we started looking into another solution, we came across mainly pdfkit,wicked_pdf,acts_as_flying_saucer library.
We initially started with pdfkit to test our need it full fill majority all requirement,but as i say i want to generate PDF and store into my rails app.So it is easy to generate from HTML but we need to generate PDF from erb template (HAML can also work) in backend.
Basically it is 3 step process
- Read ERB(Haml,...) file and store into string.
- Parse file using erb with necessary parameter.
- Generate PDF
So i am sharing my small idea it works for me.
require 'rubygems' require 'pdfkit' require 'erb' # Read ERB File content = File.read('path/of/erb/template') # Create new ERB template by passing required variable template = ERB.new(content) # Create new object to PDFKIt kit = PDFKIT.new(template.result(binding)) # Save into PDF file = kit.to_file('/path/to/save/pdf')
We can set configuration for pdfkit and erb as per our requirement.
I hope this is helpful,if any other way we can generate PDF kindly share it.
Thanks for posting! Your post helped me achieve something similar in our application.
ReplyDeleteHi , how do we pass params to ERB.new(content) ?
ReplyDeleteThanks, It is really very helpful.
ReplyDelete