vince
vince

Reputation: 2848

how to create multi page pdf from html

We need to print thousands of invoices that are in this format -

http://example.com/orders/n

where n = thousands of orders

Going through each order and then clicking on "print" is taking us a loooong time. Is there a way to create a multipage pdf from each of those URLs that we can download as one pdf so we can hit "print" once?

Upvotes: 5

Views: 3915

Answers (3)

klochner
klochner

Reputation: 8125

For invoices I like to use htmldoc - it renders a little nicer than wkhtmltopdf, but the downside is that you can't use a stylesheet.

So for htmldoc you would probably have to re-code your invoice view to use a more tabular layout with inline styles.

Upvotes: 0

David Barlow
David Barlow

Reputation: 4974

You could try a ruby script using the PDFkit gem (wraps wkhtmltopdf).

I would suggest splitting your pdf's into probably 50 to 100 pages each, don't like the thought of a 1000 page pdf in memory... probably fall over.

Example script, concats pages into one big html string with page break divs and saves to file:

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')

Upvotes: 6

Grrbrr404
Grrbrr404

Reputation: 1805

Consider to use wkhtmltopdf.

Its a very nice command line util that uses Webkit rendering engine to produce pdf pages.

Upvotes: 0

Related Questions