Rails: wicked_pdf "file_name" option - ruby-on-rails

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.

Related

How to share a view between formats with Rails

I have a Rails controller which provides both HTML and PDF responses, and thus I have view.pdf.haml and view.html.haml files. These are either identical or extremely close to identical.
How can I have Rails use a single view for multiple formats?
You can specify what format to render with :formats option:
# Both will render view.html.haml
respond_to do |format|
format.html { render :view }
format.pdf { render :view, formats: :html }
end
https://guides.rubyonrails.org/layouts_and_rendering.html#the-formats-option
Do you mean have the same action serve up different resource types - i.e. both a web page and a PDF? If I understand the problem, you can use the respond_to method.
def show
#object = Object.find params[:id]
respond_to do |format|
format.html
format.pdf { ..code to render the pdf.. }
end
end
That ..code to render the pdf.. is the tricky part. There are PDF gems that support inline rendering, but you may end up using send_data or similar to set a file name, disposition, etc and deliver the PDF document to the end user.

Wicked PDF generating pdf only on page refresh

Am working on a web app and I have this "Registration" page. I want to render a pdf page when I visit the show page. But when i click on the show button a normal html page is loaded and the content is like a dump, all gibberish (here is a gist with the content: https://gist.github.com/anonymous/0806200b7ca31bba35d3a514a7ff90e6). However, when I refresh the page it renders the pdf perfectly.
I checked if there was any differences in the url and params but everything was the same. I also checked the instance variables and they were the same. And I googled the issue and didn't find anything on it.
here is the rendering code in the show action:
respond_to do |format|
format.html do
render pdf: 'customer_print_out.pdf',
file: "#{Rails.root}/app/views/pdf/customer_print_out.pdf.erb",
page_size: 'A4',
encoding: 'UTF-8'
end
end
I would like to note that I am generating a barcode in the pdf file. It is also loaded correctly when I refresh the page and when I remove the barcode generating code I still get the same issue.
am using:
Rails v4.2.6, wicked_pdf v1.1.0, puma v3.6.0
Any help or pointers are really appreciated.
In order to render pdf, do the following:
Register the PDF mime type in the config/initializers/mime_types.rb file:
Mime::Type.register "application/pdf", :pdf
In your controller do the following:
class YourController < ApplicationController
def show
#product = Product.first
respond_to do |format|
format.html
format.pdf do
render pdf: 'customer_print_out.pdf',
file: "#{Rails.root}/app/views/pdf/customer_print_out.pdf.erb",
page_size: 'A4', encoding: 'UTF-8'
end
end
end
end
The request should be in pdf format, for example: your_controller_path(format: :pdf)
I think you should use format.pdf instead of format.html
format.pdf do
render pdf: 'customer_print_out.pdf',
file: "#{Rails.root}/app/views/pdf/customer_print_out.pdf.erb",
page_size: 'A4',
encoding: 'UTF-8'
end
also don't forget to specify format as pdf in your button's code, something like
<%= link_to('Show', your_show_action_path(format: :pdf)) %>

How to render a PDF in the browser that is retrieve via rails controller

I have a rails app that uses Recurly. I am attempting to download a PDF and render it in the browser. I currently have a link:
link_to 'Download', get_invoice_path(:number => invoice.invoice_number)
The associated controller has the get_invoice method that looks like so:
def get_invoice
begin
#pdf = Recurly::Invoice.find(params[:number], :format => 'pdf')
rescue Recurly::Resource::NotFound => e
flash[:error] = 'Invoice not found.'
end
end
When I click the link I get the PDF rendered in my console in binary form. How do I make this render the PDF in the browser?
You don't render the PDF to the browser, you send it as a file. Like so:
# GET /something/:id[.type]
def show
# .. set #pdf variable
respond_to do |format|
format.html { # html page }
format.pdf do
send_file(#pdf, filename: 'my-awesome-pdf.pdf', type: 'application/pdf')
end
end
end
The HTML response isn't needed if you aren't supporting multiple formats.
If you want to show the PDF in the browser instead of starting a download, add disposition: :inline to the send_file call.
Assuming the PDF is saved in memory, use the send_data to send the data stream back in the browser.
def get_invoice
#pdf = Recurly::Invoice.find(params[:number], :format => 'pdf')
send_data #pdf, filename: "#{params[:number]}.pdf", type: :pdf
end
If the file is stored somewhere (but this doesn't seem to be your case), use send_file.

Render XML as file, not inline

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

Using Prawn & Prawnto for Rails PDF generation

This is probably more a design or usage question but the main issue is using the Prawn plugin with Rails 2.3.4 and accessing the resulting PDF object to render it to a file.
The scenario is basically;
a controller with the respond_to block setup
a view with code for rendering the text, graphics etc to PDF
It looks like:
From Customer Controller
def show
#customer = Customer.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #customer }
format.pdf { render :layout => false }
end
From Customer View
pdf.text "Hello World"
pdf.text "Customer is #{#customer.name}"
This works fine, producing a PDF file in response to /customers/1.pdf, as expected with a PDF file being generated.
One of the other requirements is to render the pdf to a file and store a copy on the server. Prawn provides the method:
pdf.render_file {path_tofile}
So now if I include this in the View code I can of course save the file on the server. But I'd like to manage this in the controller, as it's related to logic, not view per se.
The first attempt was :
def show
#customer = Customer.find(params[:id])
#pdf = Prawn::Document.new()
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #customer }
format.pdf { render :layout => false }
pdf.render_file {path_to_file}
end
From Customer View
pdf.text "Hello World"
pdf.text "Customer is #{#customer.name}"
PROBLEM
The problem with this attempt is that the PDF is not rendered. I suspected the Controller instance variable is clashing with the Prawnto Plugin pdf variable. But even changing the PDF variable didn't work.
Any suggestions ?
This is how I use prawn in my rails app: http://yob.id.au/2009/05/30/prawn-and-x-accel-redirect.html - it doesn't use prawnto at all.
You can ignore the X-Accel-Redirect stuff unless you use nginx.
Alternatively, another one of the prawn core devs has put together this guide: http://wiki.github.com/sandal/prawn/using-prawn-in-rails

Resources