Skip to content

Instantly share code, notes, and snippets.

@bhauff
Created April 4, 2011 19:35
Show Gist options
  • Select an option

  • Save bhauff/902256 to your computer and use it in GitHub Desktop.

Select an option

Save bhauff/902256 to your computer and use it in GitHub Desktop.
C:\data\brandonh\Dev>jruby -v
jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot(TM) Client VM 1.6.0_21) [Windows XP-x86-java]
C:\data\brandonh\Dev>jruby -S irb
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'pdfkit'
=> true
irb(main):003:0>
irb(main):004:0* class PDFKit
irb(main):005:1> def to_pdf(path=nil)
irb(main):006:2> append_stylesheets
irb(main):007:2>
irb(main):008:2* args = command(path)
irb(main):009:2> invoke = args.join(' ')
irb(main):010:2>
irb(main):011:2* puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #{invoke} $$$$$$$$$$$$$$$$$$$$$$$$$$$$"
irb(main):012:2> invoke = invoke.gsub("\"", "")
irb(main):013:2> puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #{invoke} $$$$$$$$$$$$$$$$$$$$$$$$$$$$"
irb(main):014:2> result = IO.popen(invoke, "wb+") do |pdf|
irb(main):015:3* pdf.puts(@source.to_s) if @source.html?
irb(main):016:3> pdf.close_write
irb(main):017:3> pdf.gets(nil)
irb(main):018:3> end
irb(main):019:2> result = File.read(path) if path
irb(main):020:2>
irb(main):021:2* raise "command failed: #{invoke}" if result.to_s.strip.empty?
irb(main):022:2> return result
irb(main):023:2> end
irb(main):024:1> end
=> nil
irb(main):025:0>
irb(main):026:0* html = '<html><head><title>Hello World!</title></head><p>Hello World!</p></html>'
=> "<html><head><title>Hello World!</title></head><p>Hello World!</p></html>"
irb(main):027:0> # Config for non-standard wkhtmltopdf location
irb(main):028:0* PDFKit.configure {|c| c.wkhtmltopdf = 'C:\data\brandonh\Dev\ruby\libs\wkhtmltopdf.exe'}
=> "C:\\data\\brandonh\\Dev\\ruby\\libs\\wkhtmltopdf.exe"
irb(main):029:0> kit = PDFKit.new(html, :page_size => 'Letter')
=> #<PDFKit:0x9e75f6 @source=#<PDFKit::Source:0x1d4eeb5 @source="<html><head><title>Hello World!</title></head><p>Hello
World!</p></html>">, @options={"--page-size"=>"Letter", "--margin-top"=>"0.75in", "--margin-right"=>"0.75in", "--margin-
bottom"=>"0.75in", "--margin-left"=>"0.75in", "--encoding"=>"UTF-8"}, @stylesheets=[]>
irb(main):030:0> pdf = kit.to_pdf
$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "C:\data\brandonh\Dev\ruby\libs\wkhtmltopdf.exe" "--page-size" "Letter" "--margin-top" "0.7
5in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--quiet" "-" "-
" $$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$ C:\data\brandonh\Dev\ruby\libs\wkhtmltopdf.exe --page-size Letter --margin-top 0.75in --mar
gin-right 0.75in --margin-bottom 0.75in --margin-left 0.75in --encoding UTF-8 --quiet - - $$$$$$$$$$$$$$$$$$$$$$$$$$$$
=> "%PDF-1.4\n1 0 obj\n<<\n/Title (\376\377\000H\000e\000l\000l\000o\000 \000W\000o\000r\000l\000d\000!)\n/Producer (wkh
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment