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
Related
My objective is generate every month a PDF.
I am using Prawn Gem.
Likes this:
../Generatepdf/July2015.pdf
../Generatepdf/August2015.pdf
../Generatepdf/Setember2015.pdf
etc...
So my problem is how to define the urls?
I got a controller "Generatepdf" with a view "index.html"
I only can access of my PDF via:
../generatepdf/index.pdf
Controller:
class GeneratepdfController < ApplicationController
def index
respond_to do |format|
format.html
format.pdf do
pdf = ConsumptionsPdf.new
send_data pdf.render, filename: "lol2.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
end
Class:
class ConsumptionsPdf < Prawn::Document
def initialize
super
text "example"
end
end
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
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.
Creating Ruby on rails web application with qrcode display QR code in pdf
Iam using
gem 'rqrcode-with-patches', require: 'rqrcode'
gem 'prawn'
My post Controller to show pdf view
def show
#qr=RQRCode::QRCode.new(request.url)
respond_to do |format|
format.html
format.pdf do
pdf = PostPdf.new(#post)
send_data pdf.render, filename: "post#{#post.id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
post_pdf
class PostPdf < Prawn::Document
def initialize(post)
super({top_margin: 30, page_size: 'A4', page_layout: :landscape })
#post = post
post_id
post_title
qr_code
end
def post_id
move_down 10
text "Post id: #{#post.id}"
end
def post_title
move_down 10
text "Post title: #{#post.title}"
end
def qr_code
move_down 10
#qr=RQRCode::QRCode.new(request.url)
end
end
i got error on display pdf, it's show
undefined local variable or method `request' for #
There is a gem called prawn-qrcodes its really single to use.
require 'prawn/qrcodes'
Prawn::Document.generate("qr-big.pdf") do
msg = "Hello world, this is a QR code"
text msg
qrcode msg, :position => :center,
:fit => [bounds.height, bounds.width]
end
I think that this will work better for you than ** rqrcode-with-patches** because it was build for prawn
I'm trying to produce a PDF report with Prawn, I can get it to do a report on a show action easily enough by passing the single ID but I want to produce one with every record in it. Like a standard rails scaffold index page. Using rails it would look like this:
<% #customer.each do |customer| %>
<%= customer.id %>
<%= customer.name %>
<%end%>
Easy!
But I'm not sure how to do this with Prawn..
Something like:
def index
#customer = Customer.all
respond_to do |format|
format.html
Prawn::Document.generate("customer_list.pdf") do |pdf|
pdf.text "#{#customer.id} "
pdf.text "#{#customer.name} "
end
end
end
Which clearly isn't right.
Any ideas? Thank you.
It's easy to do with Prawn, Gemfile => gem 'prawn', bundle
lets say you have Customer model:
customers_controller.rb
def show
#customer = Customer.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = CustomerPdf.new(#customer)
send_data pdf.render, filename: "customer_#{id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
then just create pdfs folder under the apps directory, and create file customer_pdf.rb
class CustomerPdf< Prawn::Document
def initialize(customer)
super()
#customer = customer
text "Id\##{#customer.id}"
text "Name\##{#customer.name}"
end
end
show.html.erb
<div class="pdf_link">
<%= link_to "E-version", customer_path(#customer, :format => "pdf") %>
</div>
EDIT:
and don't forget to include pdf to config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
I think the good solution of your problem is custom renderer. The best approach was described by Jose Valim (!Rails core developer) in his book. The beginning of the first chapter is available for free here. This chapter is really what you need.
This how I do it:
class CustomerPdf< Prawn::Document
def initialize(customer)
super(page_size: "A4", page_layout: :portrait)
#customers = customer
bullet_list
end
def bullet_list
#customers.each do |customer|
text "•#{customer.id}- #{customer.name} ", style: :bold
move_down 5
end
end
end