0

I'm trying to display a PDF as basically as possible in my Rails env. Following their docs, my Gemfile has these lines:

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

I have a wicked_pdf.rb initializer (but it's an empty hash, with a comment to the effect that I shouldn't need to specify the exe_path).

In config/initializers/mime_types.rb, I have this:

Mime::Type.register "application/pdf", :pdf

In my Profiles controller, since all I want to do is have a page display a static PDF, I have this action:

def pdf_sample
  respond_to do |format|
    format.html
    format.pdf do
      render pdf: "pdf_sample" #, encoding: "ASCII-8BIT" 
    end
  end
end

And my routes.rb includes this:

get 'pdf_sample', to: 'profiles#pdf_sample', as: :pdf

When I now visit /pdf_sample it renders a blank screen, but visiting /pdf_sample.pdf generates a ActionView::WrongEncodingError, with the following message:

Your template was not saved as valid UTF-8. Please either specify UTF-8 as the encoding for your template in your text editor, or mark the template with its encoding by inserting the following as the first line of the template:

# encoding: <name of correct encoding\>.

I'm not sure how to follow these instructions though - what 'template' should I put the correct encoding in? I'm trying to render the PDF directly, not wrapped in a template.

I tried adding the commented option to the render call above (opening the sample pdf in ruby and calling #encoding.to_s on its contents returned that string), but that hasn't changed the message from saying it's 'not valid UTF-8'.

Community
  • 1
  • 1
Arepo
  • 825
  • 1
  • 9
  • 23
  • 1
    wicked pdf converts html to pdf. The template it is referring to will be the view template - in this case it should be named pdf_sample.pdf.erb (ssuming you are using ERB). – Paul Coleman Jun 02 '17 at 11:12
  • 1
    `wicked_pdf` is not the best option to render 'PDF directly, not wrapped in a template'. Check this https://stackoverflow.com/questions/26768608/how-to-render-a-pdf-in-the-browser-that-is-retrieve-via-rails-controller for simpler solutions. – yzalavin Jun 02 '17 at 12:19
  • Ah, I was wondering if it really needed that complexity. Thanks! – Arepo Jun 02 '17 at 13:44

0 Answers0