Missing template when attaching pdf to actionmailer and wicked pdf - ruby-on-rails

I get Missing template when trying to send an email with a pdf attachment:
Controller
respond_to do |format|
format.html
format.pdf do
render pdf: "job card ##{#bike_service.id} - #{DateTime.now}", :template => 'bike_services/show.html.erb' # Excluding ".pdf" extension.
end
end
BikeServiceMailer.job_card_pdf_email(BikeService.last.id).deliver_now
end
Mailer
def job_card_pdf_email(bike_service_id)
bike_service = BikeService.find(bike_service_id)
attachments["bike_service_#{bike_service.id}-#{DateTime.now}.pdf"] = WickedPdf.new.pdf_from_string(
render_to_string(pdf: 'bike_service', template: File.join('app', 'views', 'bike_services', 'show.pdf.erb'), layout: 'pdf.html')
)
mail(to: todo.owner.email, subject: 'Your job card PDF is attached', bike_service: bike_service)
end
The show.pdf.erb template is there already.

I think you need to change the render_to_string line to either:
render_to_string(pdf: 'bike_service', template: File.join('bike_services', 'show.pdf.erb'), layout: 'pdf.html')
or
render_to_string(pdf: 'bike_service', template: Rails.root.join('app', 'views', 'bike_services', 'show.pdf.erb'), layout: 'pdf.html')
At the moment, I think the system is looking for app/views/bike_services/show.pdf.erb in /Users/vangama/projects/mm-crm/app/views which is effectively /Users/vangama/projects/mm-crm/app/views/app/views/bike_services/show.pdf.erb which doesn't exist.

Related

Ruby on Rails Wicked PDF Template Missing Error

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

wicket_pdf convert HTML to PDF without displaying HTML

If I render html page, I can get its content and use it to create PDF, like so:
content = params[:html]
But what if I don't want to display the HTML of the PDF I'm exporting? I tried different variations of this:
content = render_to_string(
partial: 'some_partial.html.erb', locals: #some_var
)
respond_to do |format|
format.pdf do
render pdf: 'some_pdf', locals: { content: content },
template: 'some_template.pdf.erb',
disable_javascript: true
end
end
But I just get errors, which leads me to believe that I'm coping this the wrong way...
What you could is
Create a file with a unique name -
unique_filename = Rails.root.join('public/', "#{SecureRandom.urlsafe_base64}.pdf").to_s
Use render_to_string to create the PDF -
pdf_file = render_to_string :pdf => unique_filename,
:template => 'some_template.pdf.erb',
:save_to_file => '#{unique_filename}'
:save_only => true
Save it to the file
File.open(unique_filename, 'wb') do |file|
file << pdf_file
end

Rails 4, ActionMailer & Wicked PDF

Controller method
def email_pdf
#job = Job.find_by_id(params[:id].to_i)
job_pdf = WickedPdf.new.pdf_from_string(render_to_string(:pdf => 'job', :template => 'job/show.pdf.erb', layout: 'mailer.html.erb'))
JobMailer.send_jobs_email(params[:id].to_i, 'vir.jain#gmail.com', 'Mahavir Jain', job_pdf).deliver_now
respond_to do |format|
format.json do
render :json => {:success => true}, :status => 200
end
end
end
Job Mailer
class JobMailer < ApplicationMailer
def send_jobs_email(job_id, email, name, job_pdf)
#name = name
#job = Job.find_by_id(job_id)
puts 'hello1'
attachments['job'] = job_pdf
puts #name
mail(to: email, subject: 'Job', from: 'info#janatrak.com', from_name: 'JanaTrak Admin')
end
end
Output
Rendered job/show.pdf.erb within layouts/mailer.html.erb (15.0ms)
"***************[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"file:///var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf20151007-4801-h2de03.html\", \"/var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf_generated_file20151007-4801-7uwh23.pdf\"]***************"
"***************[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"file:///var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf20151007-4801-mp6b6i.html\", \"/var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf_generated_file20151007-4801-3bayit.pdf\"]***************"
Rendered job_mailer/send_jobs_email.html.erb within layouts/mailer (0.1ms)
[{"email"=>"vir.jain#gmail.com", "status"=>"sent", "_id"=>"dc93087c39a949de827df06a1edb116e", "reject_reason"=>nil}]
Problem
From output, I can see, I am able to generate pdf properly but for some reason, it's not sending pdf as attachment. Also it's not sending email content. But when I remove attachment from code, it do properly send content of email.
I did checked
Rails 3 ActionMailer and Wicked_PDF
and tried both solution but none worked.
I'm using rails 4.2, ruby 2.1.2, wkhtmltopdf 0.12.2.1 (with patched qt).
Any help is really appreciated
***************** UPDATE ************************
Controller Method
def email_pdf
#job = Job.find_by_id(params[:id].to_i)
self.instance_variable_set(:#lookup_context, nil)
self.instance_variable_set(:#_lookup_context, nil)
job_pdf = WickedPdf.new.pdf_from_string(render_to_string(:template => 'job/show.pdf.erb', layout: 'mailer.html.erb'))
#job_pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
save_path = Rails.root.join('pdfs','job.pdf')
dir = File.dirname(save_path)
FileUtils.mkdir_p(dir) unless File.directory?(dir)
File.open(save_path, 'wb') do |file|
file << job_pdf
end
JobMailer.send_jobs_email(params[:id].to_i, 'vir.jain#gmail.com', 'Mahavir Jain', save_path).deliver_now
respond_to do |format|
format.json do
render :json => {:success => true}, :status => 200
end
end
end
Mailer Method
def send_jobs_email(job_id, email, name, job_pdf)
#name = name
#job = Job.find_by_id(job_id)
puts 'hello1'
attachments['job.pdf'] = File.read(job_pdf)
puts job_pdf
mail(to: email, subject: 'Job', from: 'info#janatrak.com', from_name: 'JanaTrak Admin')
end
PDF is generating properly & i can see saved pdf.. But again somehow actionmailer not attaching pdf as well as not displaying content..
Am I anything missing related to actionmailer attachment?

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

Export MS Word file Rails [duplicate]

How to convert ruby file in word file i.e (docx file). For pdf, we prawn gem. But is there any gem for word file. I am trying to convert my html file in word file so that it can be editable for user too. What should do in that case ? I was planning to convert that file in word file. Will it be possible or not.
If you are using Rails:
in initializers/mime_types.rb:
Mime::Type.register 'application/vnd.ms-word', :msword
in your controller:
say you want to export show action:
def show
#item = Item.find params[:id]
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #item }
format.msword { set_header('msword', "#{#item.title}.doc") }
format.pdf do
render :pdf => 'Coming soon...', :layout => false
end
end
end
define set_header in application_controller.rb:
def set_header(p_type, filename)
case p_type
when 'xls'
headers['Content-Type'] = "application/vnd.ms-excel; charset=UTF-8'"
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Cache-Control'] = ''
when 'msword'
headers['Content-Type'] = "application/vnd.ms-word; charset=UTF-8"
headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
headers['Cache-Control'] = ''
end
end
now define a show.msword.erb #you can use any template handler like haml etc.
YOUR HTML HERE TO EXPORT TO DOC
AS LIKE NORMAL ERB TEMPLATE
Use htmltoword gem.
https://github.com/nickfrandsen/htmltoword
it hasn't been updated since November 2015, but works well.

Resources