简体   繁体   English

在Rails中获取PDF中重定向页面的响应

[英]Obtain the response of the redirected page in PDF in Rails

I have a button in my app which redirects to a new window by clicking on it. 我的应用程序中有一个按钮,可通过单击该按钮重定向到新窗口。 Here is the jQuery code for it: 这是它的jQuery代码:

jQ("#create_report").click(function() {
        if (jQ('#client_id').val() !== "" && 
            jQ('#report_date').val() !== "") {
            window.open( 
                "/automated_reporting/report?clientId=" + 
                jQ('#client_id').val() + "&clientName=" + 
                jQ('#client_id option:selected').html() + 
                "&selectedDate=" + jQ('#report_date').val(), 
                'Report',
                'height=700,width=750');
            jQ('#modal_report').modal('hide');
        } else {
            jQ('#create_modal_notification').showMessage(
                "error", "Provide Date and Report Type"
            );
        }
    });

In my controller, I have written nothing for redirecting or rendering it, as I have already done it in the JavaScript. 在控制器中,我没有编写任何用于重定向或呈现的内容,就像我在JavaScript中所做的一样。 The page is redirected successfully and the output is also displayed properly as required. 页面已成功重定向,并且输出也根据需要正确显示。 But what I want is to print the output of the redirected page in PDF format. 但是我想要的是以PDF格式打印重定向页面的输出。 I have tried to use the wicked_pdf gem for it by entering the following code in my controller's report action: 我试图通过在控制器的报告操作中输入以下代码来使用wicked_pdf gem:

respond_to do |format|
          format.pdf do
              render :pdf => "file_name"
          end
      end
  end

But it just gives template error report.pdf does not exist and double render error when I try something else. 但是它只给出模板错误report.pdf不存在,当我尝试其他操作时会出现双重渲染错误。

Using pry on my app this is what works. 在我的应用程序上使用撬动即可正常工作。

respond_to do |format|
    format.pdf do                          
          pdf = OrderPdf.new(@order, view_context)
                 send_data pdf.render, filename: "serial_#{@order.id}-#{@order.first_name}.pdf",
                     type: "application/pdf",
                     disposition: "inline"
          end
end

I do not have the second render. 我没有第二个渲染器。

Do you have the report.pdf.erb (I think) file? 您是否有report.pdf.erb (我认为)文件?

I'm using wicked_pdf on my app and this works (but I have the my_action.pdf.erb file on my views): 我在我的应用程序上使用了wicked_pdf,并且可以正常工作(但我的my_action.pdf.erb上有my_action.pdf.erb文件):

def my_action 
    format.pdf do
        render :pdf => slug, :show_as_html => params[:debug]
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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