• Stars
    star
    726
  • Rank 62,418 (Top 2 %)
  • Language
    Ruby
  • License
    Other
  • Created almost 14 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Uses wkhtmltoimage to create JPGs and PNGs from HTML

IMGKit

Create JPGs using plain old HTML+CSS. Uses wkhtmltoimage on the backend which renders HTML using Webkit.

Heavily based on PDFKit.

Install

IMGKit

gem install imgkit

wkhtmltoimage

  1. Use the wkhtmltoimage-binary gem (mac + linux)
gem install wkhtmltoimage-binary
  1. Install by hand: http://wkhtmltopdf.org/downloads.html
  2. Use installer: sudo imgkit --install-wkhtmltoimage install latest version into /usr/local/bin (overwrite defaults with e.g. ARCHITECTURE=amd64 TO=/home/foo/bin)

Usage

# IMGKit.new takes the HTML and any options for wkhtmltoimage
# run `wkhtmltoimage --extended-help` for a full list of options
kit = IMGKit.new(html, :quality => 50)
kit.stylesheets << '/path/to/css/file'
kit.javascripts << '/path/to/js/file'

# Get the image BLOB
img = kit.to_img

# New in 1.3!
img = kit.to_img(:jpg)      #default
img = kit.to_img(:jpeg)
img = kit.to_img(:png)

# Save the image to a file
file = kit.to_file('/path/to/save/file.jpg')
file = kit.to_file('/path/to/save/file.png')

# IMGKit.new can optionally accept a URL or a File.
# Stylesheets nor Javascripts can not be added when source is provided as a URL of File.
kit = IMGKit.new('http://google.com')
kit = IMGKit.new(File.new('/path/to/html'))

