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.
Related
This code I have in the products controller Index Method:
def index
#products = Product.all
respond_to do |format|
format.html
format.pdf do
pdf = PDF::Writer.new
#products.each do |product|
pdf.text product.name
end
send_data pdf.render, :filename => 'products.pdf', :type => 'application/pdf', :disposition => 'inline'
end
end
end
and in
environment.rb file
require 'pdf/writer'
Mime::Type.register 'application/pdf', :pdf
While running the program I am getting the error
undefined method `each' for "Pen":String
Aha. According to PDF::Writer's home page, "PDF::WRITER HAS BEEN DISCONTINUED AS OF APRIL 15th, 2010. PLEASE USE ITS SPIRITUAL SUCCESSOR, PRAWN INSTEAD."
I've personally been very happy using prawn to generate PDF's. But to directly answer your question, my guess is that you're probably using ruby 1.9.x, which changed the String API a bit, and PDF::Writer is meant for ruby 1.8.7.
use metaskills-pdf-writer gem. This error is happening because the each method of strings on Ruby 1.8 has changed at version 1.9. So this no longer works on new versions of ruby. This gem i mentioned will fix the issue.
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'
I may be barking up the entire wrong tree here but in case I am not, here's a go.
I am using Websnap to generate a png file of the Show page. When I go to show.png I get a blank white page and no file downloads. It was my expectation that I would get a cute little png downloaded to my machine.
And, I get nada in the log file... not even the debug statements I put in.
Respond to code:
respond_to do |format|
format.html # index.html.erb
format.png {
#html = render :action => "show.html.erb", :layout => "application.html.erb"
snap = WebSnap::Snapper.new("/dashboard/show?application=#{#application}&version=#{#jira_version}", :format => 'png')
send_data snap.to_bytes, :filename => "dashboard.png", :type => "image/png", :disposition => 'inline'}
end
environment.rb
Mime::Type.register "image/png", :png
Turns out Websnap was having an issue finding the wkhtmltoimage executable it put on the system in the first place. I really need to rewrite that Gem.
I have used the following syntax to generate a pdf file:
Prawn::Document.generate("#{RAILS_ROOT}/public/pdf/generate.pdf") do
pdf.text "hello"
end
But when I look for the file within the /public/pdf/ folder, I dont find any file.
Well my controller code for this is
def generate
respond_to do |format|
format.pdf{ render :file => "#{RAILS_ROOT}/public/pdf/generate.pdf"}
end
end
It could be something else, but the code supplied doesn't seem to work:
Prawn::Document.generate("x.pdf") do
pdf.text "Hello"
end
gives me
NameError: undefined local variable or method `pdf' for #<Prawn::Document:0x352b6e4>
from c:/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.8.4/lib/prawn/graphics/color.rb:72:in `method_missing'
from (irb):3
You should be able to see if you're getting something similar in your log file.
I think the pdf. is not needed:
Prawn::Document.generate("x.pdf") do
text "Hello"
end
ruby - 1.9.3
rails - 3.2
way 1
create new project in rails
rails new prawn_sample
you can find gem file inside project folder, add prawn gem.
gem 'prawn'
then bundle install
now, install prawnto plugin
rails plugin install git#github.com:forrest/prawnto.git
For sample data to display, let’s create a Book model with title,author and description.
rails generate scaffold book title:string author:string description:text
then
rake db:migrate
now start the server and enter sample data's to test
so,
rails s
localhost:3000/books
here enter the required amount of data's
next
Let’s get started with something simple and add a pdf version of the show action. Open up the books controller and add format.pdf
def show
#book = Book.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #book }
format.pdf { render :layout => false }
end
end
create the show.pdf.prawn file inside app/views/books. For now, lets have hello world
pdf.text "Hello World!"
visit
http://localhost:3000/books/1
http://localhost:3000/books/1.pdf
you successfully generated PDF.
Let’s make the view specific to the books.
in show.pdf.prawn
pdf.font "Helvetica"
pdf.text "Book: #{#book.title}", :size => 16, :style => :bold, :spacing => 4
pdf.text "Author: #{#book.author}", :spacing => 16
pdf.text #book.description
now you see some text with format specified above . Browse more for format you required.
way 2
Gemfile
gem 'prawn'
/config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
AuditsController
def show
#audit = Audit.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "This is an audit."
send_data pdf.render, type: "application/pdf", disposition: "inline"
end
end
end
Not sure what version of Prawn you're using, but this worked for me:
pdf = Prawn::Document.new(background: img, :page_size => "LETTER", :page_layout => :landscape)
pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")
I think you're missing the pdf.render_file line that actually creates the file.
I need to implement a custom error page in my rails application that allows me to use erb.
I've been following this tutorial (http://blog.tommilewski.net/2009/05/custom-error-pages-in-rails/) and I cannot get it to work locally (or remotely). I am running Rails 2.3.5
Here's the gist of the approach.
1) in the 'application_controller', I over ride the "render_optional_error_file(status_code)" method, and set the visibility to "protected", like this.
protected
def render_optional_error_file(status_code)
known_codes = ["404", "422", "500"]
status = interpret_status(status_code)
if known_codes.include?(status_code)
render :template => "/errors/#{status[0,3]}.html.erb", :status => status, :layout => 'errors.html.erb'
else
render :template => "/errors/unknown.html.erb", :status => status, :layout => 'errors.html.erb'
end
end
def local_request?
true
end
I also created a folder within views called errors and created the following views: 404.html.erb, 422.html.erb, 500.html.erb,unknown.html.erb and I created a new layout "errors.html.erb"
I can't seem to get it to work. I've been trying to trigger the 404 page by navigating to http://localhost:3000/foobar -- but, instead of getting the new 404.html.erb, I seem to be getting the standard apache 500 error. This happens when I try both mongrel_rails start and mongrel_rails start -e production.
I would suggest using exceptions to render such error pages, so you can use inheritance to group your error messages...
First, declare some (I usually do it in application_controller.rb)
class Error404 < StandardError; end
class PostNotFound < Error404; end
Then add code to ApplicationController to handle them
class ApplicationController < ActionController::Base
# ActionController::RoutingError works in Rails 2.x only.
# rescue_from ActionController::RoutingError, :with => :render_404
rescue_from Error404, :with => :render_404
rescue_from PostNotFound, :with => :render_post_not_found
def render_404
respond_to do |type|
type.html { render :template => "errors/error_404", :status => 404, :layout => 'error' }
type.all { render :nothing => true, :status => 404 }
end
true
end
def render_post_not_found
respond_to do |type|
type.html { render :template => "errors/shop_not_found", :status => 404, :layout => 'error' }
type.all { render :nothing => true, :status => 404 }
end
true
end
end
This renders errors/error_404 with the errors layout. Should get you started :)
And in your target_controller:
raise PostNotFound unless #post
Edit
Note for Rails 3
for a longer explanation on why ActionController::RoutingError doesn't work for rails 3:
Rails 3.0 Exception Handling.
Rails ticket 4444
"If your application relies on engines that extend your app with their
own routes, things will break because those routes will never get
fired!"
Firstly - have you deleted the file: 'public/500.html' If that file exists, it will override anything else that you try to do.
Secondly, using an explicit "rescue_from" in the controller (as explained in the other comment) - is a good option if you need to fine-tune the response to different kinds of errors.
You most likely get the 500 error because of an application error.
Have you checked the log files?
Update:
Are you certain that you are running 2.3.5 and not an older version that happens to be installed?
Mongrel should say which version you are running when it starts, otherwise it should say in the config/environment.rb file.
There are some errors in the code that might create the 500 error. I've changed that and also corrected a few other things I think you meant :)
def render_optional_error_file(status_code)
known_codes = ["404", "422", "500"]
status = interpret_status(status_code)
if known_codes.include?(status) # Here it should be status, not status_code (which is a symbol)
render :template => "errors/#{status[0,3]}", :status => status, :layout => 'errors' # No need to mention ".html.erb", you can do it, but it is not necessary since rails will guess the correct format.
else
render :template => "errors/unknown", :status => status, :layout => 'errors'
end
end
def local_request?
# This must return false instead of true so that the public viewer is called
# instead of the developer version.
false
end
Purpose of completeness for newer rails versions:
http://www.frick-web.com/en/blog/nifty_errorpages-gem
that is a little rails engine for handling your error pages. Maybe you will need it for newer projects. it is a good option to handle errors in my opinion.