Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 361
leonel Avatar asked Jan 12 '12 21:01

leonel


2 Answers

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

like image 141
John Goodman Avatar answered Nov 12 '22 08:11

John Goodman


You need a meta tag in you html file:

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

Then the PDF is in landscape orientation.

like image 44
Piioo Avatar answered Nov 12 '22 09:11

Piioo