轨道 3 + PDFKit:如何将视图转换为 PDF?
在 Windows 上的 Rails 3 应用程序中,我有以下页面,其中显示作业收据并允许用户对其进行编辑:
http://localhost:3001/jobs/45/invoice
页面底部还有一个“创建 PDF”按钮。当按下时,我的 JobsController
的 create_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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要告诉应用程序使用 pdfkit 作为中间件。
因此,您需要在初始化程序中的某个位置放置:
在此之后,如果您调用,
则应该为您生成 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:
After this if you call
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.
借助PDFKit中间件,您只能在浏览器中查看pdf格式的html页面。
如果想显示下载提示,那么您必须在操作中设置标题。
此外,如果您想将 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.
Further, If you want to save the pdf file in server then you can try this link.
Save PDF file shown by PDFKit middleware