2012-01-12 49 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