如何安装Prawn for Rails 4

Pan Wangperawong

嗨,我正在尝试实现PDF收据的下载。我不确定如何将Prawn与Rails 4应用程序集成,也找不到有关如何执行此操作的任何教程。请参阅以下我已完成的操作。有人可以向我建议一些文章或一些提示。

1添加了对虾宝石并捆绑安装

2添加了控制器代码以呈现PDF

respond_to do |format|
  format.html
  format.pdf do
    pdf = OrderPdf.new(@order, view_context)
    send_data pdf.render, filename: "order_#{@order.order_number}.pdf",
                          type: "application/pdf",
                          disposition: "inline"
  end
end

3在视图中有一个link_to代码。该视图位于App> PDF

<%= link_to "Download PDF", order_path(@order, format: pdf) %>
亚伦·亨德森

我不确定您需要帮助的哪一部分,但是这是我的工作方式。在下面的代码中,我正在创建收据的pdf文件并将其存储在数据库中。输出看起来像这样-样本收据

也许有帮助。

class Omni::ReceiptWorksheet < Omni::Receipt

  def print(receipt)
    pdf = header receipt

    data = []
    data[0] = ["PO Nbr","Carton Nbr","Sku Nbr","Sku Description","S/U Open","S/U per Pack","Packs Open", "Packs Received"]

    receipt.receipt_details.each_with_index do |detail,i|
      selling_units = detail.purchase_detail.selling_units_approved - detail.purchase_detail.selling_units_received - detail.purchase_detail.selling_units_cancelled
      data[i+1] = [detail.purchase.purchase_nbr,' ', detail.sku.sku_nbr, detail.sku.display, selling_units, detail.receipt_pack_size, selling_units / detail.receipt_pack_size, ' ']
    end

    pdf.move_down 110

    pdf.table(data) do |t|
      t.style(t.row(0), :background_color => '0075C9')
      t.header = true
    end

    pdf.number_pages "page <page> of <total>", { :at => [pdf.bounds.right - 150, 0], width: 150, align: :right, page_filter: (1..50), start_count_at: 1, color: "002B82" }
    attach StringIO.new(pdf.render), "receiving_worksheet#{Date.today}.pdf", receipt
  end

  def header(receipt)
    pdf = Prawn::Document.new
    pdf.font_size = 12
    pdf.draw_text "Printed on: #{Date.today}", at: [0, 670]
    pdf.draw_text "Receiving Worksheet", at: [220, 670]
    pdf.draw_text "Page 1", at: [480, 670]

    pdf.draw_text "Receipt #: #{receipt.receipt_nbr}", at: [0, 650]
    pdf.draw_text "Receipt Date: #{Date.today}", at: [400, 650]

    pdf.draw_text "Receiving Location: #{receipt.location_display}", at: [0, 640]
    pdf.draw_text "Carrier Name: #{receipt.carrier_supplier.display}", at: [0, 620]
    pdf.draw_text "Bill of Lading: #{receipt.bill_of_lading_number}", at: [450, 620]
    pdf
  end

  def attach(file, file_name, receipt)
    attachment = Buildit::Attachment.create(
      attachable_type: "Omni::Receipt",
      attachable_id: receipt.receipt_id,
      file_name: file_name,
      mime_type: 'application/pdf',
      byte_size:  file.size,
      locale:   'en',
      is_enabled: true
    )

    Buildit::Content.create(
    contentable_type: "Buildit::Attachment",
    contentable_id:  attachment.attachment_id,
    data: file.read
    )
  end

end

以下是我用于上传和下载附件的控制器。

class ContentController < ActionController::Base

  def download

    content     = Buildit::Content.find_by_content_id(params[:file_id])
    contentable = content.contentable
    file_name   = (contentable ? contentable.file_name : 'file')

    send_data content.data, :disposition => 'attachment', :filename => file_name

  end # def download

  def upload
    begin
      content = Buildit::Content.create(
        data: params[:file].read
      )

      result = {
        success:        true,
        content_id:     content.content_id,
        file_name:      params[:file].original_filename,
        mime_type:      params[:file].content_type,
        byte_size:      params[:file].size
      }
    rescue
      result = {success: false}
    end

    render text: result.to_json, status: 200
  end # def upload

end # class ContentController

祝好运!让我知道您是否需要更具体的信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用Prawn和Rails 4在表行中添加QR码

来自分类Dev

Prawn Rails:如何使bounding_box变圆?

来自分类Dev

Prawn Rails:如何使bounding_box变圆?

来自分类Dev

Rails 4列表安装的引擎

来自分类Dev

无法在MinGW的rails 4上安装devise

来自分类Dev

使用新控制器的Rails 4- Gem prawn pdf景观渲染

来自分类Dev

如何在Rails 4应用程序的bootstrap 3中安装response.js?

来自分类Dev

如何在Rails 4应用程序上安装fabric.js?

来自分类Dev

rails:rails的条件4

来自分类Dev

rails:rails的条件4

来自分类Dev

Rails + Prawn_rails:如何将图像添加到PDF?

来自分类Dev

如何正确配置rails 4和mongoid?

来自分类Dev

Rails 4-如何使用枚举?

来自分类Dev

Rails 4,如何用行号记录文件

来自分类Dev

如何与Rails 4同时处理请求?

来自分类Dev

如何禁用Rails 4缓存的日志记录

来自分类Dev

如何缩小Rails 4中的CSS?

来自分类Dev

Rails 4:如何翻译设计视图?

来自分类Dev

Rails 4-如何访问参数

来自分类Dev

如何搜索Rails中的关联4

来自分类Dev

Rails 4 Angularjs回形针如何上传文件

来自分类Dev

如何使用turbilinks rails 4进行添加

来自分类Dev

如何在Rails 4上使用Ajax?

来自分类Dev

Rails 4-如何路由删除路径

来自分类Dev

如何使用turbilinks rails 4进行添加

来自分类Dev

Rails 4 Angularjs回形针如何上传文件

来自分类Dev

如何禁用Rails 4缓存的日志记录

来自分类Dev

Rails 4如何更改验证错误的结构

来自分类Dev

Rails 4-如何路由删除路径