Cannot get wkhtmltopdf to work in rails - ruby-on-rails-3

I am trying to use wicked_pdf in my rails 3 application. I have followed all the tutorials and I am getting this error:
Failed to execute:
"/usr/local/bin/wkhtmltopdf" -q "file:////var/folders/_b/50kywsc97r95gvr9gy9nr6700000gn/T/wicked_pdf20130228-874-a51lgi.html" "/var/folders/_b/50kywsc97r95gvr9gy9nr6700000gn/T/wicked_pdf_generated_file20130228-874-1ihqg74.pdf"
I know that wkhtmltopdf exists in the right place because when I type which wkhtmltopdf I am shown the path above. I also get told it's there when I try to brew install again. Furthermore when I type wkhtmltopdf google.com google.pdf it runs fine.
My controller looks like this:
def show #shows some material
#material = Material.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => '#{#material.id}',
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf'
end
end
end
Any suggestions? Does this seem like a wkhtmltopdf issue or a wicked_pdf issue? I have bundle installed everything and bundle updated everything. I have added the wkhtmltopdf-binary gem (which shouldn't be necessary). It doesn't work on heroku either.

Sounds like there may be a few wkhtmltopdf images out there. If rails is trying to use /usr/local/bin/wkhtmltopdf, try the command /usr/local/bin/wkhtmltopdf from a terminal and see is there are any problems running that one. If you have a few copies around from different gem installs, the one that runs first in your search path may not be the one rails is trying.
Hope it helps
Bob

First validate the location of the binaries
Open a terminal prompt and type
$ which wkhtmltopdf
The return in my machine is
/usr/local/bin/wkhtmltopdf
Validate the binaries are working well
wkhtmltopdf google.com google It must generate a pdf with the google webpage
Create a class in your initializer
Look at my gist
And that's all

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

rails; wicked_pdf is wicked slow

1.9.2 & rails 3.0.9
wicked_pdf 0.8.0
wkhtmltopdf 0.10.0 rc2 # /usr/local/bin/
nginx and pow
It seems to barely work, if at all, and it's extremely slow when it does. What I've done:
after bundle install, went into rails console for simple test:
pdf = WickedPdf.new.pdf_from_string('Hello There!')
"******\"/usr/local/bin/wkhtmltopdf\" -q - -
******"
which hangs like that for a very long time, like, 30 minutes or so, but eventually comes back.
Dropped to shell to test wkhhtmltopdf directly with wkhtmltopdf google.com google.pdf and that comes back with:
Loading page (1/2)
Printing pages (2/2)
Done
Pretty quick, although I had to cntl-c out
After some digging, I added wkhtmltopdf-binary to gem file, bundle install... same results
My wicked_pdf.rb initializer:
WickedPdf.config = {
:exe_path => '/usr/local/bin/wkhtmltopdf',
:layout => 'pdf.html',
:orientation => 'Landscape',
:page_size => 'Letter'
}
Thanks in advance
It looks like your problem may be related to this issue:
https://github.com/mileszs/wicked_pdf/issues/110
Try rolling back wkhtmltopdf to version 0.9.9 and let me know if you still have issues.

Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?

Background
After reading around it seemed to me that Prawn is out and wkhtmltopdf is in. It also seems like the PDFKit and wicked_pdf gems for Rails are the new cool. So I found a screencast by Ryan on how to use PDFKit. I installed everything, tested wkhtmltopdf on the CLI with no problems, fiddled around with Rails settingsto run multiple processes so the asset pipeline works, and all seemed good, except I'm still stuck at the very end of the process (actually getting the PDF response from the server).
Question
When I request a .pdf version of my view (I'm using the PDFKit Middleware option) my browser just sits there waiting for a response, but as soon as I kill the Rails process the PDF I expected to get only then pops up in my browser window. What gives?
What I'm Using
OS: OSX 10.8.1
Rails: 3.2.8
Ruby: 1.9.3
wkhtmltopdf: 0.11.0_rc1 (although when I run wkhtmltopdf -V it says 0.10.0_rc2)
qt: 4.8.2
What I've Done
used the PDFKit middleware by loading config.middleware.use "PDFKit::Middleware" in my application.rb file.
included gem 'pdfkit' in my Gemfile and installed it with Bundler
set the .pdf mime-type in my mime_types.rb initializer with Mime::Type.register_alias "application/pdf", :pdf
added config.threadsafe! to config/environments/development.rb for multiple threads so asset pipeline doesn't conflict with PDF engine
tested wkhtmltopdf http://www.google.com google.pdf and it generated a PDF of the Google homepage as expected
tried swapping PDFKit for wicked_pdf and encountered the same problem (hanging, but when Rails process is killed the PDF renders as expected)
What it Looks Like
This is the regular html page rendered by Rails (I've blurred the client details):
This is the CLI output by Rails when I try to navigate to localhost:3000/some/path.pdf. (the app hangs while waiting for a response):
When I finally kill the Rails process with ctrl-c the PDF finally shows up in the browser as I expected to see it (CSS and HTML rendered properly, so assets seem to load fine):
Conclusions So Far
Swapping PDFKit for wicked_pdf and getting the same results seems to make me think the problem isn't with those libraries, but something to do with my development environment. But wkhtmltopdf runs fine off the command line, so that makes me think that it and QT are doing their job. The problem must be in Rails. Maybe I'm not configuring something properly?
Plea for Help
How do I determine what exactly the problem is and how do I fix it?
I'll love you if you can help me <3
Update
I've also tried using an alternative method of rendering the PDF (with .to_pdf) without the middleware option as follows (doing this I commented out config.middleware.use "PDFKit::Middleware" from my application.rb file):
respond_to do |format|
format.html
format.pdf do
html = '<html><body>This is a test.</body></html>'
#pdf = PDFKit.new(html)
send_data #pdf.to_pdf,
:filename => 'whatever.pdf',
:type => 'application/pdf',
:disposition => 'attachment'
end
end
The problem is with wkhtmltopdf itself, specifically, any version after 0.9.9. wkhtmltopdf hangs when run directly from the command-line.
Steps to correct:
brew uninstall wkhtmltopdf
cd /usr/local/Library/Formula/
git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb
brew install wkhtmltopdf
Then verify the correct version is installed wkhtmltopdf --version which should yield wkhtmltopdf 0.9.9
Citations:
https://github.com/mileszs/wicked_pdf/issues/110
http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav
Try the last version. The easy way install on MacOS:
brew install Caskroom/cask/wkhtmltopdf
The fix by scarver2 worked for me as advertised. But I needed a more recent version of wkhtmltopdf. Since the homebrew version still seems outdated (it still hangs on the command line), and since there isn't a recent precompiled binary available, I used the os x build script to compile one myself:
$ git clone git#github.com:wkhtmltopdf/wkhtmltopdf.git
$ cd wkhtmltopdf
$ ./build_osx.sh # i'm running 10.9.2
$ bin/wkhtmltopdf --version
Name:
wkhtmltopdf 0.12.1-72a9f2066fe9ffd162dec007a4d9b6a5cd63b670
$ curl www.example.com | bin/wkhtmltopdf - test.pdf # render pdf of example.com
$ open test.pdf # to confirm pdf
I'm using pdfkit 0.6.2 with Rails 3.2.17. I put the binary in /vendor and, in a pdfkit initializer, pointed to it with config.wkhtmltopdf. So far, so good.
I got the same issue. It works when I added: 'config.threadsafe!' in application.rb as the answer in the stack. Hope this help.
Mine was also hanging and opening the wkhtmltopdf icon in the dock.
I actually found the problem for me was I only had 1 unicorn worker process running. Once I added more than 1 it worked fine.
I'm running wkhtmltopdf 0.9.9 on OS X with pdfkit 0.6.2
Exact same symptoms but using WickedPdf currently. At this point, I believe the issue lies with wkhtmltopdf as well.
Unfortunately, neither of the recommendations I've been able to find in Stack/Google worked for me. Instead, I needed to combine several suggestions, including some found in this post.
Solution was:
brew uninstall wkhtmltopdf
find and delete any copies of wkhtmltopdf in /usr/bin
comment WickedPdf.config line in config/initializers
add config.threadsafe! to development.rb
remove middleware and allow 'show' action of main controller to handle pdf requests (may not be necessary)
add wkhtmltopdf-binary to gemfile
bundle
restart server
You may also need to add Mime::Type.register_alias "application/pdf", :pdf to config/initializers/mime_types.rb (for me, this causes 'warning: already initialized constant PDF')
My set-up was: Mac OSX Mountain Lion with Rails 3.2.1, Webrick, Postgres and wkhtmltopdf-binary (0.9.9.1).
Try editing config/initializer/pdfkit.rb in the following way:
PDFKit.configure do |config|
config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-i386').to_s
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"A4",
:margin_top=>"0.25in",
:margin_right=>"0.1in",
:margin_bottom=>"0.25in",
:margin_left=>"0.1in",
:disable_smart_shrinking=> false
}
end

CKeditor in Ruby on Rails

I installed Ckeditor following the instruction in this repository
I created a simple rails application adding
gem 'ckeditor'
to the Gemfile and then running bundle install from command line.
I created a scaffold with only one text field. I wanted to have a cktext_area rather than a normal text_area in the create/update action.
However, I get this error
undefined method 'cktext_area' for #<ActionView::Helpers::FormBuilder:0xb66fe5a8>
if I substitute the form text area with this
<%= javascript_include_tag :ckeditor %>
<%= f.cktext_area :name%>
I ran the installation as written in github, it seems it isn't able to find the appropriate helper. Even if I try to insert cktext_area_tag("test_area", "Ckeditor") it isn't able to find the correct helper.
I'm using Rails 3.1.3
Hey first I also got this error and then when i restarted the server everything worked fine.
Try it.Hope it gets working.

pdfkit not rendering correctly in rails 3.1

I have followed the following railscast about adding pdfkit to an application, and I am having some issues with the generation of pdfs. Here are the following things that I have done:
I downloaded wkhtmltopdf via the homebrew package manager
brew install wkhtmltopdf
Then I added the pdfkit gem to my gemfile and ran the bundle install command. I added the following to my config/application.rb file
require 'pdfkit'
...
config.middleware.use PDFKit::Middleware, :print_media_type => true
I then changed my application layout file to include all stylesheet types.
If I run rake middleware, the command works and I can see the pdfkit middleware
When I try to append pdf to the end of my routes the application just hangs and I have to exit via the command line. If I create a link to the page I want to make into a pdf, it changes all of the markup so it looks like a corrupted file. (it looks like you opened a text file into a word processor or vice versa I can provide images if that helps) If I try to make css changes in my stylesheet they do not go into effect when I view them with the link to pdf. I am assuming that this has something to do with the new asset pipeline in rails has anyone else experienced this issue?
So I was right in assuming that my error had something to do with the asset pipeline, after doing some research it looks like you need to create a new initializer and add the following code:
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}