Advertisement
Guest User

Untitled

a guest
Mar 30th, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. def generate_invoice_pdf(order_id):
  2. order = Order.objects.get(id=order_id)
  3. order_items = OrderItem.objects.select_related("product").filter(order=order)
  4.  
  5. buffer = BytesIO()
  6. p = canvas.Canvas(buffer, pagesize=letter)
  7. width, height = letter
  8. y_position = height - 40 # Start position for writing text
  9.  
  10. # Company Name
  11. p.setFont("Helvetica-Bold", 14)
  12. p.drawString(50, y_position, "Store")
  13. y_position -= 30
  14.  
  15. # Invoice Title
  16. p.setFont("Helvetica-Bold", 16)
  17. p.drawString(230, y_position, "Invoice")
  18. p.line(50, y_position - 5, 550, y_position - 5) # Horizontal Line
  19. y_position -= 30
  20.  
  21. # Customer & Order Details
  22. p.setFont("Helvetica", 12)
  23. details = [
  24. f"Full Name: {order.full_name}",
  25. f"Contact: {order.user.profile.phone}",
  26. f"Order ID: {order.id}",
  27. f"Order Date: {order.date_ordered.strftime('%B %d, %Y')}",
  28. f"Payment Method: PayPal",
  29. f"Total Amount: ${order.amount_paid}",
  30. ]
  31. for detail in details:
  32. p.drawString(50, y_position, detail)
  33. y_position -= 20
  34.  
  35. # Shipping Address Section
  36. y_position -= 30 # Space before address
  37. p.setFont("Helvetica-Bold", 12)
  38. p.drawString(50, y_position, "Shipping Address:")
  39. y_position -= 20
  40.  
  41. p.setFont("Helvetica", 12)
  42. address_lines = [
  43. order.address_line_1,
  44. order.address_line_2 if order.address_line_2 else "",
  45. f"{order.city}, {order.state} - {order.zip_code}",
  46. order.country
  47. ]
  48. for line in address_lines:
  49. if line:
  50. p.drawString(50, y_position, line)
  51. y_position -= 20
  52.  
  53. y_position -= 30 # Space before the table
  54.  
  55. # Table Header & Data
  56. data = [["Product", "Qty", "Unit Price", "Total Price"]]
  57. for item in order_items:
  58. data.append([item.product.name, str(item.quantity), f"${item.price}", f"${item.total_price}"])
  59.  
  60. # Table Styling
  61. table = Table(data, colWidths=[200, 70, 100, 100])
  62. table.setStyle(TableStyle([
  63. ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
  64. ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
  65. ("ALIGN", (0, 0), (-1, -1), "CENTER"),
  66. ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
  67. ("BOTTOMPADDING", (0, 0), (-1, 0), 10),
  68. ("GRID", (0, 0), (-1, -1), 1, colors.black),
  69. ]))
  70.  
  71. # Dynamic Table Handling
  72. max_rows_per_page = 10 # Adjust as needed
  73. header = data[0] # Table header
  74. rows = data[1:] # Table rows
  75.  
  76. while rows:
  77. rows_to_draw = rows[:max_rows_per_page] # Get rows that fit on this page
  78. rows = rows[max_rows_per_page:] # Remaining rows for next page
  79.  
  80. # Create table for this page
  81. page_data = [header] + rows_to_draw
  82. table = Table(page_data, colWidths=[200, 70, 100, 100])
  83. table.setStyle(TableStyle([
  84. ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
  85. ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
  86. ("ALIGN", (0, 0), (-1, -1), "CENTER"),
  87. ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
  88. ("BOTTOMPADDING", (0, 0), (-1, 0), 10),
  89. ("GRID", (0, 0), (-1, -1), 1, colors.black),
  90. ]))
  91.  
  92. # Check if table fits, otherwise create a new page
  93. if y_position - (len(rows_to_draw) * 20) < 100:
  94. p.showPage() # Create a new page
  95. y_position = height - 40 # Reset position
  96.  
  97. table.wrapOn(p, width, height)
  98. table.drawOn(p, 50, y_position - (len(rows_to_draw) * 20))
  99. y_position -= (len(rows_to_draw) * 20 + 30)
  100.  
  101. # Net Total at the bottom of the last page
  102. p.setFont("Helvetica-Bold", 14)
  103. p.drawString(400, y_position, "Net Total:")
  104. p.drawString(470, y_position, f"${order.amount_paid}")
  105.  
  106. # Footer
  107. p.setFont("Helvetica", 10)
  108. p.drawString(50, 30, "Thank you for shopping with us!")
  109. p.drawString(50, 15, "For any queries, contact [email protected]")
  110.  
  111. p.showPage()
  112. p.save()
  113.  
  114. buffer.seek(0)
  115. return buffer.getvalue()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement