Ruby on rails Rails 3和PDFKit,如何将HTML文件转换为横向PDF?

Ruby on rails Rails 3和PDFKit,如何将HTML文件转换为横向PDF?,ruby-on-rails,ruby,ruby-on-rails-3,pdfkit,Ruby On Rails,Ruby,Ruby On Rails 3,Pdfkit,我能很好地将HTML页面转换成PDF文档。问题是,我不知道如何将HTML文件转换为横向PDF。有没有办法在控制器中进行设置 从控制器 def pdf_customer_shipments @customer = Customer.find(params[:id]) @shipments = Shipment.where("customer_id = ? AND status = 'Open'", @customer.id) render :layout => 'pdf' end

我能很好地将HTML页面转换成PDF文档。问题是,我不知道如何将HTML文件转换为横向PDF。有没有办法在控制器中进行设置

从控制器

def pdf_customer_shipments
  @customer = Customer.find(params[:id])
  @shipments = Shipment.where("customer_id = ? AND status = 'Open'", @customer.id)
  render :layout => 'pdf'
end

PDFKit应该接受wkhtmltopdf的选项,因此您应该能够使用以下选项以横向模式进行渲染:

def pdf_customer_shipments   
  @customer = Customer.find(params[:id])   
  @shipments = Shipment.where("customer_id = ? AND status = 'Open'", @customer.id
  render :layout => 'pdf', :orientation => 'Landscape' 
end 

如果这有帮助,我正在使用PDFKit,可以使用以下方式在横向中渲染pdf:

respond_to do |format|
  format.html
  format.pdf {
    html = render_to_string(:layout => false , :action => "my_page.html.haml")
    kit = PDFKit.new(html, :orientation => 'Landscape')
    kit.stylesheets << "#{Rails.root}/public/stylesheets/views/pdf.css"
    send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf')
    return # to avoid double render call
  }
end
respond|to do |格式|
format.html
格式.pdf{
html=render_to_string(:layout=>false,:action=>“my_page.html.haml”)
kit=PDFKit.new(html,:orientation=>“横向”)
kit.stylesheets“some_name.pdf”,:type=>application/pdf)
返回#以避免双重渲染调用
}
结束

我从这里得到了方向部分:

您的html文件中需要一个元标记:

...
<head>
  <meta name="pdfkit-orientation" content="Landscape"/>
</head>
...
。。。
...

然后PDF是横向的。

这太完美了!在我的中间件初始值设定项中,我需要将默认值保留为纵向,但这允许我逐页覆盖横向。