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

I can well convert HTML pages to PDF documents. The problem is that I don’t know how to convert the HTML file to landscape PDF orientation. Is there any way to set this 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 
+6
source share
3 answers

In case this helps, I use PDFKit and can render the PDF to the 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

+7
source

You need the meta tag in your html file:

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

Then the PDF is in landscape orientation.

+6
source

PDFKit should accept parameters for wkhtmltopdf, so you should be able to render in landscape mode 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 
0
source

Source: https://habr.com/ru/post/905865/


All Articles