uninitialized constant Prawn::FLOAT_PRECISION error showing while using prawn - ruby-on-rails

I took the approach of Railscast episode 153 revised.
My controller is
class AdminsController < ApplicationController
def index
#examples = Example.all
respond_to do |format|
format.html
format.csv { send_data #examples.to_csv }
format.xls { send_data #examples.to_csv }
format.pdf do
pdf = DownloadPdf.new(#examples)
send_data pdf.render, filename: 'generate_table.pdf',
type: 'application/pdf', disposition: "inline"
end
end
end
end
and my download_pdf.rb file is
class DownloadPdf < Prawn::Document#make_table
require 'prawn/table'
def initialize(example)
super()
#examples = example
line_items
end
def line_items
image "#{Rails.root}/app/assets/images/logo.png"
table [[1,2],[3,4]]
end
end
I am using gems
gem 'prawn', :git => "https://github.com/prawnpdf/prawn.git", :ref => '8028ca0cd2'
gem 'prawn-table', '~> 0.1.0'
Thanx in advance for helping.

TL;DR: Update prawn gem by adding this to your Gemfile: gem 'prawn' and running bundle install.
Longer answer: You are using an old version of Prawn - that ref you are using in your Gemfile refers to somewhere in 2013. prawn-table 0.1 is newer and requires a newer version of prawn. More precisely, it's using Prawn's ::FLOAT_PRECISION constant, which was added in this 2014's commit to Prawn.

Please use below on Gemfile
gem 'prawn'
and then remove Gemfile.lock
and then
bundle install
restart server

you can use round(2) like this..
val= 456.7890999999
val.round(2)
and Ans will be 456.79

Related

Unknown format with wicked_pdf

I am trying to install wicked_pdf in order to generate pre-filled contracts between 2 users on my Rails application.
I feel like I have installed wicked_pdf properly, but I get an "ActionController::UnknownFormat" error.
What I did :
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
With or without uncommenting the 'exe...' lines (one after the other), I still get the error :
# initializers/wicked_pdf.rb
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
}
My controller:
#bookings_controller.rb
class BookingsController < ApplicationController
def show
#booking = Booking.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render pdf: "test_wicked_pdf"
end
end
authorize #booking # For Pundit
end
The render HTML is working when i go to localhost:3000/bookings/135 ...
# bookings/show.html.erb
<h1>PDF test</h1>
...but not the PDF when I comment out "# format.html" in my controller
# bookings/show.pdf.erb
<h1>PDF test</h1>
Thanks a lot in advance
I know it is a pretty old question but you can use the gem wkhtmltopdf-binary-edge instead of the regular wkhtmltopdf-binary gem. It worked for me.

"Error: PDF could not be generated!" with WickedPDF

Error:
RuntimeError in BillsController#printing
Failed to execute:
"/usr/local/bin/wkhtmltopdf" -q "file:////var/folders/j5/2wx0qdjj7kl7vbvq3m2z4rj00000gn/T/wicked_pdf20130213-41259-x9dcb5.html" "/var/folders/j5/2wx0qdjj7kl7vbvq3m2z4rj00000gn/T/wicked_pdf_generated_file20130213-41259-mg4iqp.pdf"
Error: PDF could not be generated!
BillsController:
# GET /bills
# GET /bills.json
def print
respond_to do |format|
format.html
format.pdf do
render :pdf => "rechnung_id",
:layout => "printing.pdf",
:show_as_html => params[:debug]
end
end
end
as views I created a printing.html.erb and a printing.pdf.erb - and tried both.
I've installed wkhtmltopdf as binary and as gem. When I try to use the gem (commenting out the line:
WickedPdf.config = { :exe_path => '/usr/local/bin/wkhtmltopdf'}
something seems to crash and nothing happens..
When I use the binary, I get the error displayed on top.
The versions of my gems are:
wicked_pdf (0.9.4)
and wkhtmltopdf-binary (0.9.9.1).
I was searching for help - that's what I already tried:
"bundle update" and "bundle install"
installed wkhtmltopdf in version 9.9
added "Mime::Type.register "application/pdf", :pdf"
EDIT:
If I use the terminal and enter "wkhtmltopdf www.myHomepage.info myhomepage.pdf" it works fine. "which wkhtmltopdf" gives me the path "/usr/bin/wkhtmltopdf", but if I want to use with one, it's opening "wkhtmltopdf_darwin_386" and the website freezes..
now i solved the problem.
I changed my controller method to:
def printing
#bills = Bill.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "bill_#{#bills.id}",
:wkhtmltopdf => '/usr/bin/wkhtmltopdf',
:template => '/bills/printing.pdf.erb',
:disposition => "inline"
#:save_to_file => Rails.root.join('pdf', "rechnung_#{#bills.id}.pdf")
end
end
end
and i had to remove WickedPDF as middleware in the application.rb:
require 'wicked_pdf'
config.middleware.use WickedPdf::Middleware, {}
now it's working as expected.

PDFKit, TypeError "Can't convert hash to string"

I'm using PDFKit with it's Middlware to render HTML as PDF but it keeps having a TypeError when I try to go to localhost:3000/booklets/1.pdf
can't convert Hash into String
It says the error is in BookletsController#show. This is an excerpt from my booklets_controller.rb
def show
#booklet = Booklet.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf {
html = render_to_string(:action => "show.html.erb", :formats => [:html])
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "booklet.pdf", :type => 'application/pdf')
return
}
end
end
environnment.rb
# Initialize the rails application
Ziin::Application.initialize!
Mime::Type.register "application/pdf", :pdf
excerpt from application.rb
require 'pdfkit'
config.middleware.use "PDFKit::Middleware"
PDFKit.configure do |config|
config.wkhtmltopdf = { :exe_path => '/usr/local/bin/wkhtmltopdf' }
end
The problem is the exec path you specify. I just replicated the problem on my own machine (ubuntu 12.04), and I fixed it by NOT using the gem install of wkhtmltopdf, which adds some version of wkhtmltopdf that doesn't work.
1.) Look here for how to install wkhtmltopdf (non-gem version)
https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF
2.) Remove 'wkhtmltopdf' from your gem file
3.) Find the wkhtmltopdf executable by doing 'which wkhtmltopdf'. Use the path that is returned in the exe_path declaratioin.
4.) Don't know if this next step matters, but I moved:
PDFKit.configure do |config|
config.wkhtmltopdf = { :exe_path => "#{PATH}"
end
into config/initializers/pdfkit.rb
also remove the gem by 'gem uninstall wkhtmltopdf'

cannot load such file - Nokogiri rails

I wrote a Ruby script in which I use Nokogiri.
For Rails I made this module in the lib/ directory:
require "net/http"
require "uri"
require 'nokogiri'
Module gk_CT
class CT
def getCT
uri = URI.parse("http://www.website.com")
CT = Net::HTTP.get_response(uri)
proc = Nokogiri::HTML(CT.body)
CTQ = Array.new
CTQ << proc.css('td')
end
end
In the controller I have:
require 'gk_CT'
def show
#CT= gk_CT::CT.getCT()
respond_to do |format|
format.html # show.html.erb
format.json { render json: #CT}
end
end
It always gives me the error:
cannot load such file -- nokogiri
and I have no idea why.
If the script is part of an actual Rails project, then you need to add Nokogiri to the Gemfile (with the line gem 'nokogiri'). If you're not in a Rails project or aren't using Bundler or some such weird thing, you'll still need to install the gem (gem install nokogiri).

Problem with a gem: "no such file to load"

I used 'uuidtools' gem in my controller this way:
def create
require 'uuidtools'
game = Game.new
game.permalink = Base64.encode64(UUIDTools::UUID.random_create)[0..8]
game.save
redirect_to :controller => 'home', :action => 'index'
end
I get this error about the requiring of 'uuidtools':
no such file to load -- uuidtools
(I added the gem to my gem file.)
How can I fix this?
Thanks,
Oded
Perhaps restarting the server would also had fixed the problem
Solved it.
What I did is to migrate the use of 'uuidtools' from the controller to the model:
class Game < ActiveRecord::Base
before_save :create_permalink
def create_permalink
self.permalink = Base64.encode64(UUIDTools::UUID.random_create)[0..8]
end
end
Did you run 'bundle install' to install the gem ?

Resources