使用sidekiq,wicked_pdf和haml rails 4生成PDF

时间:2016-03-16 16:20:01

标签: ruby-on-rails ruby-on-rails-4 pdf haml wicked-pdf

所以我正在尝试使用Sidekiq在后台生成PDF。我已经按照他们的wiki上的说明进行操作,但我仍然遇到haml格式化错误。当我通过常规方法生成相同的pdf模板时,我没有得到任何这些haml格式错误,并且pdf生成正常。我确信我错过了一些简单的东西,但我无法弄清楚是什么。代码如下:

工人代码:

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 => @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 << pdf
    end
  end
end

因此,当作业触发时,Sidekiq会从页面的第二行吐出格式错误,如下所示:

= wicked_pdf_stylesheet_link_tag "application"
= wicked_pdf_javascript_include_tag "application", "jquery_nested_form", "chartkick"

在此之后,它显示格式问题,几乎每个haml行在视图中。它没有显示代码的第一行的问题,这是上面显示的样式表链接。我究竟做错了什么?在此先感谢您的帮助!

show.pdf.haml

= wicked_pdf_stylesheet_link_tag "application"
= wicked_pdf_javascript_include_tag "application", "jquery_nested_form", "chartkick"
- @page_title = "Report"
%meta{"http-equiv" => "content-type", "content" => "text/html", "charset" => "utf-8"}
%br
%br
%br
%br
%h2{align: "center"}
    Report
    %br
来自sidekiq的

错误:

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("<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
...("<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'>\n<br>\...
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
...ml' http-equiv='content-type'>\n<br>\n<br>\n<br>\n<br>\n<h2#{
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
...tp-equiv='content-type'>\n<br>\n<br>\n<br>\n<br>\n<h2#{
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
...iv='content-type'>\n<br>\n<br>\n<br>\n<br>\n<h2#{
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
...ntent-type'>\n<br>\n<br>\n<br>\n<br>\n<h2#{
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:4: syntax error, unexpected $undefined
...type'>\n<br>\n<br>\n<br>\n<br>\n<h2#{
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:9: syntax error, unexpected '}', expecting keyword_end
...utes({}, nil, align: "center")}>\n  Report\n  <br>\n</h2>\n"...
...                               ^
/Users/user/Sites/cp_reports/app/views/quarterly_reports/show.pdf.haml:9: syntax error, unexpected $undefined
... "center")}>\n  Report\n  <br>\n</h2>\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'

1 个答案:

答案 0 :(得分:0)

通过cli单步执行渲染。我注意到这个错误毕竟与haml无关。当我在渲染中调用本地人时,我的符号错了。我改变了这个:

pdf = av.render template: 'quarterly_reports/show.pdf.haml',
locals: {@report => @report}

To This:

pdf = av.render template: 'quarterly_reports/show.pdf.haml',
locals: {:@report => @report}

现在一切都按预期工作了。