Ruby on Rails Wicked PDF Template Missing Error - ruby-on-rails

I am currently using rails 7 with wicked pdf . wicked pdf is throwing me an missing template error even if I have template file at exact place? what am i doing wrong?
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "file_name", template: "stocks/pdf.html.erb"
end
end
end
what am i doing wrong ?
I am using tailwindcss with jsbuild if that matters.

in case your template is stocks/pdf.html.erb, your render should be as following:
format.pdf do
render pdf: "file_name", template: "stocks/pdf", formats: [:html]
end
Let me know if this works for you.
Cheers

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 convert existing html file to pdfs in rails 4?

I have an html file generate_invoice.html.erb. I have to provide a button on the same file to convert generate_invoice.html.erb into a downloadable pdf.I have tried using wicked pdf gem and gem 'wkhtmltopdf-binary'
then I also created the config/initializers/wicked_pdf.rb and set the mime types to pdf
my routes is as follows:
match 'orders/:id/invoice' => 'orders#order_invoice', as:
'order_invoice', via: [:put,:post,:get]
My order_controller is as follows
def order_invoice
#base_url = ENV['base_url']
#order = Order.includes(:status, :user, payment:
[:status]).where(id: params[:id]).first
if (['Notary', 'Attestation','Franking'].include?
#order.service.name)
#no_of_copies = ((#order.answers.where(question_id:
[37,15]).length
> 0) ? #order.answers.where(question_id: [37,15]).first.body :
0).to_i
else
#no_of_copies = ((#order.answers.where(question_id:
[37,15]).length
> 0) ? #order.answers.where(question_id: [37,15]).first.body :
0).to_i + 1
end
render 'order_mailer/generate_invoice',layout: false
respond_to do |format|
format.pdf do
render pdf: "order_invoice"
end
end
end
it however throws this error
ActionController::UnknownFormat at /admin/orders/131/invoice
ActionController::UnknownFormat
basically it is pointing at respond_to do |format| line
What am I doing wrong.Please help me rectify this.Also please let me know the next course of action to achieve the pdf format
I assume your order_invoice file has a format of order_invoice.html.erb instead of order_invoice.pdf.erb which should be the correct format.
respond_to do |format|
format.html
format.pdf do
render pdf: "order_invoice"
end
end
and I hope you already have the view file "order_invoice.html.erb"

Rails 4 wicked pdf Issue

I'm trying to use wicked pdf to render a html page as pdf.
mime_types.rb
Mime::Type.register "application/pdf", :pdf
controller method
def invoice
respond_to do |format|
format.html
format.pdf do
render pdf: "invoice"
end
end
end
wicked_pdf.rb
:exe_path => '/usr/local/bin/wkhtmltopdf'
invoice.pdf.erb
<div id="test_pdf">
test data
</div>
I have added the above codes and added the following gems to my project.
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
When I try to render the page, i get Template is missing error. If i rename invoice.pdf.erb to invoice.html.erb i can bypass the error, but i will be getting a html page instead of pdf.
Do I miss something?
As mentioned in the document of wicked_pdf, you can use it like this. Its self explanatory. Don't forget to create "pdfs" directory
# or from your controller, using views & templates and all wicked_pdf options as normal
pdf = render_to_string pdf: "some_file_name", template: "templates/pdf.html.erb", encoding: "UTF-8"
# then save to a file
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
file << pdf
end
send_file save_path, :type=>'text/pdf
This code will be more than enough if you just want to render the pdf:
def show
respond_to do |format|
format.html
format.pdf do
render pdf: 'file_name',
template: 'example/pdf_view.pdf.erb',
layout: 'layouts/application.pdf.erb'
end
end
end
If you want to save as pdf:
def save
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
template: 'example/pdf_view.pdf.erb',
layout: 'layouts/application.pdf.erb'))
send_data(pdf,
filename: 'file_name.pdf',
type: 'application/pdf',
disposition: 'attachment')
end

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