Rails 3 和 PDFKit,如何将 HTML 文件转换为横向 PDF?

发布于 2024-12-26 17:10:33 字数 302 浏览 5 评论 0原文

我可以很好地将 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2025-01-02 17:10:33

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

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

温柔戏命师 2025-01-02 17:10:33

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

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

那么 PDF 就是横向的。

You need a meta tag in you html file:

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

Then the PDF is in landscape orientation.

饭团 2025-01-02 17:10:33

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 

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文