轨道 3 + PDFKit:如何将视图转换为 PDF?

发布于 2024-10-19 22:42:05 字数 1175 浏览 2 评论 0原文

在 Windows 上的 Rails 3 应用程序中,我有以下页面,其中显示作业收据并允许用户对其进行编辑:

http://localhost:3001/jobs/45/invoice

页面底部还有一个“创建 PDF”按钮。当按下时,我的 JobsControllercreate_pdf_invoice 被调用:

def create_pdf_invoice
    job = Job.find(params[:id])

    kit = PDFKit.new("<h1>Hello</h1><p>This is PDF!!!</p>", :page_size => "A4")
    file = kit.to_file("my_file_name.pdf")

    redirect_to(:action => 'index')
  end
end

所有这些工作正常,即 PDF 已创建!

我的问题是如何打印发票本身而不是这个静态文本(就像我在 http://localhost:3001/jobs/45/invoice 页面上按“打印”)?


更新

我尝试按照建议将

require 'pdfkit'

config.middleware.use PDFKit::Middleware

放入 config/application.rb

服务器正常启动,但是当我转到

http://localhost:3001/jobs/45/invoice.pdf

Ruby 时崩溃:

在此处输入图像描述

我使用:

ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Rails 3.0.1
rake, version 0.8.7
pdfkit (0.5.0)

有什么想法吗?

In my Rails 3 application on Windows I have the following page which shows a job receipt and lets user to edit it:

http://localhost:3001/jobs/45/invoice

I have also a "Create PDF" button in the bottom of the page. When pressed, create_pdf_invoice of my JobsController is called:

def create_pdf_invoice
    job = Job.find(params[:id])

    kit = PDFKit.new("<h1>Hello</h1><p>This is PDF!!!</p>", :page_size => "A4")
    file = kit.to_file("my_file_name.pdf")

    redirect_to(:action => 'index')
  end
end

All this works fine, i.e. the PDF is created!

My question is how can I print the invoice itself rather than this static text (like if I press "Print" on the http://localhost:3001/jobs/45/invoice page) ?


UPDATE

I tried to put

require 'pdfkit'

and

config.middleware.use PDFKit::Middleware

in config/application.rb as suggested here.

The server starts normally, but when I go to

http://localhost:3001/jobs/45/invoice.pdf

Ruby crashes:

enter image description here

I use:

ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Rails 3.0.1
rake, version 0.8.7
pdfkit (0.5.0)

Any ideas ?

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

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

发布评论

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

评论(2

故事与诗 2024-10-26 22:42:05

首先,您需要告诉应用程序使用 pdfkit 作为中间件。

因此,您需要在初始化程序中的某个位置放置:

# PDFKit
require 'pdfkit'
middleware.use PDFKit::Middleware

PDFKit.configure do |config|
  config.wkhtmltopdf = 'windows_path_to_wkhtmltopdf'
end

在此之后,如果您调用,

http://localhost:3001/jobs/45/invoice.pdf

则应该为您生成 pdf。

PDFkit是一个中间件,可以拦截pdf格式并相应地渲染页面。

如果需要,您还可以通过正则表达式或字符串限制配置中的 pdfable 路由。

只是需要注意的是,如果页面中有图像,则必须使用绝对路径调用它们。

我们还发现 pdfkit 0.5.0 存在一些问题,但 0.4.6 版本一切正常,这与路径有关,所以也许它可以解决您的问题。

first you need to tell the application to use pdfkit as a middleware.

So somewhere in an initializer you need to put:

# PDFKit
require 'pdfkit'
middleware.use PDFKit::Middleware

PDFKit.configure do |config|
  config.wkhtmltopdf = 'windows_path_to_wkhtmltopdf'
end

After this if you call

http://localhost:3001/jobs/45/invoice.pdf

a pdf should be generated for you.

PDFkit is a middleware that intercepts the pdf format rendering the page accordingly.

If you want you can also restrict pdfable routes in the config through regexes or string.

Just a caveat, if you have images in the page they must be called with an absolute path.

We are also finding some problems with pdfkit 0.5.0 but things are working fine with version 0.4.6 it is something to do with paths so maybe it can solve your issues.

痕至 2024-10-26 22:42:05

借助PDFKit中间件,您只能在浏览器中查看pdf格式的html页面。
如果想显示下载提示,那么您必须在操作中设置标题。

headers["Content-Disposition"] = "attachment; filename=export"

此外,如果您想将 pdf 文件保存在服务器中,那么您可以尝试此链接。

保存 PDFKit 中间件显示的 PDF 文件

With the help of PDFKit middle-ware you can only view the html page in pdf format in the browser.
If want to show the download prompt then you have to set the header in your action.

headers["Content-Disposition"] = "attachment; filename=export"

Further, If you want to save the pdf file in server then you can try this link.

Save PDF file shown by PDFKit middleware

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