Render Prawn pdf and rqrcode display QR with Ruby on rails - ruby-on-rails

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

Related

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

Generate PDF's with Prawn

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

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

Rails render array to pdf with prawn

I need to render my #manufacturers array to pdf, but do it only via click on some link in view...
Now i have such code
def index
#manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #manufacturers }
format.pdf { render :layout => false }
end
end
I see a lot of examples in web, but i didn't found clear and actual example... Just how simple do in a4 pdf table with my array #manufacturers ?
In addition to prawn, use the prawnto rails plugin to help with rendering the PDF as a template.
See https://github.com/prior/prawnto for the plugin and http://railscasts.com/episodes/153-pdfs-with-prawn for how to use it.
[Note: the Report gem currently only generates on letter-size paper, patch for A4 would be welcome!]
You can use the Report gem, which generates PDF using Prawn but also XLSX and CSV.
# a fake Manufacturer class - you probably have an ActiveRecord model
Manufacturer = Struct.new(:name, :gsa)
require 'report'
class ManufacturerReport < Report
table 'Manufacturers' do
head do
row 'Manufacturer report'
end
body do
rows :manufacturers
column 'Name', :name
column 'GSA?', :gsa
end
end
# you would want this so that you can pass in an array
# attr_reader :manufacturers
# def initialize(manufacturers)
# #manufacturers = manufacturers
# end
def manufacturers
[
Manufacturer.new('Ford', true),
Manufacturer.new('Fischer', false),
Manufacturer.new('Tesla', nil),
]
end
end
When you call report.pdf.path, a PDF is generating in the tmp directory:
report = ManufacturerReport.new
puts report.pdf.path #=> /tmp/185051406_Report__Pdf.pdf
puts report.xlsx.path #=> /tmp/185050541_Report__Xlsx.xlsx
You can do it in your controller like:
#manufacturers = Manufacturer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #manufacturers }
format.pdf do
report = ManufacturerReport.new(#manufacturers) # using the commented-out code
send_file report.pdf.path, :type => 'application/pdf', :disposition => 'attachment', :filename => 'ManufacturersReport.pdf'
# tmp files are periodically cleaned up by the operating system, but if you want to be extra clean you can call
# report.cleanup
# but this may remove the tmp files before apache/nginx/etc. finishes delivering the file
end
end
End result:
PDF
XLSX
Note that the XLSX has an autofilter added for you automatically.

Ruby on Rails & Prawn PDF - Create Customer List

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

Resources