Untitled

yankeeco avatar
yankeeco
python
05/03/2026 5:57 PM
5.8 KB
7
Indexable
# ----------------------------------------------------------------------------
# PDF GENERATION
# ----------------------------------------------------------------------------

def generate_pdf(doc_data):
    buffer = io.BytesIO()
    doc = SimpleDocTemplate(
        buffer, pagesize=A4,
        rightMargin=2*cm, leftMargin=2.5*cm,
        topMargin=2*cm, bottomMargin=2*cm,
    )
    styles = getSampleStyleSheet()

    title_style = ParagraphStyle("DocTitle", parent=styles["Normal"],
        fontSize=14, leading=18, alignment=TA_CENTER,
        spaceAfter=4, fontName=PDF_FONT_BOLD)
    subtitle_style = ParagraphStyle("DocSubtitle", parent=styles["Normal"],
        fontSize=9, leading=12, alignment=TA_CENTER, spaceAfter=2,
        fontName=PDF_FONT_ITALIC, textColor=colors.HexColor("#555555"))
    meta_style = ParagraphStyle("DocMeta", parent=styles["Normal"],
        fontSize=10, leading=14, alignment=TA_CENTER,
        spaceAfter=6, fontName=PDF_FONT)
    heading_style = ParagraphStyle("SectionHeading", parent=styles["Normal"],
        fontSize=11, leading=14, spaceBefore=14, spaceAfter=4,
        fontName=PDF_FONT_BOLD, textColor=colors.HexColor("#1a3a5c"))
    body_style = ParagraphStyle("BodyText", parent=styles["Normal"],
        fontSize=10, leading=15, alignment=TA_JUSTIFY,
        spaceAfter=4, fontName=PDF_FONT)
    sign_style = ParagraphStyle("SignLine", parent=styles["Normal"],
        fontSize=10, leading=20, fontName=PDF_FONT)

    story = []
    story.append(Spacer(1, 0.3*cm))
    story.append(Paragraph(doc_data["title"], title_style))
    if doc_data.get("subtitle"):
        story.append(Paragraph(doc_data["subtitle"], subtitle_style))
    story.append(Paragraph(
        "№ %s  |  %s  |  %s" % (doc_data["number"], doc_data["city"], doc_data["date"]),
        meta_style))
    story.append(HRFlowable(width="100%", thickness=1.5, color=colors.HexColor("#1a3a5c")))
    story.append(Spacer(1, 0.4*cm))

    for section in doc_data["sections"]:
        story.append(Paragraph(section["heading"], heading_style))
        if "body" in section:
            for line in section["body"].split("\n"):
                story.append(Paragraph(line or " ", body_style))
        if "table" in section:
            t = section["table"]
            table_data = [t["headers"]] + t["rows"] + [["", "", "", "", "РАЗОМ:", t["total"]]]
            col_widths = [1*cm, 7*cm, 2*cm, 2*cm, 2.5*cm, 2.5*cm]
            tbl = Table(table_data, colWidths=col_widths)
            tbl.setStyle(TableStyle([
                ("FONTNAME",      (0,0),  (-1,-1), PDF_FONT),
                ("BACKGROUND",    (0,0),  (-1,0),  colors.HexColor("#1a3a5c")),
                ("TEXTCOLOR",     (0,0),  (-1,0),  colors.white),
                ("FONTNAME",      (0,0),  (-1,0),  PDF_FONT_BOLD),
                ("FONTSIZE",      (0,0),  (-1,-1), 9),
                ("ALIGN",         (0,0),  (-1,-1), "CENTER"),
                ("ALIGN",         (1,1),  (1,-2),  "LEFT"),
                ("GRID",          (0,0),  (-1,-2), 0.5, colors.HexColor("#cccccc")),
                ("BACKGROUND",    (0,-1), (-1,-1), colors.HexColor("#e8f0fe")),
                ("FONTNAME",      (0,-1), (-1,-1), PDF_FONT_BOLD),
                ("LINEABOVE",     (0,-1), (-1,-1), 1, colors.HexColor("#1a3a5c")),
                ("ROWBACKGROUNDS",(0,1),  (-1,-2), [colors.white, colors.HexColor("#f5f8ff")]),
                ("TOPPADDING",    (0,0),  (-1,-1), 5),
                ("BOTTOMPADDING", (0,0),  (-1,-1), 5),
            ]))
            story.append(tbl)
            story.append(Spacer(1, 0.3*cm))

    story.append(Spacer(1, 0.8*cm))
    story.append(HRFlowable(width="100%", thickness=0.5, color=colors.HexColor("#aaaaaa")))
    story.append(Spacer(1, 0.4*cm))
    sign_data = [
        [Paragraph("ВИКОНАВЕЦЬ / ЗАЯВНИК", sign_style), Paragraph("ЗАМОВНИК / ОТРИМУВАЧ", sign_style)],
        [Paragraph("________________  /_____________/", sign_style), Paragraph("________________  /_____________/", sign_style)],
    ]
    sign_table = Table(sign_data, colWidths=[9*cm, 9*cm])
    sign_table.setStyle(TableStyle([
        ("ALIGN",      (0,0), (-1,-1), "LEFT"),
        ("FONTSIZE",   (0,0), (-1,-1), 10),
        ("TOPPADDING", (0,0), (-1,-1), 6),
    ]))
    story.append(sign_table)
    doc.build(story)
    buffer.seek(0)
    return buffer.read()
Editor is loading...
Leave a Comment