Slim2pdf
Slim2pdf renders slim template with data hash and saves the results as pdf file.
Installation
Add this line to your application's Gemfile:
gem 'slim2pdf'
And then execute:
$ bundle
Or install it yourself as:
$ gem install slim2pdf
Usage
require 'slim2pdf'
Create writer object
writer = Slim2pdf::Writer.new
writer.template = 'template.slim'
writer.data = {name: 'John', surname: 'Doe'}
Set parameters in initializer
writer = Slim2pdf::Writer.new('template.slim', {name: 'John', surname: 'Doe'})
Possible actions
writer.render_to_html
writer.save_to_html('output.html')
writer.save_to_pdf('output.pdf')
Changing default wkhtmltopdf path
writer = Slim2pdf::Writer.new
writer.wkhtmltopdf_path = '/your/path/to/wkhtmltopdf'
Wkhtmltopdf params will be generated automatically.
Changing default wkhtmltopdf command with params
writer = Slim2pdf::Writer.new
writer.wkhtmltopdf_command = '/bin/some_wrapper /your/path/to/wkhtmltopdf --your --params'
Input (html) and output (pdf) files will be added automatically.
Examples
Save simple slim template without data to pdf file
require 'slim2pdf'
writer = Slim2pdf::Writer.new('simple.slim')
writer.save_to_pdf('simple.pdf')
See: doc/examples/simple
writer = Slim2pdf::Writer.new('agreement.slim')
writer.data = {date: '2014-01-14', part1: 'Google', part2: 'Microsoft'}
writer.footer_text = 'Agreement footer'
writer.save_to_pdf('agreement.pdf')
See: doc/examples/agreement
Generate bulk invoices
writer = Slim2pdf::Writer.new('invoice.slim')
buyers = ['Buyer A', 'Buyer B', 'Buyer C', 'Buyer D']
buyers.each_with_index do |buyer, index|
number = index + 1
writer.data = {
seller: 'Seller', buyer: buyer, number: "#{number}/2014",
item_name: 'Service', price: '$100', date: '2014-01-14'
}
writer.save_to_pdf("invoice-#{number}.pdf")
end
See: doc/examples/invoices
Debuging
Setting logger
require 'logger'
writer = Slim2pdf::Writer.new
writer.logger = Logger.new(STDERR)
If you use Slim2pdf with Rails, the Rails.logger will be set automatically.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request