简体   繁体   中英

PDF is not generated in wicked_pdf

I am new to rails and i want to convert a html pages to a pdf. i am using wicked_pdf gem for that.My issue is the pdf should generated for two types of users (manager,staff). for ordinary staffs the pdf is generated properly.But when a manager is trying to generate pdf the he can't.I;ve given my code.

This is my controller

  @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

i have used a ajax function to fetch params for the controller.the function is given below

    $("#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 }
  });
});

and the link tag in the view is

<% 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>

when normal user clicks the link it should go for weekly_status_report_path and when a manager clicks the it should go for get_prr_path.

the problem is both actions have done well but the pdf is not generated

when i check console it gives the following quote

  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 

i guess the issue is with the ajax function.

thanks in advance and sorry for my bad english :)

Try adding in the format.html into your respond_to block. I think wicked_pdf expects it to be there, even if you won't use it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM