pdfkit does not style pdfs - ruby-on-rails

I have a rails 3.1 app that creates pdf documents using pdfkit, and everything works as specified, except for the fact that the generated pdfs don't have any styling. I am assuming that wkhtmltopdf doesn't have access to my stylesheets and that it is not a larger issue than that. Would anyone have a clue as to how you would allow access to these stylesheets? I have basically followed railscast #220 on the subject, however I have had to create a new initializer to get pdfkit to work with rails 3.1.
This is the initializer that I had to use to get pdfkit to work with rails 3.1
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
}
The link to the pdf looks like this:
<%= link_to 'Download PDF', load_path(#load, :format => "pdf") %>
This will give me a link to the pdf that has no styling.
In my application.rb I have configured pdfkit as such:
config.middleware.use PDFKit::Middleware, :print_media_type => true
I have also added this to my layouts/application.html.erb file:
<%= stylesheet_link_tag "application", :media => "all" %>

Stealing a couple of lines from the middleware code found at https://github.com/pdfkit/pdfkit/blob/master/lib/pdfkit/middleware.rb
You can use:
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
html.gsub!(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
My example is:
html = render_to_string #render current action to string
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
html.gsub!(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
kit = PDFKit.new(html, :print_media_type => true)

For me it was problem with installation for ubuntu. I just reinstalled from source:
# first, installing dependencies
sudo aptitude install openssl build-essential xorg libssl-dev
# for 64bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf
# for 32bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2
tar xvjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
mv wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf
And everything works now for me. So my advice is do not install wkhtmltopdf by this command sudo apt-get install wkhtmltopdf and install it from sources. Full instructions
for installation process

I know you're looking for solution that will render whole page, just reminder for googling people that there is still problem free workaround
class DocumentController < ApplicationController
def show
#document = Document.last
# ... implement your respond_to
kit = PDFKit.new(#document.content, :page_size => 'Letter')
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/pdf.css"
send_data kit.to_pdf, :filename => "#{#document.title}.pdf", :type => 'application/pdf'
end
end
now the pdf.css must be css, so teoretically if you need to load sass load it from pre-compiled public/assets/

I ran into this problem as well, and it appears that when the asset pipeline was added in Rails 3.1, pdfkit has a problem with the stylesheet links. See the GitHub issue about this problem.
I ended up switching to wicked_pdf and am really happy with it. They've solved this problem and it works nicely on Rails 3.2.x (haven't tried 3.1.x).

I have used gem 'wicked_pdf' and its helpers to include CSS into pages. Internally that helpers just read all CSS files and include into the page itself. So if you prefer to use PdfKit try to investigate how to include non-inline stylesheets.

I have successfully run PDFKit on Rails 3.1. I have used a different setup though.
At first I had the same problem you did but that was because stylesheet_link_tag has a default set to media => "screen"; specifying explicitely media => "all" fixed it.

Related

Rails 6.1.4 Wicked PDF

I am attempting to implement Wicked PDF (https://github.com/mileszs/wicked_pdf/) in a Rails 6.1.4 project and am continually getting the following error messages on the console.
My configuration is as simple as it gets. The controller looks like:
class Api::V1::ProductsController < Api::V1::BaseController
def datasheet
#product = Product.active.friendly.find params[:id]
respond_to do |format|
format.pdf {
render "products/datasheet", pdf: "datasheet-#{#product.name}"
}
end
end
end
and the datasheet.haml file looks like:
%html
%head
%body
= "This and that"
The file encoding for the datasheet.haml is UTF-8, but this shouldn't matter because the text contains no special characters.
The Gemfile has both wicked_pdf and wkhtmltopdf-binary.
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
I have executed the command rails generate wicked_pdf and it did generate the wicked_pdf.rb initializer, as expected.
I have tried putting Mime::Type.register "application/pdf", :pdf into the mime_types.rb file, but expect that this wouldn't matter because I am running Rails 6.
The response generated is:
We have another application running Rails 6.1.4 using wicked_pdf in, "ahem...", the exact same way successfully. Obviously something is different, I just cannot find out what...
Any suggestions on what I am overlooking? Your help is much appreciated!

How to install Wicked PDF gem in rails 3.0.0

Right now I am using Rails 3.0.0 version. I want to install Wicked pdf gem. I don't know how to install that gem. Please tell me that step by step procedure.
It's really easy!
Add this to Gem file!
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary', '~> 0.12.3'
Run Bundle install and run rails generate wicked_pdf
In config/initializers/mime_types.rb add this (or Uncomment)
WickedPdf.config = {
exe_path: '/usr/local/bin/wkhtmltopdf'
}
In your controller (show action)
def show
respond_to do |format|
format.pdf do
render :pdf => "file_name.pdf", :template => 'file_directory/file_name.html.erb', :encoding => 'utf-8'
end
format.html
end end
Create a erb.html file and put your pdf code in it.
In view you have to use the route controller#show for your link_to tag!
Good Luck.
use the command gem install wicked_pdf .See the documentation here. http://rubygems.org/gems/wicked_pdf & http://rubydoc.info/gems/wicked_pdf/0.7.9/frames
Reminder to restart you server after changing you config/initializers/mime_types.rb file. Other than that Afsanefda's answer worked for me.

wicked_pdf Error: PDF could not be generated

Gemfile
gem "wicked_pdf"
gem "wkhtmltopdf-binary"
the error:
RuntimeError in CarsController#show
Failed to execute:
/usr/bin/wkhtmltopdf --print-media-type -q - -
Error: PDF could not be generated!
Rails.root: /u/apps/zeepauto/autozeep_update
cars_controller
def show
#class_showcar = true
#class_admin = true
#car = Car.find(params[:id])
#search = Car.search(params[:search])
#cars_see_special = Car.where(:special => "1").order('rand()').limit(3)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #car }
format.pdf do
render :pdf => "#{#car.carname.name}",
:print_media_type => true
end
end
end
show.html.erb
<p class="show_links"><%= link_to url_for(request.params.merge(:format => :pdf)) do %>
<%= image_tag('/images/printversion.png', :alt => 'Download') %>
</p>
wicked_pdf.erb
# config/initializers/wicked_pdf.rb
WickedPdf.config = {
# :exe_path => '/var/lib/gems/1.8/bin/wkhtmltopdf'
:exe_path => '/usr/bin/wkhtmltopdf'
}
I had the same problem. The solution was to add wkhtmltopdf-binary to the gem file and run bundle install.
gem "wicked_pdf"
gem "wkhtmltopdf-binary"
I had wkhtmltopdf-binary already in gemfile, but as this was working on my local computer and not on server, I left this error for the server support team to care off.. they have checked the path to wkhtmltopdf, they tried to convert a simple html to pdf and it worked.. so they tried to run a bundle update command and after this the pdf converting worked fine on server too. I had a gem path changed and I guess this was the problem. I posted my solution in case someone else will have this problem too.
For Alpine 3.9+ the wkhtmltopdf binary is available, however I was getting either a blank PDF or "Failed to load document" error - despite working fine locally on MacOSX. It turns out that you need to include fonts explicitly for alpine builds (at least)
Controller action
def show
respond_to do |format|
format.html do
render 'pdfs/templates/my_template.html.erb'
end
format.pdf do
render(
pdf: "file_name",
template: 'pdfs/templates/my_template.html.erb',
disposition: 'inline'
)
end
end
end
The above worked locally on MacOSX machine but on a server based on ruby alpine image, as below, it failed with failed to load document
Dockerfile
FROM ruby:2.6.3-alpine3.10
....
# add wkhtmltopdf for use with wicked_pdf gem
RUN apk --no-cache add wkhtmltopdf
...
even a more basic example failed with a blank pdf
respond_to do |format|
format.pdf do
pdf = WickedPdf.new.pdf_from_string('TESTING 123')
send_data(
pdf,
filename: "file_name.pdf",
type: 'application/pdf',
disposition: 'inline'
)
end
end
Solution
Dockerfile
FROM ruby:2.6.3-alpine3.10
....
# add wkhtmltopdf for use with wicked_pdf gem
RUN apk --no-cache add \
ttf-ubuntu-font-family \
wkhtmltopdf
...
Ideally Alpine would include a basic font with the wkhtmltopdf package, however until such time I found this to be a useful source of info and/or good for adding a mutistage Docker file.
https://github.com/madnight/docker-alpine-wkhtmltopdf/blob/master/Dockerfile
NOTE:
lack of an explicit font package in alpine may also cause PDF conversion using libreoffice to fail too. We found corrupt PDFs when converted from docx files in particular.
I had the same problem. I had wkhtmltopdf-binary installed and bundle update didn't help neither. Here is what helped me:
The important thing is that I run this on Alpine Linux and it does not seem to be supported by gem wkhtmltopdf_binary_gem https://github.com/zakird/wkhtmltopdf_binary_gem/issues/53
I installed separately wkhtmltopdf into the system: apk add wkhtmltopdf
And then edited the initializer to include the binary path:
# config/initializers/wicked_pdf.rb
require "wicked_pdf"
WickedPdf.config = {
exe_path: ENV["WKHTMLTOPDF_BIN"]
}
I'm facing the same issue, it works fine on local machine but when deployed on the server it raises an error:
Error: PDF could not be generated!.
In my case, there are some dependencies missing on the server. Use the below command on the server to install the dependencies.
sudo apt-get install libfontconfig1 libxrender1
I have the same problem when using Ubuntu 20.04.
I solve the problem by using wkhtmltopdf-binary version 0.12.6.1.
If you are facing this in the docker container, most probably, you are using the Alpine Linux. In that case, wkhtmltopdf-binary is not compatible with the Alpine Linux. So, add these gems instead:
gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
gem 'wkhtmltopdf-binary-edge-alpine', '~> 0.12.5.0'
wkhtmltopdf-binary-edge-alpine is for the Alpine Linux. Everything should work just fine, hopefully😂

Rails 3.1 issue with javascript_include_tag in application.html.erb

I've created a clean and simple Rails 3.1 application called demo1. It contains two controllers - hello and goodbye. I get the following error when I try to load http://localhost:3000/say/hello:
ActionView::Template::Error (
(in /home/me/dev/ruby/demo1/app/assets/javascripts/say.js.coffee)):
3: <head>
4: <title>Demo1</title>
5: <%= stylesheet_link_tag "application" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
app/views/layouts/application.html.erb:6:in
`_app_views_layouts_application_html_erb___558576499_89622220'
The problematic line of application.html.erb is:
<%= javascript_include_tag "application" %>
When I created the application using Rails 3.0 this particular line was:
<%= javascript_include_tag :defaults %>
And this worked fine. In fact, when I change the application.html.erb to use :defaults everything works, but I want to use the new features of Rails 3.1.
I can't seem to turn up anything on Google around this, I guess because Rails 3.1 has just been released.
By the way, I'm following the first chapter in the Agile Web Development with Rails (4th edition) Updated for Rails 3.1 book.
Some environmental info that may help in answering this question:
$ cat /etc/issue
Ubuntu 10.04.2 LTS \n \l
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
$ rails -v
Rails 3.1.0
Contents of the say.js.coffee file:
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Ok, I've figured out what the issue was, and so I'll answer my own question.
The problem was the app/assets/javascripts/application.js file contained commented out code. However, one of the commented out lines was as follows:
//= require_tree .
When I delete this line everything works fine.
Hope this helps. If someone can provide some insight as to why the underscore was causing the issue that would be great.
I just ran into this issue when starting a new RoR 3.2.1 app. The problem is that you are missing a JS runtime, which is because the line
# gem 'therubyracer'
in your Gemfile has that # in front of it. God only knows why they ship Rails with that line commented, because any tutorial that uses generate scaffold or similar will result in the asset compilation process tripping up over the generated coffeescript file.
The solution is to uncomment that line. Installing another JS runtime may also solve the problem. See e.g. ExecJS and could not find a JavaScript runtime.
The official ruby on rails guide says:
Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an execjs error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the therubyracer gem to Gemfile in a commented line for new apps and you can uncomment if you need it. therubyrhino is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at ExecJS.
Simply delete the 2 lines from application.js
//= require turbolinks
//= require_tree
I had the same problem running rails 3.1.1
Once I reinstated the following code in my Gemfile, my issue disappeared:
group :assets do
gem 'sass-rails', '~> 3.1.4'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
This was placed in my Gemfile when initially setting up the project, i.e. using "rails new myapp".
Note that the in my template file I include:
<%= javascript_include_tag :application %>
Hope that helps
If you're on Windows and you used RailsInstaller to get your development going, you need to use that .bat file included with RailsInstaller to run your server. Also you can't use any CMD hook like conemu to start the server, unfortunately.
For the benefit of future Googlers, I went down a rabbit hole trying to install therubyracer until I stumbled upon this related post:
ExecJS::RuntimeError in rails 3.2.8 engine with javascript_include_tag
The link mentioned explains that ExecJS is configured incorrectly for Windows 8 out-of-the-box. In particular, the workaround described by pottsk did the trick for me.
https://github.com/sstephenson/execjs/issues/81#issuecomment-9892952
It involves changing the way that the cscript executable is run by ExecJS in %rubyinstall%\gems\2.0.0\gems\execjs-2.0.1\lib\execjs\runtimes.rb:
# JScript = ExternalRuntime.new(
# :name => "JScript",
# :command => "cscript //E:jscript //Nologo //U",
# :runner_path => ExecJS.root + "/support/jscript_runner.js",
# :encoding => 'UTF-16LE' # CScript with //U returns UTF-16LE
# )
JScript = ExternalRuntime.new(
:name => "JScript",
:command => "cscript //E:jscript //Nologo",
:runner_path => ExecJS.root + "/support/jscript_runner.js",
:encoding => 'UTF-8' # CScript with //U returns UTF-16LE
)
It goes without saying that this is not a permanent solution but it does get me up and running until the patch is incorporated.
Removing //= require_tree will just stop your javascript files from being loaded into rails. It will probably stop the error, but probably not what you'd like.
However, in my case, I had some .js.coffee files with incorrect indenting. Once I fixed this (by deleting them), it worked.
Another thing you can try which worked for me is to add the json (and perhaps also the yajl-ruby) gem to your Gemfile. I don't really understand why they weren't already installed before because:
rails depends on actionpack
which depends on sprockets
which depends on json and tilt
and tilt depends on yajl-ruby
Yet, somehow, it seems many of the dependent gems were not installed!?
In case of using linux(Ubuntu linux like mine), install javascript runtime. The best is issue the below command to install,
apt-get install nodejs
or else, for each and every new creation of rails project, add
gem 'therubyracer' to your gemfile and run bundle install
devise_scope :user do
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
If your routes.rb has this above line just delete it and run. This is what corrected my problem.
modify generator file (application.html.erb.tt) as the following:
<%= javascript_include_tag "application", "data-turbolinks-track" true %>
<%= javascript_include_tag :default, "data-turbolinks-track" true %>
Just REMOVE the following line (Line no. 6) from generator file (application.html.erb) :
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
please update me if there is any drawback to remove this line
Thanks
I have this issue in Rails 4, and if I switch it to 'defaults' rather than 'application', it works, just as the OP says. But this is in an external Gem that I'd rather not edit. I have found that if I force my Gemfile to use version 1.8.0 of coffee-script-source, do a bundle install, and start the server, then everything works fine.
Adding the gem 'therubyracer' fixed the issue.
I was getting the error only after adding external js file through <%= javascript_include_tag %>

Rails 3.1 ckeditor

So I just tried to install ckeditor in rails, however it doesn't look like its working.
Here is what I did
added these lines to my gemfile
gem "ckeditor", "~> 3.6.0"
gem "paperclip"
Then bundled installed and ran
rails generate ckeditor:install
rails generate ckeditor:models --orm=active_record
Added this file tom config/application.rb
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
And then I tried out this code:
<%= javascript_include_tag :ckeditor %>
cktext_area_tag("test_area", "Ckeditor is the best")
cktext_area_tag("content", "Ckeditor", :input_html => {:cols => 10, :rows => 20}, :toolbar => 'Easy')
However, all I am getting is two textareas that do not have any editing ability. They look like normal textareas and all I can do is erase and add text.
What am I doing wrong?
This works like a charm, just tested it on rails 3.1.rc6. Also be cautious of the gem you are using. As of the moment of this post, the oficial gem was not working and was waiting for a pull request, so be sure to use fxposter's version of the gem in your gemfile.
https://github.com/fxposter/rails_3_1_with_ckeditor_and_carrierwave
I had the same setup as you and the same issue. With jalagrange's example app as a comparison to mine, I eventually found the issue to be in development.rb. I had to remove this line:
config.assets.debug = true
It worked for me with after that.
I had a similar issue when I was setting it up a while ago. I gave up on the gem eventually. The problem was getting ckeditor to behave in the asset pipeline, but I had mixed results depending on the browser (of course IE was the problem). Here is what worked for me:
Download ckeditor package from their site and drop in to public/ckeditor.
Then, directly include the javascript files.
<%= javascript_include_tag "/ckeditor/ckeditor" %>
<%= javascript_include_tag "/ckeditor/adapters/jquery" %>
Not exactly elegant, but it worked and haven't had to touch it since.
STEP 1: Add gem 'paperclip' and gem "ckeditor" in your gemfile.
STEP 2: Bundle Install.
STEP 3: rails generate ckeditor:install --orm=active_record --backend=paperclip
STEP 4: Place config.autoload_paths += %W(#{config.root}/app/models/ckeditor) in application.rb
STEP 5: Place mount Ckeditor::Engine => "/ckeditor" if not present already and run db:migrate
STEP 6: Open application.html.erb and place this <%= javascript_include_tag 'ckeditor/ckeditor.js' %> in header.
STEP 7: Place this in footer(above the body tag) in application.html.erb
<script type="text/javascript">$(document).ready(function() {
if ($('textarea').length > 0) {
var data = $('textarea');
$.each(data, function(i) {
CKEDITOR.replace(data[i].id);
});
}
});</script>
STEP 8: Restart the WEBrick SERVER.
That's it.

Resources