Rails 3 and PDFKit, how can I convert an HTML file into a landscape orientation PDF?
Asked Answered
D

3

8

I can convert HTML pages into PDF documents well. The problem is, I don't know how to convert the HTML file into a landscape orientation PDF. Is there a way to set that in the controller?

From the controller...

def pdf_customer_shipments
  @customer = Customer.find(params[:id])
  @shipments = Shipment.where("customer_id = ? AND status = 'Open'", @customer.id)
  render :layout => 'pdf'
end
Domino answered 12/1, 2012 at 21:7 Comment(0)
T
9

In case this helps, I am using PDFKit and can render the pdf in landscape using:

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

I got the orientation part from here: https://github.com/pdfkit/pdfkit/issues/12

Tract answered 26/4, 2012 at 16:19 Comment(0)
W
6

You need a meta tag in you html file:

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

Then the PDF is in landscape orientation.

Wellordered answered 1/11, 2012 at 16:54 Comment(1)
This was perfect! In my middleware initializer I need to leave the default as Portrait, but this allows me to override it to Landscape on a page by page basis.Activator
P
0

PDFKit should accept options for wkhtmltopdf, so you should be able to render in landscape mode by using this:

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 
Platina answered 12/1, 2012 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.