【发布时间】:2025-12-02 07:55:02
【问题描述】:
我想创建多行,这些行将从虾 pdf 上的 orderid 生成多个订单行,但只有一行订单行被填充。
class OrderPdf < Prawn::Document
def initialize(order, view)
super(top_margin: 70)
@order = order
@view = view
order_id
products
end
def logo
image_path = "#{Rails.root}/app/assets/images/logo.jpg"
end
def order_id
text "Invoice", size: 30, style: :bold, :align => :right
font("Helvetica", :size => 11, style: :bold) do
text "14077 Westheimer Rd"
text "Houston, TX, 77077"
text "Phone: 281-258-4940"
text "Fax: 281-258-4947"
text "AlphaOmegaFurniture.com"
move_up 40
text "INVOICE NO. ##{@order.id}", :align => :right
text "DATE: #{@order.OrderDate}", :align => :right
text "CUSTOMER ID: #{@order.Customer_id}", :align => :right
move_down 40
font_size 12
text "To: #{@order.Customer.fullName}"
text "Phone: #{@order.ShipToPhone}"
move_up 30
font_size 12
text "Ship To: Name: #{@order.Customer.fullName}", :indent_paragraphs => 200
text "Address: #{@order.ShipToAddress}", :indent_paragraphs => 260
text " #{@order.ShipToCity}, #{@order.ShipToState}, #{@order.ShipToZip}", :indent_paragraphs => 325
text "Phone: #{@order.ShipToPhone}", :indent_paragraphs => 260
end
move_down 20
font_size 8
data =[["SALES PERSON", "JOB", "SHIPPING METHOD", "SHIPPING TERMS", "DELIVERY DATE", "PAYMENT TERMS", "DUE DATE" ],
["#{@order.Employee.fullName}", "", "", "", "#{@order.EstimatedDeliveryDate}", "", "#{@order.OrderDate}"]
]
table(data, :position => :left, :row_colors => ["F0F0F0", "FFFFCC"] ) do
row(0).font_style = :bold
end
move_down 20
font_size 8
def products
data =["PROVIDER", "QTY", "ITEM #", "DESCRIPTION", "UNIT PRICE", "DISCOUNT", "LINE TOTAL" ]
data2 = data
OrderLine.all.each do |orderline|
data2 = ["", "", "#{orderline.Product_id}", "#{orderline.Order_id}", "#{orderline.Quantity}", "", ""]
end
data3 = [data,data2]
table(data3, :position => :left, :row_colors => ["F0F0F0", "FFFFCC"], :column_widths => [60,60,60,180,60,60,60] )
end
end
end
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
@orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
@order = Order.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = OrderPdf.new(@order, view_context)
send_data pdf.render, filename: "order_#{@order_id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby pdf prawn