简体   繁体   中英

Generate many PDF without reload the page with wicked_pdf

I would know how generate many PDF with wicked_pdf without reload the page, My function not render a PDF view but it direct download as attachement

In my view the user select a template and download it but if i want change the template and download again i must reload the page

My form in the view:

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

How i respond in my controller:

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

Looks like you should solve a problem it with another way:

  1. Use sidekiq or delayed_job , as you're can going to hang up your server.
  2. Generate the pdf from AJAX call .
  3. Open new tab after server response window.open(ajax_result.url);

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