mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.54k stars 646 forks source link

ActionView::Template::Error: 403 Forbidden on Heroku #374

Open ahanmal opened 9 years ago

ahanmal commented 9 years ago

Everything was working fine on my Local and on my Heroku dev environment. Pushed to staging, and now I'm getting this error: ActionView::Template::Error: 403 Forbidden. Emptied the remote repo and repushed - same error.

Here is the stack trace:

 /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 358:in `open_http'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 736:in `buffer_open'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 211:in `block in open_loop'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 209:in `catch'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 209:in `open_loop'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 150:in `open_uri'
…_redirections-0.2.1/lib/open-uri/
redirections_patch.rb:  55:in `open_uri'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb: 716:in `open'
      /app/vendor/ruby-2.2.0/lib/ruby/2.2.0/
open-uri.rb:  34:in `open'
…/wicked_pdf-0.11.0/lib/wicked_pdf/
wicked_pdf_helper.rb: 111:in `read_from_uri'
…/wicked_pdf-0.11.0/lib/wicked_pdf/
wicked_pdf_helper.rb: 100:in `read_asset'
…/wicked_pdf-0.11.0/lib/wicked_pdf/
wicked_pdf_helper.rb:  40:in `block in wicked_pdf_stylesheet_link_tag'
…/wicked_pdf-0.11.0/lib/wicked_pdf/
wicked_pdf_helper.rb:  38:in `collect'
…/wicked_pdf-0.11.0/lib/wicked_pdf/
wicked_pdf_helper.rb:  38:in `wicked_pdf_stylesheet_link_tag'
….2.0/gems/actionview-4.2.0/lib/action_view/
template.rb: 145:in `block in render'
….2.0/gems/actionview-4.2.0/lib/action_view/
template.rb: 333:in `instrument'
….2.0/gems/actionview-4.2.0/lib/action_view/
template.rb: 143:in `render'
…ew-4.2.0/lib/action_view/renderer/
template_renderer.rb:  54:in `block (2 levels) in render_template'
…ew-4.2.0/lib/action_view/renderer/
abstract_renderer.rb:  39:in `block in instrument'
…ew-4.2.0/lib/action_view/renderer/
abstract_renderer.rb:  39:in `instrument'
…ew-4.2.0/lib/action_view/renderer/
template_renderer.rb:  53:in `block in render_template'
…ew-4.2.0/lib/action_view/renderer/
template_renderer.rb:  61:in `render_with_layout'
…ew-4.2.0/lib/action_view/renderer/
template_renderer.rb:  52:in `render_template'
…ew-4.2.0/lib/action_view/renderer/
template_renderer.rb:  14:in `render'
…/actionview-4.2.0/lib/action_view/renderer/
renderer.rb:  42:in `render_template'
…/actionview-4.2.0/lib/action_view/renderer/
renderer.rb:  23:in `render'
…2.0/gems/actionview-4.2.0/lib/action_view/
rendering.rb: 100:in `_render_template'
…2.0/gems/actionview-4.2.0/lib/action_view/
rendering.rb:  83:in `render_to_body'
….0/gems/wicked_pdf-0.11.0/lib/wicked_pdf/
pdf_helper.rb:  34:in `render_to_string_with_wicked_pdf'
….0/gems/wicked_pdf-0.11.0/lib/wicked_pdf/
pdf_helper.rb:  61:in `make_pdf'
….0/gems/wicked_pdf-0.11.0/lib/wicked_pdf/
pdf_helper.rb:  78:in `make_and_send_pdf'
….0/gems/wicked_pdf-0.11.0/lib/wicked_pdf/
pdf_helper.rb:  21:in `render_with_wicked_pdf'
           /app/app/controllers/
