使用 wicked_pdf 无需重新加载页面即可生成许多 PDF

Generate many PDF without reload the page with wicked_pdf

我会知道如何在不重新加载页面的情况下使用 wicked_pdf 生成许多 PDF, 我的函数不呈现 PDF 视图,但它直接下载为附件

在我看来,用户 select 一个模板并下载它,但如果我想更改模板并再次下载,我必须重新加载页面

我在视图中的表单:

<%= form_tag preview_path(@document, format: :pdf), method: :get do %>
  <%= select_tag "id", options_from_collection_for_select(@templates, "id", "code"), include_blank: true %>
  <%= submit_tag "Download" %>
<% end %> 

我在控制器中的响应方式:

respond_to do |format|
          format.pdf do
            render pdf: "Labels " + @template.code,
            template: "documents/preview.pdf.erb",
            layout: 'layouts/pdf.html.erb',
            disposition: 'attachment', /* DIRECT DOWNLOAD */
            locals: {:document => @document},
            show_as_html: false,
            margin:  {   top:               @template.top_margin,                     # default 10 (mm)
              bottom:            @template.bottom_margin,
              left:              @template.left_margin,
              right:             @template.right_margin },
              paper_size: @template.paper_size
            end
          end
        end

看来你应该用另一种方式解决问题:

  1. 使用 sidekiq or delayed_job,因为您可能会挂断服务器。
  2. AJAX call 生成 pdf。
  3. 服务器响应后打开新标签页window.open(ajax_result.url);