aFelipeSP / pdfme

Make PDFs easily
MIT License
312 stars 21 forks source link

progress bar or output the page being processed #26

Closed sheneeb closed 5 months ago

sheneeb commented 2 years ago

Hi @aFelipeSP , Larger datasets takes longer to generate the pdf like about 8 to 10 mins, total page nos of about 1000 pages, is there a way to implement a progress bar or status on which page is being processed ?


from pdfme import PDFDocument

df = pd.DataFrame(
    [
        [
            "sn",
            "sn_title",
            "cross_reference_sn",
            "doc_ref",
            "mc",
            "ld",
            "nd",
            "sn_status",
        ],
        [
            1,
            "title...",
            "ref1",
            "doc1",
            "very long " + ("text " * 50),
            "text",
            "text",
            "closed",
        ],
    ]
)
df = df.astype(str)
table = df.to_numpy().tolist()

def build(table, pages=100):

    document = {
        "style": {
            "text_align": "l",
            "rotate_page": True,
            "page_size": "a4",
        },
        "running_sections": {
            "header1": {
                "x": "left",
                "y": 10,
                "height": "top",
                "style": {"text_align": "c"},
                "content": [{".b": "sample heading \nsample sub heading"}],
            },
            # "header2": {
            #     "x": "left",
            #     "y": 10,
            #     "width": 40,
            #     "content": [{"image": "logo.jpg"}],
            # },
            "header3": {
                "x": "left",
                "y": 50,
                "height": "top",
                "style": {"text_align": "c"},
                "content": [
                    {
                        "widths": [0.5, 2, 2, 2, 1.5, 1, 1],
                        "style": {
                            "s": 9,
                            "cell_margin": 0,
                            "cell_margin_left": 5,
                            "cell_margin_bottom": 4,
                        },
                        "table": [
                            [
                                None,
                                {
                                    ".": [
                                        {".b": "mfg: "},
                                        {".": "eurolace" + "\n\n"},
                                        {".b": "sponsor: "},
                                        {".": " fantom" + "\n\n"},
                                        {".b": "Line No.: "},
                                        {".": "743" + "\n\n"},
                                        {".b": "Line Type: "},
                                        {".": " Linen"},
                                    ]
                                },
                                {
                                    ".": [
                                        {".b": "(Rotation): "},
                                        {".": "62408" + "\n\n"},
                                        {".b": "Double Rotation: "},
                                        {".": " 8911" + "\n\n"},
                                        {".b": "At Date: "},
                                        {".": "23-Oct-21" + "\n\n"},
                                        {".b": "Date of Start: "},
                                        {".": " 09-Dec-08"},
                                    ]
                                },
                                {
                                    ".": [
                                        {".b": "Data Weekly: "},
                                        {".": "202XB" + "\n\n"},
                                        {".b": "Data Monthly: "},
                                        {".": "110XB" + "\n\n"},
                                        {".b": "Data Yearly "},
                                        {".": "10-Mar-10" + "\n\n"},
                                    ]
                                },
                                {
                                    "colspan": 3,
                                    ".": [
                                        {".b": "Data Provided by: "},
                                        {".": "Local Library" + "\n\n"},
                                        {".b": "Address: "},
                                        {".": "PO Box XXXX" + "\n\n"},
                                        {".b": "Contact: "},
                                        {".": "Tel: 435..." + "\n\n"},
                                        {".b": ""},
                                        {".": "e-mail: report@email.com" + "\n\n"},
                                    ],
                                },
                                None,
                                None,
                            ],
                        ],
                    },
                ],
            },
            "header4": {
                "x": "left",
                "y": 50,
                "height": "top",
                "style": {"text_align": "c"},
                "content": [
                    {
                        "widths": [0.5, 2, 2, 2, 1.5, 1, 1],
                        "style": {
                            "s": 9,
                            "cell_margin": 0,
                            "cell_margin_left": 5,
                            "cell_margin_bottom": 4,
                        },
                        "table": [
                            [
                                None,
                                {
                                    ".": [
                                        {".b": "mfg: "},
                                        {".": "eurolace" + "\n\n"},
                                        {".b": "sponsor: "},
                                        {".": " fantom" + "\n\n"},
                                        {".b": "Line No.: "},
                                        {".": "743" + "\n\n"},
                                        {".b": "Line Type: "},
                                        {".": " Linen"},
                                    ]
                                },
                                None,
                                None,
                                None,
                                None,
                                None,
                            ],
                        ],
                    },
                ],
            },
            "footer": {
                "x": "left",
                "y": 570,
                "height": "bottom",
                "style": {"text_align": "c"},
                "content": [
                    {
                        "widths": [0.5, 3, 1, 3],
                        "style": {
                            "border_width": 0,
                            "cell_margin": 0,
                            "s": 7,
                            "text_align ": "l",
                        },
                        "borders": [{"pos": "h0;::", "width": 0.5}],
                        "table": [
                            [
                                {".i": "F/1957R1"},
                                {".": ["Report Date: ", {".i": "report_date"}]},
                                {".": ["Page ", {"var": "$page"}, " of ", str(pages)]},
                                {".": ["Internal Audit by:", {".i": "user_name"}]},
                            ]
                        ],
                    }
                ],
            },
        },
        "sections": [
            {
                "style": {"page_numbering_style": "roman"},
                "running_sections": [
                    # "header1",
                    # "header2",
                    "header3",
                    "footer"
                ],
                "content": [
                    {
                        "widths": [1, 2, 1.2, 1, 4, 1, 1, 1],
                        "style": {
                            "margin_left": 0,
                            "margin_right": 0,
                            "s": 7,
                            "border_width": 0,
                            "cell_margin_top": 2,
                            "cell_margin": 10,
                        },
                        "borders": [{"pos": "::;:", "width": 0.2}],
                        "table": table,
                    }
                ],
            }
        ],
    }

    doc = PDFDocument(document)
    doc.run()
    return doc, len(doc.pdf.pages)

doc, pages = build(table)
doc, pages = build(table, pages)

with open("document.pdf", "wb") as f:
    doc.output(f)```