1

After running into difficulty with Prawn I have decided to use PDFKit. In doing so I followed the following link:

Rails 3 + PDFKit: How to convert a view to PDF?

The problem is that my ruby on rails application is quite unusual in that it doesnt seem to follow the usual structure. In the link it says to place the following in the initializer:

require 'pdfkit'
middleware.use PDFKit::Middleware

PDFKit.configure do |config|
  config.wkhtmltopdf = 'windows_path_to_wkhtmltopdf'
end

Where would i place this? I have an app.rb file which i tried to place this in but got the following error:

undefined method `use' for [[Sass::Plugin::Rack, [], nil]]:Array

The stacktrace points to the line:

middleware.use PDFKit::Middleware
Community
  • 1
  • 1
user3385136
  • 513
  • 1
  • 7
  • 20
  • Your rails app doesn't have a `config/initializers` folder? – j-dexx Sep 16 '15 at 08:57
  • My config folder has a deploy (production.rb and staging.rb) and recipes (base.rb, nginx.rb and rbenv.rb) folder as well as app.rb, boot.rb, deploy.rb, environments.yaml, settings.yaml and warble.rb. I use Sinatra and Padrino if that helps clarify. – user3385136 Sep 16 '15 at 09:02
  • So why did you tag your question with rails? – j-dexx Sep 16 '15 at 09:17

2 Answers2

0

Non-Rails Rack apps

in config.ru

require 'pdfkit' 
use PDFKit::Middleware

From the documentation

I'm assuming you're using a config.ru setup

j-dexx
  • 10,286
  • 3
  • 23
  • 36
  • I had actually tried that but got the following error: `#!/usr/bin/env rackup # encoding: utf-8 require File.expand_path("../config/boot.rb", __FILE__) sessioned = Rack::Session::Pool.new(Padrino.application, {key: 'ib', path: '/', expire_after: 300#, #httponly: true, #secure: true }) run sessioned require 'pdfkit' use PDFKit::Middleware` I get this error `undefined local variable or method `config' for InternetBanking:Class` – user3385136 Sep 16 '15 at 09:22
  • Sorry. I was copying in the code first. The error is below. Apologies. Useless at adding comments. The first chunk is my config.ru code – user3385136 Sep 16 '15 at 09:28
  • Well, that doesn't look like it's pdfkit related, just a different error. You should either edit your question based on that or probably better as it's unrelated ask a new question about that error, with the code that causes the error. – j-dexx Sep 16 '15 at 09:31
  • Thanks. Sorry. Inherited the application and its composition is very strange and doesnt ever seem to conform to online examples so very hard to get any examples. Will do as you ask and hopefully get more luck# – user3385136 Sep 16 '15 at 09:32
0

I have an application that uses WickedPDF, and there I only need to place

gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'

And then you can simply use WickedPdf object from the application. I am not sure how your application is laid out because it does not appear to be using bundler.

Vitaly Stanchits
  • 658
  • 2
  • 7
  • 24
  • OK, as I see from the comments, this app is actually using Sinatra, not Rails. Since I never worked with Sinatra I am not sure of how to correctly bundle gems into the app.rb file. – Vitaly Stanchits Sep 16 '15 at 09:42