Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 and PDFKit. How to specify page size?

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
like image 843
leonel Avatar asked Jan 18 '26 21:01

leonel


2 Answers

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

like image 136
Paul Simpson Avatar answered Jan 21 '26 11:01

Paul Simpson


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).

like image 34
j.avi Avatar answered Jan 21 '26 10:01

j.avi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!