Rails:如何将模型的详细信息打印到A4 PDF页面? [英] Rails: How to print model's details to A4 PDF page?
问题描述
在我的工作管理应用程序中,我想准备一张A4页的收据给客户.
In my Job Management application I would like to prepare an A4 page receipt to customer.
因此,我需要以某种方式将Job
模型的详细信息打印到一个A4 PDF页面上.
Thus, I need to print somehow Job
model's details to a single A4 PDF page.
Rails中是否有用于此目的的任何内置工具?
Are the any built-in tools in Rails for this purpose ?
如果没有,最好的方法是什么?
If no, what would be the best way to go ?
推荐答案
基本上有两个选项:(任何PDF创建都需要gem-Rails没有默认的PDF创建).
-
使用 Prawn 创建纯PDF,您必须使用Prawn API进行所有格式设置
Create a pure PDF using Prawn, You have to do all the formatting using the Prawn API
创建收据的HTML版本并将其转换为PDF,做到这一点的更好的方法之一是 PDFkit .使用基于Web套件的浏览器引擎.
Create a HTML version of your receipt and convert it to PDF, One of the better gems to do this is PDFkit. which uses a web-kit powered browser engine.
它们都很好用,对于一页文档,我通常使用PDFkit转换HTML,对于要处理很多页面的较大文档,我使用Prawn,因为它可以为您提供较小的文件大小并更好地处理多个页面.
They both work good, For one page documents I usually use PDFkit to convert HTML and for larger documents that are going have lots of pages I use Prawn because it gives you a smaller file size and handles multiple pages better.
我的建议是制作HTML收据并将其显示在屏幕上,并为用户提供使用 PDFkit .
My suggestion would be to make a HTML receipt and display it on the screen and give the user an option to save a PDF version using PDFkit.
Windows安装. (未经测试-Windows和我已经分开了.)
windows install. (not tested - windows and I have parted company.)
下载wkhtmltopdf的Windows安装程序: win-wkhtmltopdf
Download the windows installer for wkhtmltopdf: win-wkhtmltopdf
现在创建一个初始化文件,例如config/intializers/pdfkit_config.rb
now create an initializer file, e.g. config/intializers/pdfkit_config.rb
设置本地计算机上wkhtmltopdf的绝对路径:
in pdfkit_config.rb set the absolute path to wkhtmltopdf on your local machine:
PDFKit.configure do |config|
if RAILS_ENV == 'development'
config.wkhtmltopdf = 'D:\path\to\your\wkhtmltopdf' #this bit i'm not sure about
else
config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
end
end
对于您的生产ENV,实际上您可以在回购单中拥有wkhtmltopdf的副本,当然是unix版本. (在git添加之前,请记住chmod + x它)
for your production ENV you can actually just have a copy of wkhtmltopdf in you repo, a unix version of course. (remember to chmod +x it before you git add it)
这篇关于Rails:如何将模型的详细信息打印到A4 PDF页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!