I want to generate a document with prawn that uses a PDF template.
I get no errors, but the template is completely ignored. Does anyone know why this might be? I would really appreciate any help!
class JobPdf < Prawn::Document
def initialize(job)
super()
#job = job
text #job.title
text #job.target_text
end
def render(job)
doc = Prawn::Document.new({template: "#{Rails.root}/app/templates/jobs/layout.pdf"})
doc.text job.title
doc.text job.target_text
doc.render
end
end
Jobs Controller:
def show
#job = Job.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = JobPdf.new(#job)
send_data pdf.render(#job),
filename: "job#{#job.id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
Prawn has removed templates from its core apparently it is possible to use https://github.com/prawnpdf/prawn-templates to still utilize them but note even prawn states that this is "very buggy"
There is a tutorial, to perform such a task:
http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/
But in order to make it work you have to use PDFtk besides Rails.
Related
I am new to rails and I am trying to use Prawm to generate pdf's. I am getting this error:
ActionController::UnknownFormat
Here is the code:
def show_deposited_checks
deposit_id = params[:format]
if deposit_id.present?
#payments = Payment.where(:deposit_id => deposit_id)
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "Hello World"
send_data pdf.render
end
end
else
#payments = Payment.all.limit(10)
end
end
I an redirecting from here:
redirect_to show_deposited_checks_payments_path(deposit)
I have another method that works just fine so I suspect it has something to do with the redirect and the way the controller is receiving the data. Any help with be greatly appreciated.
I think the problem is that where doesn't execute immediately. There's probably a better way(I'm fairly new to Rails too), but I think this should work:
#payments = Payment.where(:deposit_id => deposit_id).take(10) #or how ever many you want included
I got wicked_pdf working, but one of the render options is file_name which as far as I can tell has no bearing at all on the PDF rendering. Can someone explain to me what exactly this is for?
class ThingsController < ApplicationController
def show
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name"
end
end
end
end
It is ultimately passed to Rails' send_data method as the filename option, which:
:filename - suggests a filename for the browser to use.
So when you say:
format.pdf { render pdf: 'the_answer' }
It becomes:
send_data(pdf_content, :filename => 'the_answer.pdf',..
The send_data method uses this (with other options) to set the Content-Disposition header in the response. If you inspect the response (e.g. in Chrome's Developer Tools) you will see:
Content-Disposition:inline; filename="the_answer.pdf"
How the browser implements this is ultimately up to it, but in Chrome you can see it by right clicking on the PDF and clicking 'Save as...'
Note: The presence of the :pdf key is used to decide whether wicked_pdf should handle it, so it must be specified if you wish you use this render helper.
I have the option in my app to render a XML version of an invoice. To do this, I use a separate class, because it has some complicated calculations. This works fine and it renders the XML nicely in the browser.
However, I prefer to have it downloaded as file instead. How can I achieve this?
In the controller I now have this:
def show
#invoice = Invoice.find(params[:id])
respond_to do |format|
format.xml { render xml: #invoice.render_xml }
end
end
I know you can add options to get it downloaded:
filename: 'mydoc.xml', type: "application/xml", disposition: 'attachment'
But how do I combine it with my specific code?
Change render in your controller to send_data, and add the options that you have in your question. http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
I have an index controller action which responds to the pdf format which looks something like
class ProposalsController < ApplicationController
respond_to :pdf, :html
def index
#proposals = Proposal.all
respond_with #proposals do |format|
format.html
format.pdf
end
end
end
Proposal has a to_pdf method which creates Prawn::Document. How can I loop over each of the propsals, grab it's PDF, append it to a newly created PDF, and then render that in the browser via respond_with?
One can use rails API to create a personalized renderer. In fact, the book Crafting Rails 4 Applications by José Valim show you how to create personalized PDF renderer in the first chapter.
I know this has been asked a lot, but I've looked at all the simple solutions and they're not working.
Firstly, .js.erb responses are working for all my other controllers...I generated a new controller and it won't respond to .js.erb
def report
#report = Report.all
respond_to do |format|
format.js do
render :content_type => 'text/javascript'
end
end
end
This route does work and will render html. I have no idea why .js.erb would work for my other controllers and not for a new one. I've done plenty of fiddling before making this new one...so debugging will be hard for me.
Remove the entire respond_to block. Rails will pick the correct view:
def report
#report = Report.all
end
If you are getting a 406
You need to explicitly disable erb's layout template checking.
render layout: false
You can keep respond_to and format if you need them for supporting other formats.
(NOTE: dynamic js for Content Security Policy purposes?)