百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

wisepdf

> 编程语言
开源

Wkhtmltopdf 封装程序做得很好

278 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

Wkhtmltopdf 封装程序做得很好

wisepdf

Wkhtmltopdf wrapper done right.

Wisepdf uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let PDF take care of the hard stuff.

Wisepdf is inspired by Wicked PDF and PDFKit. PDF is optimized to use with Rails 3.1 (3.2), Ruby 1.9.2 and wkhtmltopdf 0.10.0 (and above).

Installation

First, be sure to install wkhtmltopdf. Note that versions before 0.9.0 have problems on some machines with reading/writing to streams. This plugin relies on streams to communicate with wkhtmltopdf.

More information about wkhtmltopdf could be found here.

Add this to your Gemfile:

gem 'wisepdf'

if you don't already have wkhtmltopdf installed on your machine you can get up and running quickly by adding this to your Gemfile:

gem 'wkhtmltopdf-binary'

then do:

bundle install

How does it work?

Basic Usage

class ThingsController  "file_name"
      end
    end
  end
end

Advanced Usage with all available options

class ThingsController  'file_name',
               :template                       => 'things/show.pdf.erb',
               :layout                         => 'pdf.html',                   # use 'pdf.html' for a pdf.html.erb file
               :show_as_html                   => params[:debug].present?,      # allow debuging based on url param
               :orientation                    => 'Landscape',                  # default Portrait
               :page_size                      => 'A4, Letter, ...',            # default A4
               :save_to_file                   => Rails.root.join('pdfs', "#{filename}.pdf"),
               :save_only                      => false,                        # depends on :save_to_file being set first
               :proxy                          => 'TEXT',
               :basic_auth                     => false                         # when true username & password are automatically sent from session
               :username                       => 'TEXT',
               :password                       => 'TEXT',
               :cover                          => 'URL',
               :dpi                            => 'dpi',
               :encoding                       => 'TEXT',
               :user_style_sheet               => 'URL',
               :cookie                         => ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
               :post                           => ['query QUERY_PARAM'],    # could be an array or a single string in a 'name value' format
               :redirect_delay                 => NUMBER,
               :zoom                           => FLOAT,
               :page_offset                    => NUMBER,
               :book                           => true,
               :default_header                 => true,
               :disable_javascript             => false,
               :greyscale                      => true,
               :lowquality                     => true,
               :enable_plugins                 => true,
               :disable_internal_links         => true,
               :disable_external_links         => true,
               :print_media_type               => true,
               :disable_smart_shrinking        => true,
               :use_xserver                    => true,
               :no_background                  => true,
               :margin => {:top                => SIZE,                         # default 10 (mm)
                           :bottom             => SIZE,
                           :left               => SIZE,
                           :right              => SIZE},
               :header => {:html => { :template => 'users/header.pdf.erb',  # use :template OR :url
                                      :layout   => 'pdf_plain.html',        # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
                                      :url      => 'www.example.com',
                                      :locals   => { :foo => @bar }},
                           :center             => 'TEXT',
                           :font_name          => 'NAME',
                           :font_size          => SIZE,
                           :left               => 'TEXT',
                           :right              => 'TEXT',
                           :spacing            => REAL,
                           :line               => true},
               :footer => {:html => { :template => 'shared/footer.pdf.erb', # use :template OR :url
                                      :layout   => 'pdf_plain.html',        # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
                                      :url      => 'www.example.com',
                                      :locals   => { :foo => @bar }},
                           :center             => 'TEXT',
                           :font_name          => 'NAME',
                           :font_size          => SIZE,
                           :left               => 'TEXT',
                           :right              => 'TEXT',
                           :spacing            => REAL,
                           :line               => true},
               :outline => {:outline           => true,
                            :outline_depth     => LEVEL}
      end
    end
  end
end

By default, it will render without a layout (:layout => false) and the template for the current controller and action.

Super Advanced Usage

If you need to just create a pdf and not display it:

# create a pdf from a string
pdf = Wisepdf::Writer.new.to_pdf('

Hello There! ')

# or from your controller, using views & templates and all other options as normal
pdf = render_to_string :pdf => "some_file_name"

# then save to a file
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
  file 

Styles

You must define absolute paths to CSS files, images, and javascripts; the best option is to use the wisepdf_stylesheet_tag, wisepdf_image_tag, and wisepdf_javascript_tag helpers.

Page Numbering

A bit of javascript can help you number your pages. Create a template or header/footer file with this:

    Page  of 

Anything with a class listed in "var x" above will be auto-filled at render time.

If you do not have explicit page breaks (and therefore do not have any "page" class), you can also use wkhtmltopdf's built in page number generation by setting one of the headers to "[page]":

render :pdf => 'filename', :header => { :right => '[page] of [topage]' }

Configuration

You can put your default configuration, applied to all pdf's at "configure_wisepdf.rb" initializer.

Wisepdf::Configuration.configure do |c|
  c.wkhtmltopdf = '/path/to/wkhtmltopdf'
  c.options = {
    :layout => "layout.html",
    :use_xserver => true,
    :footer => {
      :right => "#{Date.today.year}",
      :font_size => 8,
      :spacing => 8
    },
    :margin => {
      :bottom => 15
    }
  }

end

Problems with with wkhtmltopdf-0.10 and above?

If you experience problems with wkhtmltopdf-0.10 and above like getting Broken Pipe error or something similar, then you probably should compile wkhtmltopdf from source.

To do this you will have to:

  • download the latest sources from wkhtmltopdf repository https://github.com/antialize/wkhtmltopdf.
  • install needed dependencies (libXrender-devel, libXext-devel, libXft-devel, openssl-devel, development tools)
  • install QT
  • run qmake in your wkhtmltopdf sources folder
  • run make && make install

After this all should as expected.

Debugging

You can use a debug param on the URL that shows you the content of the pdf in plain html to design it faster.

First of all you must configure the render parameter :show_as_html => params[:debug] and then just use it like normally but adding debug=1 as a param:

http://localhost:3001/CONTROLLER/X.pdf?debug=1

However, the wisepdf_* helpers will use file:// paths for assets when using :show_as_html, and your browser's cross-domain safety feature will kick in, and not render them. To get around this, you can load your assets like so in your templates:

Production?

wisepdf is used at:

  • www.sdelki.ru
  • www.lienlog.com

Know other projects? Then contact me and I will add them to the list.

Note on Patches / Pull Requests

  • Fork the project.
  • 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.

Credits

Wisepsd is maintained by JetRockets.

Contributors:

  • Igor Alexandrov
  • Alexey Solilin

License

It is free software, and may be redistributed under the terms specified in the LICENSE file.

Issues· 17 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言