本文介绍了无法使用 Ruby on Rails 的 Prawn PDF 将 PDF 呈现到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rails (3.1.1) 的最新版本的 Prawn 库 (v1.0.1rc) 中创建一个 pdf 文件,当我运行我的代码时,它会将 PDF 生成到应用程序的根目录中.

I am creating a pdf file in the latest version of the Prawn library (v1.0.1rc) in Rails (3.1.1) and when I run my code it generates the PDF into the root of the application.

我不想要这个.我希望它将输出呈现到用户的浏览器窗口中,而不是将其本地保存到服务器.

I don't want this. I want it to render the output into user's browser window, without saving it locally to the server.

请告诉我如何实现这一目标.这是我的文件:

Please tell me how I can achieve this. Here are my files:

views/foo/show.pdf.erb:

<%=

require 'prawn'

pdf = Prawn::Document.new(:page_size => 'LETTER', :page_layout => :landscape, :margin => 50, :top_margin => 20, :bottom_margin => 50)

.....

render_file("foo.pdf")

%>

controllers/foo_controller:

class AuditsController < ApplicationController
  before_filter :authenticate_user!
  layout 'application'
  can_edit_on_the_spot
  respond_to :html, :xml, :js, :pdf

  def index
    @audits = Audit.all
    respond_with @audits
  end

  def show
    @audit = Audit.find(params[:id])
    respond_with @audit do |format|
      format.pdf { render :layour => false }
    end
  end

推荐答案

Gemfile

gem 'prawn'

/config/initializers/mime_types.rb

Mime::Type.register "application/pdf", :pdf

审计控制器

def show
  @audit = Audit.find(params[:id])
  respond_to do |format|
    format.html
    format.pdf do
      pdf = Prawn::Document.new
      pdf.text "This is an audit."
      # Use whatever prawn methods you need on the pdf object to generate the PDF file right here.

      send_data pdf.render, type: "application/pdf", disposition: "inline"
      # send_data renders the pdf on the client side rather than saving it on the server filesystem.
      # Inline disposition renders it in the browser rather than making it a file download.
    end
  end
end

我曾经在 Rails 3.1 之前使用 prawnto gem,但如果没有一点黑客攻击,它就无法工作了.这是在 3.1 中通过直接访问 Prawn 来实例化和显示 PDF 对象的更简洁的方法.

I used to use the prawnto gem before Rails 3.1, but it doesn't work without a bit of hacking anymore. This is a much cleaner way to instantiate and display the PDF object in 3.1 by accessing Prawn directly.

我直接从 Ryan Bates 的 Railscasts 中获得了这项技术.从那以后一直在使用它.您可以在此处查看该特定剧集.他更详细地介绍了 Prawn 的子类化和将 PDF 生成代码移出控制器.还展示了许多有用的 Prawn 方法来帮助您入门.强烈推荐.

I got this technique straight from one of Ryan Bates' Railscasts. Been using it ever since. You can view that specific episode here. He goes into much more detail about subclassing Prawn and moving the PDF generating code out of the controller. Also shows a lot of useful Prawn methods to get you started. Highly recommended.

许多剧集都是免费的,但修订后的 Prawn 剧集是只有付费订阅才能观看的剧集之一.不过,订阅费用为每月 9 美元,很快就能收回成本.

A lot of the episodes are free, but that revised Prawn episode is one of those that are only available with a paid subscription. At $9/month though, a subscription quickly pays for itself.

这篇关于无法使用 Ruby on Rails 的 Prawn PDF 将 PDF 呈现到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:36