documents_controller.rb: 119:in `block (2 levels) in pdf'
           /app/app/controllers/
documents_controller.rb: 117:in `pdf'
…2.0/gems/actionview-4.2.0/lib/action_view/
rendering.rb:  30:in `process'
…uby/2.2.0/gems/omniauth-1.2.2/lib/omniauth/
strategy.rb: 186:in `call!'
…uby/2.2.0/gems/omniauth-1.2.2/lib/omniauth/
strategy.rb: 164:in `call'
…uby/2.2.0/gems/omniauth-1.2.2/lib/omniauth/
strategy.rb: 186:in `call!'
…uby/2.2.0/gems/omniauth-1.2.2/lib/omniauth/
strategy.rb: 164:in `call'
…uby/2.2.0/gems/omniauth-1.2.2/lib/omniauth/
strategy.rb: 186:in `call!'
…uby/2.2.0/gems/omniauth-1.2.2/lib/omniauth/
strategy.rb: 164:in `call'
…-rails-0.4.1/lib/jquery/fileupload/rails/
middleware.rb:  14:in `_call'
…-rails-0.4.1/lib/jquery/fileupload/rails/
middleware.rb:  10:in `call'
…dle/ruby/2.2.0/gems/warden-1.2.3/lib/warden/
manager.rb:  35:in `block in call'
…dle/ruby/2.2.0/gems/warden-1.2.3/lib/warden/
manager.rb:  34:in `catch'
…dle/ruby/2.2.0/gems/warden-1.2.3/lib/warden/
manager.rb:  34:in `call'
…dor/bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/
etag.rb:  24:in `call'
…/ruby/2.2.0/gems/rack-1.6.0/lib/rack/
conditionalget.rb:  25:in `call'
…dor/bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/
head.rb:  13:in `call'
…/2.2.0/gems/rack-1.6.0/lib/rack/session/abstract/
id.rb: 225:in `context'
…/2.2.0/gems/rack-1.6.0/lib/rack/session/abstract/
id.rb: 220:in `call'
unixmonkey commented 9 years ago

It appears this is somehow having trouble reading an asset using open-uri within wicked_pdf_stylesheet_link_tag, where it is trying to read in a stylesheet over the internet, eventually leading to https://github.com/mileszs/wicked_pdf/blob/master/lib/wicked_pdf/wicked_pdf_helper.rb#L120

What does that wicked_pdf_stylesheet_link_tag call look like in your app? Is the stylesheet on a remote host? Does it work with a regular stylesheet_link_tag?

ahanmal commented 9 years ago

This is the direct code from my app:

<!doctype html>
<html>
<head>
    <meta charset='utf-8' />
    <%= wicked_pdf_stylesheet_link_tag "documents-pdf" -%>
</head>
<body>
    <div id="pdf-header">
    <%= params[:debug].present? ? image_tag('documents/vocabulist_black.jpg') : wicked_pdf_image_tag('documents/vocabulist_black.jpg') %>
        <h2 id="pdf-title"><%= @document.title %></h2>
    </div>
    <div id="pdf-content">
        <br>
        <br>
        <table>
            <tbody>
                <% @document.words.each do |word| %>
                <tr>
                    <td>
                        <%= word %>
                    </td>
                    <td>
                        <%= @document.dictionary[word] %>
                    </td>
                </tr>
                <% end %>
            </tbody>
        </table>
    </div>
</body>
</html>

The assets are on Cloudfront on the staging server, while on the development server, they are served by rails. That might be the problem. Ideas?

unixmonkey commented 9 years ago

That is very likely the problem. Try using just regular image_tag instead of wicked_pdf_image_tag.

AdrienGiboire commented 8 years ago

Any news regarding this issue? I can't use basic stylesheet_link_tag helper as it breaks the display.

EDIT: My bad. The error was issued because of a file missing in my manifest.

krokrob commented 8 years ago

Hi there, I stay stuck on that issue for days, I really need help. Pdf conversion succeeds on local server but crash on heroku Here are my heroku logs:

ActionView::Template::Error (undefined method `find_asset' for nil:NilClass):
2016-01-22T21:14:53.472646+00:00 app[web.1]:      6:   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
2016-01-22T21:14:53.472647+00:00 app[web.1]:      7:   <meta charset='utf-8' />
2016-01-22T21:14:53.472648+00:00 app[web.1]:      8:   <%#= stylesheet_link_tag "application", media: 'all' %>
2016-01-22T21:14:53.472666+00:00 app[web.1]:      9:   <%= wicked_pdf_stylesheet_link_tag "application", media: 'all' %>
2016-01-22T21:14:53.472667+00:00 app[web.1]:     10: </head>
2016-01-22T21:14:53.472668+00:00 app[web.1]:     11: <body id="pdf_body">
2016-01-22T21:14:53.472668+00:00 app[web.1]:     12: 
2016-01-22T21:14:53.472669+00:00 app[web.1]:   app/views/bills/show.pdf.erb:9:in `_app_views_bills_show_pdf_erb__2743476161104474535_70162487021800'

My Gemfile:

gem 'wicked_pdf'
gem 'wkhtmltopdf-heroku'
gem 'wkhtmltopdf'

initializers/wicked_pdf.rb:

WickedPdf.config = {
}
ritec commented 8 years ago

I just encountered this same issue as well on Heroku, things work fine locally but the following error is thrown within my demo environment: ActionView::Template::Error: 403 Forbidden

Here is the backtrace

/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:353 in open_http
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:724 in buffer_open
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:210 in block in open_loop
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:208 in catch
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:208 in open_loop
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:149 in open_uri
/gems/open_uri_redirections-0.2.1/lib/open-uri/redirections_patch.rb:55 in open_uri
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:704 in open
/app/vendor/ruby-2.1.2/lib/ruby/2.1.0/open-uri.rb:34 in open
/gems/wicked_pdf-1.0.6/lib/wicked_pdf/wicked_pdf_helper/assets.rb:107 in read_from_uri
/gems/wicked_pdf-1.0.6/lib/wicked_pdf/wicked_pdf_helper/assets.rb:96 in read_asset
/gems/wicked_pdf-1.0.6/lib/wicked_pdf/wicked_pdf_helper/assets.rb:17 in block in wicked_pdf_stylesheet_link_tag
/gems/wicked_pdf-1.0.6/lib/wicked_pdf/wicked_pdf_helper/assets.rb:15 in collect
/gems/wicked_pdf-1.0.6/lib/wicked_pdf/wicked_pdf_helper/assets.rb:15 in wicked_pdf_stylesheet_link_tag
krokrob commented 8 years ago

My problem vanished with that config:

# Gemfile
gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
gem 'wkhtmltopdf-binary'

# config/initializers/wicked.rb
WickedPdf.config = {}
<!-- page.pdf -->
<%= wicked_pdf_stylesheet_link_tag "application", media: 'all' %>
ritec commented 8 years ago

I was able to overcome my issue by limiting the amount of styles I was loading to the bare essentials for my pdf to render properly, leaving everything else I needed behind. I then started getting a similar issue with wicked_pdf_javascript_include_tag and again solved it by limiting JS to the bare essentials and loading everything I could from CDNs with the regular javascript_include_tag instead of using local files. Works for now.

zerobatu commented 6 years ago

We had the same problem and fixed changing:

<%= wicked_pdf_stylesheet_link_tag "documents-pdf" -%> for this <%= stylesheet_link_tag(wicked_pdf_asset_base64('documents-pdf')) -%>

leom806 commented 5 years ago

Thanks @zerobatu, it helped me a lot. I would appreciate even more if you could say a little about why this alternative solved the problem, I guess it is about on how wicked_pdf request the resource but it is a guess though.