【问题标题】:Rails 3 and PDFKit, how can I convert an HTML file into a landscape orientation PDF?Rails 3 和 PDFKit,如何将 HTML 文件转换为横向 PDF?
【发布时间】:2012-01-12 21:07:06
【问题描述】:

我可以很好地将 HTML 页面转换为 PDF 文档。问题是,我不知道如何将 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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 pdfkit


    【解决方案1】:

    如果这有帮助,我正在使用 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

    【讨论】:

      【解决方案2】:

      你的 html 文件中需要一个元标记:

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

      那么 PDF 是横向的。

      【讨论】:

      • 这太完美了!在我的中间件初始化程序中,我需要将默认值保留为纵向,但这允许我逐页将其覆盖为横向。
      【解决方案3】:

      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 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-29
        • 2022-01-01
        • 1970-01-01
        • 2021-12-12
        • 2019-02-10
        • 1970-01-01
        • 2014-02-01
        • 2018-06-03
        相关资源
        最近更新 更多