Rails 3 and PDFKit. How to specify page size?

蹲街弑〆低调 提交于 2019-12-19 04:04:48

问题


I have been looking in the documentation but can't find the answer. How can I specify the page size of my pdf document and what are the available page sizes? I keep on looking and looking but I can't find good documentation. Please point me to a URL or let me know how can I code some page size into my PDF document.

Oh and I don't want to do that on any config file because I need to generate PDf documents of different sizes.

NOT in config file...

PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.to_s.strip
config.default_options = {
  :encoding=>"UTF-8",
  :page_size=>"A4", #or "Letter" or whatever needed
  :margin_top=>"0.25in",
  :margin_right=>"1in",
  :margin_bottom=>"0.25in",
  :margin_left=>"1in",
  :disable_smart_shrinking=>false
  }
end

回答1:


You can set the page size when creating a new PDF like this:

kit = PDFKit.new(source, :page_size => "Legal")

PDFKit uses WKHTMLTOPDF which in turn uses QPrinter. You can find the available sizes in the QPrinter documentation (there's a bunch), but its pretty safe to say that any size paper you want is available. Also, you can set a custom size if you can't find what you need.

NB: If you don't set a default option for page_size in a config somewhere AND don't supply one in your method call, PDFKit will use its internal default (Letter). See line 10 of lib/pdfkit/configuration.rb




回答2:


Since it's using wkhtmltopdf to generate the PDFs I'm assuming you can use the same options that it supports. In a wkhtmltopdf manual I found, it mentions the following site for a list of sizes:

http://doc.trolltech.com/4.6/qprinter.html#PaperSize-enum

To set the page size, you can use the :page_size option like so:

PDFKit.new(html, :page_size => 'Letter')



回答3:


Pdkit accepts custom sizes:

  PDFKit.configure do |config|
    config.wkhtmltopdf = `which wkhtmltopdf`.strip
    config.default_options = {
          :page_width => '1682',
          :page_height => '2378'
      }
  end

The sizes must be in milimeters (wkthmltopdf documentation).



来源:https://stackoverflow.com/questions/10307043/rails-3-and-pdfkit-how-to-specify-page-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!