2012-01-12 15 views
6

Posso convertire bene le pagine HTML in documenti PDF. Il problema è che non so come convertire il file HTML in un PDF con orientamento orizzontale. C'è un modo per impostarlo nel controller?Rails 3 e PDFKit, come posso convertire un file HTML in un PDF con orientamento orizzontale?

Dal controller ...

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

risposta

0

PDFKit dovrebbe accettare opzioni per wkhtmltopdf, così si dovrebbe essere in grado di rendere in modalità orizzontale utilizzando questo:

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

provato, mostrando ancora come ritratto – leonel

7

Nel caso in cui questo aiuta, Sto usando PDFKit e posso rendere il pdf in orizzontale usando:

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 

Ho ottenuto l'orientamento t da qui: https://github.com/pdfkit/pdfkit/issues/12

6

È necessario un meta tag in voi file html:

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

Poi il PDF è in orientamento orizzontale.

+0

https://github.com/pdfkit/pdfkit/issues/23 –

+0

Questo era perfetto! Nel mio inizializzatore del middleware ho bisogno di lasciare il default come Portrait, ma questo mi permette di sovrascriverlo su Landscape su una pagina per pagina. – FireDragon

Problemi correlati