Rails 3 and PDFKit. How to specify page size?

前端 未结 3 1483
误落风尘
误落风尘 2021-01-06 18:36

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 look

相关标签:
3条回答
  • 2021-01-06 18:43

    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')
    
    0 讨论(0)
  • 2021-01-06 18:49

    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

    0 讨论(0)
  • 2021-01-06 19:02

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

    0 讨论(0)
提交回复
热议问题