85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
from datetime import datetime
|
|
from io import BytesIO
|
|
|
|
from fastapi import APIRouter
|
|
from fastapi.responses import Response
|
|
from reportlab.lib import colors
|
|
from reportlab.lib.enums import TA_LEFT
|
|
from reportlab.lib.pagesizes import mm
|
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
from reportlab.pdfbase import pdfmetrics
|
|
from reportlab.pdfbase.ttfonts import TTFont
|
|
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
|
|
|
|
from app import mongo
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/pdf/deal/{deal_id}", tags=[""])
|
|
async def deal_shipping_pdf(deal_id: int):
|
|
deal = await mongo.deals_collection.find_one({"id": deal_id}, {"_id": False})
|
|
deal = (await mongo.additional_deals_data(deal, full=True))[0]
|
|
|
|
buffer = BytesIO()
|
|
pdfmetrics.registerFont(TTFont('Arial', 'assets/arial.ttf'))
|
|
pdfmetrics.registerFont(TTFont('Arial Bold', 'assets/arial_bold.ttf'))
|
|
|
|
document = SimpleDocTemplate(
|
|
buffer,
|
|
pagesize=(75 * mm, 120 * mm),
|
|
leftMargin=5 * mm, rightMargin=5 * mm,
|
|
topMargin=5 * mm, bottomMargin=5 * mm
|
|
)
|
|
|
|
styles = getSampleStyleSheet()
|
|
title_style = styles['Heading1']
|
|
title_style.fontName = 'Arial Bold'
|
|
title_style.fontSize = 12
|
|
title_style.alignment = 1
|
|
|
|
normal_style = styles['Normal']
|
|
normal_style.fontName = 'Arial'
|
|
normal_style.fontSize = 8
|
|
|
|
elements = [
|
|
Paragraph("Упаковочный лист", title_style),
|
|
Paragraph("<br/>", normal_style)
|
|
]
|
|
|
|
wrapped_style = ParagraphStyle(
|
|
name='Wrapped',
|
|
fontName='Arial',
|
|
fontSize=8,
|
|
leading=10,
|
|
wordWrap='CJK',
|
|
alignment=TA_LEFT
|
|
)
|
|
|
|
data = [
|
|
["ID", Paragraph(str(deal['id']), wrapped_style)],
|
|
["Название", Paragraph(deal.get('name', ''), wrapped_style)],
|
|
["Клиент", Paragraph(deal.get('client', {}).get('name', ''), wrapped_style)],
|
|
["Склад", Paragraph(deal.get('shipmentWarehouseName', ''), wrapped_style)],
|
|
["Товаров", Paragraph(f'{deal.get("totalProducts", 0)} тов.', wrapped_style)],
|
|
]
|
|
|
|
table = Table(data, colWidths=[30 * mm, 35 * mm])
|
|
table.setStyle(TableStyle([
|
|
('FONTNAME', (0, 0), (-1, -1), 'Arial'),
|
|
('FONTSIZE', (0, 0), (-1, -1), 8),
|
|
('BOTTOMPADDING', (0, 0), (-1, -1), 4),
|
|
('TOPPADDING', (0, 0), (-1, -1), 4),
|
|
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
|
('GRID', (0, 0), (-1, -1), 0.5, colors.black),
|
|
]))
|
|
|
|
elements.append(table)
|
|
document.build(elements)
|
|
|
|
return Response(
|
|
content=buffer.getvalue(),
|
|
media_type="application/pdf",
|
|
headers={"Content-Disposition": f"inline; filename=deal_{deal_id}.pdf"}
|
|
)
|