title: Generating PDF using sidekiq, wicked_pdf and haml rails 4
body: <p>So I am trying to generate a PDF in the background with Sidekiq. I have followed the directions on their wiki but I keep running into a haml formatting error. When I generate the same pdf template through normal methods I do not get any of these haml formatting errors and the pdf generates fine. I am sure I'm missing something simple but, I just can't figure out what. Code below:</p>
  
  <p>Worker Code:</p>
  
  <pre><code>class GeneratePdf
    include Sidekiq::Worker
    sidekiq_options retry: false
  
    def perform(id)
      @report = Report.find(id)
  
      av = ActionView::Base.new()
      av.view_paths = ActionController::Base.view_paths
  
      av.class_eval do
        include Rails.application.routes.url_helpers
        include ApplicationHelper
      end
  
      pdf = av.render template: 'quarterly_reports/show.pdf.haml',
      locals: {@report =&gt; @report}
  
      pdf = WickedPdf.new.pdf_from_string(
        pdf,
        header: {spacing: 10, html: {template: 'shared/qheader'}},
        footer: {spacing: 10, html: {template: 'shared/qfooter'}}
      )
      save_path = Rails.root.join('public', "PDF-{Time.now.strftime('%Y%m%d')}.pdf")
      File.open(save_path, 'wb') do |file|
        file &lt;&lt; pdf
      end
    end
  end
  </code></pre>
  
  <p>So when the job fires Sidekiq spits out a formatting error from the second line of the page which is the following:</p>
  
  <pre><code>= wicked_pdf_stylesheet_link_tag "application"
  = wicked_pdf_javascript_include_tag "application", "jquery_nested_form", "chartkick"
  </code></pre>
  
  <p>After this it shows formatting issues which almost every line of haml in the view. It does not show issues with the first line of the code which is the stylesheet link shown above. What am I doing wrong? Thanks in advance for your help!</p>
  
  <p>show.pdf.haml</p>
  
  <pre><code>= wicked_pdf_stylesheet_link_tag "application"
  = wicked_pdf_javascript_include_tag "application", "jquery_nested_form", "chartkick"
  - @page_title = "Report"
  %meta{"http-equiv" =&gt; "content-type", "content" =&gt; "text/html", "charset" =&gt; "utf-8"}
  %br
  %br
  %br
  %br
  %h2{align: "center"}
      Report
      %br
  </code></pre>
  
  <p>errors from sidekiq:</p>
  
  <pre><code>2016-03-16T16:29:06.488Z 22864 TID-ousqdbkxk WARN: SyntaxError: /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:2: syntax error, unexpected ')', expecting keyword_end
  ));}\n#{_hamlout.format_script...
   ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:3: syntax error, unexpected tCONSTANT, expecting keyword_end
  ));}\n", 0, false); @page_title = "Report"
                                           ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
  ...amlout.push_text("&lt;meta charset='utf-8' content='text/html' ...
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected tIDENTIFIER, expecting keyword_end
  ...("&lt;meta charset='utf-8' content='text/html' http-equiv='cont...
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected tIDENTIFIER, expecting keyword_end
  ...utf-8' content='text/html' http-equiv='content-type'&gt;\n&lt;br&gt;\...
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
  ...ml' http-equiv='content-type'&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;h2#{
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
  ...tp-equiv='content-type'&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;h2#{
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
  ...iv='content-type'&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;h2#{
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
  ...ntent-type'&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;h2#{
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
  ...type'&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;br&gt;\n&lt;h2#{
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:9: syntax error, unexpected '}', expecting keyword_end
  ...utes({}, nil, align: "center")}&gt;\n  Report\n  &lt;br&gt;\n&lt;/h2&gt;\n"...
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:9: syntax error, unexpected $undefined
  ... "center")}&gt;\n  Report\n  &lt;br&gt;\n&lt;/h2&gt;\n", 0, false);::Haml::...
  ...                               ^
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:9: unterminated regexp meets end of file
  /Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:9: syntax error, unexpected end-of-input, expecting keyword_end
  2016-03-16T16:29:06.489Z 22864 TID-ousqdbkxk WARN: /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:297:in `module_eval'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:297:in `compile'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:245:in `block (2 levels) in compile!'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/activesupport-4.1.10/lib/active_support/notifications.rb:161:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:339:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:244:in `block in compile!'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:232:in `synchronize'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:232:in `compile!'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:144:in `block in render'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/activesupport-4.1.10/lib/active_support/notifications.rb:161:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:339:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/template.rb:143:in `render'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/activesupport-4.1.10/lib/active_support/notifications.rb:159:in `block in instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/activesupport-4.1.10/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/activesupport-4.1.10/lib/active_support/notifications.rb:159:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/template_renderer.rb:53:in `render_template'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/template_renderer.rb:17:in `render'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/renderer.rb:42:in `render_template'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/renderer/renderer.rb:23:in `render'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/actionview-4.1.10/lib/action_view/helpers/rendering_helper.rb:32:in `render'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/haml-4.0.7/lib/haml/helpers/action_view_mods.rb:12:in `render_with_haml'
  /Users/user/Sites/cp_reports/app/workers/generate_pdf.rb:33:in `perform'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:150:in `execute_job'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:132:in `block (2 levels) in process'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:127:in `block in invoke'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/active_record.rb:6:in `call'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/retry_jobs.rb:74:in `call'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/logging.rb:11:in `block in call'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/logging.rb:30:in `with_context'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/server/logging.rb:7:in `call'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:129:in `block in invoke'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:132:in `call'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/middleware/chain.rb:132:in `invoke'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:127:in `block in process'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:166:in `stats'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:126:in `process'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:79:in `process_one'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/processor.rb:67:in `run'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/util.rb:16:in `watchdog'
  /Users/user/.rvm/gems/ruby-2.2.3/gems/sidekiq-4.0.1/lib/sidekiq/util.rb:24:in `block in safe_thread'
  </code></pre>