【问题标题】:PDF is not generated in wicked_pdfwicked_pdf 中未生成 PDF
【发布时间】:2017-04-25 05:47:29
【问题描述】:

我是 Rails 新手,我想将 html 页面转换为 pdf。我为此使用 wicked_pdf gem。我的问题是应该为两种类型的用户(经理、员工)生成 pdf。对于普通员工,pdf 生成正确。但是当经理试图生成 pdf 时,他不能。我已经给出了我的代码。

这是我的控制器

  @time_entries = TimeEntry.find(params[:ids])
    @project = Project.find_by_id(params[:project_id])
    @upcoming_week_tasks = @project.issues.open
    opened_risks = Risk.where(project_identifier:     @project.identifier, status: Risk.get_open_statuses )
    closed_risks = Risk.where("project_identifier = ? and updated_at >= ? and updated_at <= ? and status in (?)", @project.identifier,    params[:wsr_start_date],params[:wsr_end_date], Risk.get_closed_statuses)
    @risks = opened_risks + closed_risks
    respond_to do |format|
      format.js do
        render :partial => "wsr_entries", :content_type => 'text/html'
      end
      format.pdf do
        render pdf: "PRR-#{@project.name}", template: "wsr/prr.html.erb"
      end
    end
  end

我使用 ajax 函数来获取控制器的参数。函数如下所示

    $("#pdf_link").click(function(e){
  e.preventDefault();
  var ids = [];
  var project_id = $("#project_id").find(":selected").attr('value');
    $(".list input[type=checkbox]:checked").each(function(){
      ids.push($(this).attr('value'));
    });

  $.ajax({
    url: '/get_prr.pdf',
    type: 'POST',
    data: {ids: ids, project_id: project_id }
  });
});

并且视图中的链接标签是

<% action_path = @managers.include?(@user) ? get_prr_path(:format => "pdf") : weekly_status_report_path(:format => "pdf")%>
  <p id="pdf_link" style="display:none;"><%= link_to "Download PDF", action_path %></p>

当普通用户点击链接时,它应该转到weekly_status_report_path,当经理点击它时,它应该转到get_prr_path。

问题是两个动作都做得很好,但没有生成 pdf

当我检查控制台时,它会给出以下报价

  Rendered wsr/_summary_of_time_entries.html.erb (6.2ms)
  Rendered wsr/prr.html.erb (8.6ms)
"***************[\"/home/likewise-open/CHENNAI/001133/.rvm/gems/ruby-    2.2.2@redmine1/bin/wkhtmltopdf\", \"-q\",  \"file:////tmp/wicked_pdf20170425-20985-1kfny53.html\", \"/tmp/wicked_pdf_generated_file20170425-20985-1ke6sll.pdf\"]***************"
  Rendered text template (0.0ms)
Sent data PRR-WSR task check.pdf (2.2ms)
Completed 200 OK in 1061ms 

我猜问题出在 ajax 函数上。

提前感谢我的英语不好:)

【问题讨论】:

    标签: jquery ruby-on-rails ajax pdf wicked-pdf


    【解决方案1】:

    尝试将format.html 添加到您的respond_to 块中。我认为wicked_pdf 期望它存在,即使您不会使用它。

    respond_to do |format|
      format.html
      format.js do
        render :partial => "wsr_entries", :content_type => 'text/html'
      end
      format.pdf do
        render pdf: "PRR-#{@project.name}", template: "wsr/prr.html.erb"
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      相关资源
      最近更新 更多