Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 223
vince Avatar asked Dec 19 '11 07:12

vince


1 Answers

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')
like image 99
David Barlow Avatar answered Sep 28 '22 05:09

David Barlow