Rails 4 wicked pdf Issue - ruby-on-rails

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

Related

Missing template when attaching pdf to actionmailer and wicked pdf

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.

Rails - Dynamically generate PDF from HTML and show link to download

I have a rails application in which a user can click a link on a page and a little popup opens up with some HTML on it. I'd like to put another link next to it called 'Download as PDF' and when someone clicks this link, I'd like to dynamically generate a PDF from the HTML and allow the user to download the generated PDF onto their system. I've seen a few pages on stackoverflow which talk about using pdfkit to generate pdf from html but nothing about dynamically generating the pdf and then making it available to download. Please help!
The best way to do this is by using gem prawn
Here is youtube of how to upload it to your ruby on rails website https://www.youtube.com/watch?v=BW5zwqj37Lo&t=233s
https://github.com/prawnpdf/prawn
Here is example of how to add gem prawn
gem 'prawn', '~> 1.2.1'
gem 'prawn-table', '~> 0.1.0’
Mime::Type.register "application/pdf", :pdf
def index
#users = User.order("id DESC").all
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text “hello
send_data pdf.render, filename: 'member.pdf', type: 'application/pdf', disposition: "inline"
end
end
end
<%= link_to 'PDF File', members_path(format: 'pdf') %>
def index
#users = User.order("id DESC").all
respond_to do |format|
format.html
format.pdf do
pdf = MemberPdf.new(#users)
send_data pdf.render, filename: 'member.pdf', type: 'application/pdf', disposition: "inline"
end
end
end
pdfs folder
member_pdf.rb
class MemberPdf < Prawn::Document
def initialize(users)
super(top_margin: 170)
#users=User.order("id DESC").all
user_id
end
def user_id
move_down 20
table user_id_row do
row(0).font_style = :bold
columns(1..3).align = :right
self.row_colors = ["DDDDDD","FFFFFF"]
self.header = true
end
end
def user_id_row
[["id","First","Middle","LastName","Email","Address","Address2","City","PostalCode","phone","Status","Child","Intrest"]] +
#users.map do |user|
[user.id,user.first_name,user.middle_name,user.last_name,user.email,user.street_address,user.street_address2,user.city,user.postal_code,user.phone_number,user.status,user.child,user.interest]
end
end
end

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

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)) %>

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