Wicked_PDF 仅下载 - Rails 3.1

发布于 2024-12-12 07:58:58 字数 757 浏览 2 评论 0原文

我目前运行 Rails 3.1 并使用最新版本的 Wicked_pdf 来生成 PDF。 我已经正确设置了所有内容,并且 PDF 生成得很好。

但是,我希望用户能够单击按钮来下载 pdf。目前,当单击时,浏览器会渲染 pdf 并将其显示在页面上。

<%= link_to "Download PDF", { :action => "show", :format => :pdf }, class: 'button nice red large radius', target: '_blank'%>

我的控制器。

def show
    @curric = Curric.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @curric }
      format.pdf do
        render :pdf => @curric.name + " CV",
        :margin => {:top                => 20,  
                    :bottom             => 20 }

      end
    end
  end

我已经被指向 send_file,但完全不知道如何在这种情况下使用它。

任何帮助表示赞赏。

I currently run Rails 3.1 and am using the latest version of Wicked_pdf for PDF generation.
I have set everything up correctly, and PDFs are generated just fine.

However, I want the user to be able to click a button to DOWNLOAD the pdf. At the present time, when clicked, the browser renders the pdf and displays it on the page.

<%= link_to "Download PDF", { :action => "show", :format => :pdf }, class: 'button nice red large radius', target: '_blank'%>

My Controller.

def show
    @curric = Curric.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @curric }
      format.pdf do
        render :pdf => @curric.name + " CV",
        :margin => {:top                => 20,  
                    :bottom             => 20 }

      end
    end
  end

I have been pointed towards send_file, but have absolutely no idea how to use it in this scenario.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

所有深爱都是秘密 2024-12-19 07:58:58

分解您需要设置为“附件”的配置,例如:

respond_to do |format|
  format.pdf do
    render pdf: @curric.name + " CV",
           disposition: 'attachment'
  end
end

Decomposition the config you need to set as 'attachment', example:

respond_to do |format|
  format.pdf do
    render pdf: @curric.name + " CV",
           disposition: 'attachment'
  end
end
小伙你站住 2024-12-19 07:58:58

我就是这样做的:

  def show
    @candidate = Candidate.find params[:id]

    respond_to do |format|
      format.html
      format.pdf do
        @pdf = render_to_string :pdf => @candidate.cv_filename,
            :encoding => "UTF-8"
        send_data(@pdf, :filename => @candidate.cv_filename,  :type=>"application/pdf")
      end
    end    

  end

它对我有用;-)

I am doing it that way:

  def show
    @candidate = Candidate.find params[:id]

    respond_to do |format|
      format.html
      format.pdf do
        @pdf = render_to_string :pdf => @candidate.cv_filename,
            :encoding => "UTF-8"
        send_data(@pdf, :filename => @candidate.cv_filename,  :type=>"application/pdf")
      end
    end    

  end

and it works for me ;-)

余生共白头 2024-12-19 07:58:58

让我们尝试一下:

pdf = render_to_string :pdf => @curric.name + " CV",
                       :margin => {:top     => 20,  
                                   :bottom  => 20 }
send_file pdf

Let's try:

pdf = render_to_string :pdf => @curric.name + " CV",
                       :margin => {:top     => 20,  
                                   :bottom  => 20 }
send_file pdf
黎歌 2024-12-19 07:58:58
//Download pdf generated from html template using wicket_pdf gem 
pdf = render_to_string :template => "simple/show" // here show is a show.pdf.erb inside view

send_data pdf
//Download pdf generated from html template using wicket_pdf gem 
pdf = render_to_string :template => "simple/show" // here show is a show.pdf.erb inside view

send_data pdf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文