# Add any kind of option through meta tags
IMGKit.new('<html><head><meta name="imgkit-quality" content="75"...

# Format shortcuts - New in 1.3!
IMGKit.new("hello").to_jpg
IMGKit.new("hello").to_jpeg
IMGKit.new("hello").to_png

Note: Ruby's buffered I/O means that if you want to write the string data to a file or tempfile make sure to call `#flush` to ensure the contents don't get stuck in the buffer.

Configuration

wkhtmltoimage binary location

If you're on Windows or you installed wkhtmltoimage by hand to a location other than /usr/local/bin you will need to tell IMGKit where the binary is. You can configure IMGKit like so:

# config/initializers/imgkit.rb
IMGKit.configure do |config|
  config.wkhtmltoimage = '/path/to/wkhtmltoimage'
end

You could put the binaries in your bin/ folder and load the correct one depending on the platform and CPU type:

# config/initializers/imgkit.rb
IMGKit.configure do |config|
  if OS.linux? && OS.host_cpu == "x86_64"
    config.wkhtmltoimage =
      Rails.root.join("bin", "wkhtmltoimage-linux-amd64").to_s
  elsif OS.mac? && OS.host_cpu == "x86_64"
    config.wkhtmltoimage =
      Rails.root.join("bin", "wkhtmltoimage-macos-amd64").to_s
  else
    puts OS.report
    abort "You need to add a binary for wkhtmltoimage for your OS and CPU"
  end
end

To get wkhtmltoimage-linux-amd64 you can get the latest .deb (if you are targeting ubuntu) from https://github.com/wkhtmltopdf/packaging/releases

docker run -it -v $(pwd):/data ubuntu:latest /bin/bash
# or with fish
# docker run -it -v (pwd):/data ubuntu:latest /bin/bash
cd data
dpkg -x wkhtmltox_0.12.6-1.focal_amd64.deb out
exit
cp out/usr/local/bin/wkhtmltoimage bin/wkhtmltoimage-linux-amd64

And for wkhtmltoimage-macos-amd64 you can download the .pkg from https://github.com/wkhtmltopdf/packaging/releases, install it, then:

mv /usr/local/bin/wkhtmltoimage bin/wkhtmltoimage-macos-amd64

Default image format

May be set to one of IMGKit::KNOWN_FORMATS = [:jpg, :jpeg, :png]

  config.default_format = :png

Prefix for <meta> tag options (see Usage) :

May be changed from its default (imgkit-):

  config.meta_tag_prefix = 'imgkit-option'

Additional default options

Any flag accepted by wkhtmltoimage may be set thus:

  config.default_options = {
    :quality => 60
  }

For a flag which takes no parameters, use true for the value:

    'no-images' => true

For flags with multiple parameters, use an array:

    :cookie => ['my_session', '123BADBEEF456']

Overriding options

When initializing an IMGKit options may be may be set for the life time of the IMGKit object:

IMGKit.new('http://example.com/form', :post => ['my_field', 'my_unique_value'])

Heroku

get a version of wkhtmltoimage as an amd64 binary and commit it to your git repo. I like to put mine in "./bin/wkhtmltoimage-amd64"

version 0.10.0 has worked best for me

assuming its in that location you can just do:

IMGKit.configure do |config|
  config.wkhtmltoimage = Rails.root.join('bin', 'wkhtmltoimage-amd64').to_s if ENV['RACK_ENV'] == 'production'
end

If you're not using Rails just replace Rails.root with the root dir of your app.

Rails

Mime Types

register a .jpg mime type in:

#config/initializers/mime_type.rb
Mime::Type.register       "image/jpeg", :jpg

register a .png mime type in:

#config/initializers/mime_type.rb
Mime::Type.register       "image/png", :png

Controller Actions

You can respond in a controller with:

@kit = IMGKit.new(render_to_string)

format.jpg do
  send_data(@kit.to_jpg, :type => "image/jpeg", :disposition => 'inline')
end

- or -

format.png do
  send_data(@kit.to_png, :type => "image/png", :disposition => 'inline')
end

- or -

respond_to do |format|
  send_data(@kit.to_img(format.to_sym),
            :type => "image/#{format}", :disposition => 'inline')
end

This allows you to take advantage of rails page caching so you only generate the image when you need to.

--user-style-sheet workaround

To overcome the lack of support for --user-style-sheet option by wkhtmltoimage 0.10.0 rc2 as reported here http://code.google.com/p/wkhtmltopdf/issues/detail?id=387

  require 'imgkit'
  require 'restclient'
  require 'stringio'

  url = 'http://domain/path/to/stylesheet.css'
  css = StringIO.new( RestClient.get(url) )

  kit = IMGKit.new(<<EOD)
  <!DOCTYPE HTML>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>coolest converter</title>
  </head>
  <body>
    <div class="cool">image kit</div>
  </body>
  </html>
  EOD

  kit.stylesheets << css

Paperclip Example

Model:

class Model < ActiveRecord::Base
  # attr_accessible :title, :body
   has_attached_file :snapshot, :storage => :s3,
        :s3_credentials => "#{Rails.root}/config/s3.yml"
end

Controller:

def upload_image
   model = Model.find(params[:id])
   html  = render_to_string
   kit   = IMGKit.new(html)
   img   = kit.to_img(:png)
   file  = Tempfile.new(["template_#{model.id}", 'png'], 'tmp',
                         :encoding => 'ascii-8bit')
   file.write(img)
   file.flush
   model.snapshot = file
   model.save
   file.unlink
end

CarrierWave Workaround

Contributed by @ticktricktrack

  class MyClass < ActiveRecord::Base
    mount_uploader :snapshot, SnapshotUploader

    after_create :take_snapshot

    # private

    def take_snapshot
      file = Tempfile.new(["template_#{self.id.to_s}", '.jpg'], 'tmp', :encoding => 'ascii-8bit')
      file.write(IMGKit.new(self.html_body, quality: 50, width: 600).to_jpg)
      file.flush
      self.snapshot = file
      self.save
      file.unlink
    end
  end

Note on Patches/Pull Requests

  • Fork the project.
  • Setup your development environment with: gem install bundler; bundle install
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Testing

Travis.yml is configured for multiple rubies, so I would just test a 2.1.x version and let travis handle the rest.

Copyright

Copyright (c) 2010 Chris Continanza Based on work by Jared Pace See LICENSE for details.

More Repositories

1

arduino-restclient

Arduino RESTful HTTP Request Library
C++
205
star
2

node-logfmt

logfmt (key=value) logger and parser
JavaScript
151
star
3

rack-worker

Rack middleware that implements the Worker Pattern to process generic GET requests in the background and only serve them from a cache.
Ruby
103
star
4

fernet.js

Javascript implementation of Fernet symmetric encryption https://github.com/kr/fernet-spec
JavaScript
72
star
5

checkcheckit

command line checklist tool
Ruby
43
star
6

sinatra-google-auth

Drop-in google auth for Sinatra apps
Ruby
37
star
7

arduino-http-test

Test app for arduino-http library.
Ruby
9
star
8

resin-piminer

Bitcoin mining with Raspberry Pi and Resin.io
Shell
9
star
9

arduino-mqtt-example

sample node.js app that connects to Arduino to browser via CloudMQTT and Socket.io
JavaScript
8
star
10

postgres-o-matic

demo of postgres as a cloud service for Heroku
Ruby
8
star
11

node-proxy

Proxy Server(s) in Node.js and JS.Class
JavaScript
8
star
12

loopr

Loop Pedal with Arduino, Ruby, and SoX
Ruby
8
star
13

udacity-dlnd

Notes / scripts from my Udacity deep learning nanodegree
Jupyter Notebook
7
star
14

cablegator

Downloads all the Wikileaks CableGate files to a directory of your choosing
Ruby
6
star
15

oauth2-server

OAuth2 server implemented in Sinatra
Ruby
6
star
16

remote-control

node.js server that exposes a volume control for a mac as an HTTP app controlled via websockets
JavaScript
6
star
17

env.rb

Manage your ENV with ease
Ruby
5
star
18

draino

Heroku logplex drain framework for node.js
JavaScript
5
star
19

kensa-create-sinatra-production

Sinatra Add-on with all my idiosyncrasies- ready with a db, background processing, and testing
Ruby
4
star
20

ion-cannon

Multi-Protocol Load Tester
JavaScript
4
star
21

NQueens

BDD NQueens Solver for Austin on Rails presentiation
Ruby
3
star
22

cerberus-prox-frontend

A Fronted for the cerberus-proxy
JavaScript
3
star
23

neuron

UNIX process wrapper that uses etcd for configuration
Go
3
star
24

space_cowboy

Space Cowboy Rulez the Universe!
Java
3
star
25

Chaos-Composer

Ruby research program that uses event trees derived from chaotic equations to write melodies (with midi).
Ruby
3
star
26

deploying-rails-3.1-on-cedar

Talk for Austin on Rails about deploying Rails 3.1 on Cedar
Ruby
3
star
27

rack-cgi-proxy

an experiment in add-ons
Ruby
2
star
28

heartbeat-monitor

Sinatra app that functions as a simple heartbeat monitor.
Ruby
2
star
29

Bundler-Presentation

showoff slides from my rails user group presentation on the bundler
Ruby
2
star
30

simple-buffer-overflow

ruby program that brute force searches for the necessary offsets for an overflow
Ruby
2
star
31

cerberus_prox_sinatra_old

Old version fo the cerberus prox frontend for documentation
Ruby
1
star
32

test-devcenter-article

example of a complex devcenter article for use with devcenter gem
Ruby
1
star
33

resin-simple-demo

Simplest resin.io project that blinks Raspberry Pi ACT(ivity) LED
Shell
1
star
34

outage-lights-device

Heroku outage lights for Raspberry PI
JavaScript
1
star
35

arduino-EEE314-arduino

Instructions for Arduino-EEE314 class
1
star
36

jersey

Sinatra API Helpers
Ruby
1
star
37

callchain

Simple, composable pipelines for ruby
Ruby
1
star
38

anthropic-prompt-library-scores

Tests for the anthropic prompt library
Python
1
star
39

RGBreather

slowly pulsing RGB LED for desktop use as a reminder to breathe
Arduino
1
star
40

smart_env

Take those boring ENV vars and do something interesting with them.
Ruby
1
star
41

errorbucket

Python
1
star
42

env-conf

A better way to use the ENV for configuration
Ruby
1
star
43

devcenter

Gem for locally working with Heroku Dev Center
Ruby
1
star
44

.vim

My .vim directory
Vim Script
1
star
45

metrik

Metrics aggregator for logfmt-style metrics on STDOUT
JavaScript
1
star
46

heroku-buildpack-rack-nomagic

heroku buildpack for rack apps in shell
1
star
47

htmltocloud

Sinatra Service to generate PDFs and Images from html
Ruby
1
star
48

prompt-experiments

Jupyter notebooks for tracking experiments with prompting techniques
Jupyter Notebook
1
star
49

monotonic

Provides Monotonic.now to make syscalls to the monotonically increasing system clock
Ruby
1
star
50

game_of_life

Python CLI inmplemenation of game of life
Python
1
star
51

meta-prompt

ChatBot Prompt Improvement Tool
Python
1
star
52

arduino-http-switch

Arduino software that implements an HTTP server that toggles a 5V output and has a heartbeat for monitoring.
Arduino
1
star