使用虾和Prawnto for Rails PDF 生成

发布于 2024-08-15 02:57:12 字数 1368 浏览 7 评论 0原文

这可能更多是一个设计或使用问题,但主要问题是将 Prawn 插件与 Rails 2.3.4 一起使用并访问生成的 PDF 对象以将其渲染到文件中。

场景基本上是;

  • 带有 respond_to 块的控制器设置了
  • 一个视图,其中包含用于将文本、图形等渲染为 PDF 的代码,

它看起来像:
从客户控制器

def show    
    @customer = Customer.find(params[:id])  

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @customer }
      format.pdf  { render :layout => false  }
end

从客户视图

  pdf.text "Hello World"  
  pdf.text "Customer is #{@customer.name}"

这工作正常,生成响应 /customers/1.pdf 的 PDF 文件,正如生成 PDF 文件所预期的那样。

其他要求之一是将 pdf 呈现为文件并在服务器上存储副本。大虾提供方法: pdf.render_file {path_tofile}

所以现在,如果我将其包含在视图代码中,我当然可以将文件保存在服务器上。但我想在控制器中管理它,因为它与逻辑相关,而不是视图本身。

第一次尝试是:

def show    
   @customer = Customer.find(params[:id])
   @pdf = Prawn::Document.new()  

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @customer }
      format.pdf  { render :layout => false  }
      pdf.render_file {path_to_file}
end

从客户角度来看

  pdf.text "Hello World"  
  pdf.text "Customer is #{@customer.name}"

问题 此尝试的问题是 PDF 未呈现。我怀疑 Controller 实例变量与 Prawnto Plugin pdf 变量发生冲突。但即使更改 PDF 变量也不起作用。 有什么建议吗?

This is probably more a design or usage question but the main issue is using the Prawn plugin with Rails 2.3.4 and accessing the resulting PDF object to render it to a file.

The scenario is basically;

  • a controller with the respond_to block setup
  • a view with code for rendering the text, graphics etc to PDF

It looks like:
From Customer Controller

def show    
    @customer = Customer.find(params[:id])  

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @customer }
      format.pdf  { render :layout => false  }
end

From Customer View

  pdf.text "Hello World"  
  pdf.text "Customer is #{@customer.name}"

This works fine, producing a PDF file in response to /customers/1.pdf, as expected with a PDF file being generated.

One of the other requirements is to render the pdf to a file and store a copy on the server. Prawn provides the method:
pdf.render_file {path_tofile}

So now if I include this in the View code I can of course save the file on the server. But I'd like to manage this in the controller, as it's related to logic, not view per se.

The first attempt was :

def show    
   @customer = Customer.find(params[:id])
   @pdf = Prawn::Document.new()  

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @customer }
      format.pdf  { render :layout => false  }
      pdf.render_file {path_to_file}
end

From Customer View

  pdf.text "Hello World"  
  pdf.text "Customer is #{@customer.name}"

PROBLEM
The problem with this attempt is that the PDF is not rendered. I suspected the Controller instance variable is clashing with the Prawnto Plugin pdf variable. But even changing the PDF variable didn't work.
Any suggestions ?

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

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

发布评论

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

评论(1

°如果伤别离去 2024-08-22 02:57:12

这就是我在 Rails 应用程序中使用 prawn 的方式: http://yob.id.au/2009/05/30/prawn-and-x-accel-redirect.html - 它根本不使用 prawnto。

你可以忽略 X-Accel-Redirect 的东西,除非你使用 nginx。

或者,另一位虾核心开发人员整理了本指南: http ://wiki.github.com/sandal/prawn/using-prawn-in-rails

This is how I use prawn in my rails app: http://yob.id.au/2009/05/30/prawn-and-x-accel-redirect.html - it doesn't use prawnto at all.

You can ignore the X-Accel-Redirect stuff unless you use nginx.

Alternatively, another one of the prawn core devs has put together this guide: http://wiki.github.com/sandal/prawn/using-prawn-in-rails

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