2011-12-19 39 views
5

多页面PDF我们需要打印成千上万的发票是这种格式 -如何创建HTML

http://example.com/orders/n

其中n =上千份订单

通过每个订单去的,然后点击在“印刷”上给我们带来的是一段漫长的时光。有没有办法从每个这样的网址创建一个多页PDF,我们可以下载为一个PDF格式,因此我们可以打印一次?

+0

[PrinceXML](http://princexml.com/)是一个很棒的HTML到PDF渲染器。 – chesles 2011-12-20 07:34:28

回答

6

您可以使用PDFkit gem(wrap wkhtmltopdf)尝试使用ruby脚本。

我建议你将pdf分成大概50到100页,不喜欢在内存中使用1000页pdf的想法......可能会翻倒。

示例脚本,concats页面变成一个大HTML字符串分页符的div并保存到文件:

require 'rubygems' 
require 'open-uri' 
require 'pdfkit' 


PDFKit.configure do |config| 
    config.wkhtmltopdf = '/path/to/wkhtmltopdf' 
end 

invoice_numbers = (1..1000) #replace with actual numbers 

html = "" 

invoice_numbers.each do |n| 
    html << open("http://example.com/orders/#{n}").read + "<div style='page-break-before:always'></div>" 
end 


pdf = PDFKit.new(html, :page_size => 'Letter') 

pdf.to_file('/path/to/invoices.pdf') 
0

考虑使用wkhtmltopdf

它是一个非常漂亮的命令行实用程序,它使用Webkit渲染引擎来生成PDF页面。

0

发票我喜欢用htmldoc - 它呈现比wkhtmltopdf更好一点,但缺点是你不能使用样式表。

因此,对于htmldoc,您可能必须重新编写发票视图以使用更多表格式布局以及内联样式。