1

I'd like to generate PDF file, so I am trying to use PDFKit but failing.

The following error was displayed when input http://localhost:3000/users/1.pdf into my browser.

Template is missing Missing template /users.show with {:locale=>[:en], :formats=>[:pdf], :variants=>>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "c:/xxx/xxx/app/views"

Please advise me on how to avoid this error.

\config\initializers\pdfkit.rb

PDFKit.configure do |config|
  config.wkhtmltopdf = `which wkhtmltopdf`.to_s.strip
  config.default_options = {
    encoding:                "UTF-8",
    page_size:               "A4"
  }
end

\app\controllers\users_controller.rb

  def show
    @user = User.find(params[:id])

    respond_to do |format|
        format.html

        format.pdf do
            html = render_to_string template: "users.show"
            pdf = PDFKit.new(html, encoding: "UTF-8")

            send_data pdf.to_pdf,
                filename: "#{@user.id}.pdf",
                type: "application/pdf",
                disposition: "inline"
        end
    end
  end

\views\users\show.pdf.erb

  <div class="row">
    <div class="col-md-5" style="background:orange;"><%= @user.id %></div>
    <div class="col-md-7" style="background:yellow;"><%= @user.name %></div>
  </div>

I use bootstrap @import "bootstrap"; in custom.css.scss. Is that something to do with this error?

\assets\stylesheets\custom.css.scss

@import "bootstrap";

My environment is Windows

SamuraiBlue
  • 851
  • 2
  • 17
  • 44
  • Try changing `html = render_to_string template: "users.show"` to `html = render_to_string template: "show"` – Pavan Jul 18 '15 at 15:16
  • Thank you for your comment, @pavan. I tried, but it doesn't work. The similar message is displayed. `Template is missing Missing template /show with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "c:/xxx/xxx/app/views"` – SamuraiBlue Jul 19 '15 at 06:21
  • What happens when you give it like `html = render_to_string template: "users/show"` – Pavan Jul 19 '15 at 06:28
  • Another error `PDFKit::NoExecutableError in UsersController#show` is displayed. `No wkhtmltopdf executable found at >> Please install wkhtmltopdf - https://github.com/pdfkit/PDFKit/wiki/Installing-WKHTMLTOPDF`. And the following line is highlighted. `pdf = PDFKit.new(html, encoding: "UTF-8")` in users_controller.rb. – SamuraiBlue Jul 19 '15 at 06:39
  • Now try changing `config.wkhtmltopdf = `which wkhtmltopdf`.to_s.strip` to `config.wkhtmltopdf = '/path/to/wkhtmltopdf'` – Pavan Jul 19 '15 at 07:05
  • Thank you for your comment, @Pavan. I tried `config.wkhtmltopdf = 'C:\RailsInstaller\Ruby2.0.0\lib\ruby\gems\2.0.0\gems\wkhtmltopdf-0.1.2'`, then the following line is highligted. `send_data pdf.to_pdf,` in users_controller.rb. My environment is Windows. – SamuraiBlue Jul 19 '15 at 07:32
  • And the error message is as following: `Errno::EACCES in UsersController#show Permission denied - C:\RailsInstaller\Ruby2.0.0\lib\ruby\gems\2.0.0\gems\wkhtmltopdf-0.1.2 --encoding UTF-8 --page-size A4 - -` – SamuraiBlue Jul 19 '15 at 07:40
  • Although I found the following [Q&A in stackoverflow](http://stackoverflow.com/questions/5176239/rails-3-pdfkit-problem-permission-denied-errnoeacces), there is no `wkhtmltopdf.exe` in my windows PC. I wrote `gem 'wkhtmltopdf'` and `gem 'pdfkit'` in Gemfile and `bundle install`. – SamuraiBlue Jul 19 '15 at 21:07
  • I installed `wkhtmltox-0.12.2.4_msvc2013-win32.exe` manually on my windows pc and tried `config.wkhtmltopdf = 'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe'` in `\initializers\pdfkit.rb`. But it doesn't work. The following error is still appeared. `Errno::ENOENT in UsersController#show` and `No such file or directory - C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe`. It would be appreciated if you could give me any advice. – SamuraiBlue Jul 20 '15 at 01:39
  • It works after reinstalling wkhtmltopdf in `C:\wkhtmltopdf` and set `config.wkhtmltopdf = "C:\wkhtmltopdf\bin\wkhtmltopdf.exe"` in pdfkit.rb. The path may not include any space, I guess. – SamuraiBlue Jul 20 '15 at 02:06

0 Answers0