2012-01-12 125 views
6

我可以很好地将HTML页面转换为PDF文档。问题是,我不知道如何将HTML文件转换为横向PDF。有没有办法在控制器中设置它?Rails 3和PDFKit,如何将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 

回答

0

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 
+0

试过,仍然显示为肖像 – leonel 2012-01-17 18:39:01

7

在这种情况下帮助,我正在使用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 

我得到了方向标准牛逼从这里:https://github.com/pdfkit/pdfkit/issues/12

6

你在你的HTML文件需要一个meta标签:

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

然后PDF为横向。

+0

https://github.com/pdfkit/pdfkit/issues/23 – 2014-03-24 21:14:12

+0

这很完美!在我的中间件初始化程序中,我需要将默认值保留为纵向,但是这使我可以将其覆盖为逐页横向。 – FireDragon 2014-05-06 21:00:29