【问题标题】:Obtain the response of the redirected page in PDF in Rails在 Rails 中以 PDF 格式获取重定向页面的响应
【发布时间】:2013-02-18 07:53:03
【问题描述】:

我的应用中有一个按钮,通过单击它会重定向到新窗口。这是它的 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"
            );
        }
    });

在我的控制器中,我没有编写任何用于重定向或呈现它的内容,因为我已经在 J​​avaScript 中完成了它。页面重定向成功,输出也按要求正确显示。但我想要的是以 PDF 格式打印重定向页面的输出。我尝试通过在控制器的报告操作中输入以下代码来使用wicked_pdf gem:

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

但是当我尝试别的东西时它只是给出模板错误report.pdf不存在和双重渲染错误。

【问题讨论】:

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


    【解决方案1】:

    在我的应用上使用 pry 是可行的。

    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
    

    我没有第二个渲染。

    【讨论】:

      【解决方案2】:

      你有report.pdf.erb(我想)文件吗?

      我在我的应用程序上使用了 wicked_pdf,这很有效(但我的视图中有 my_action.pdf.erb 文件):

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

      【讨论】:

        猜你喜欢
        • 2014-10-04
        • 2016-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        • 2021-07-24
        相关资源
        最近更新 更多