aFelipeSP / pdfme

Make PDFs easily
MIT License
312 stars 21 forks source link

overlapping header with section data #18

Closed sheneeb closed 2 years ago

sheneeb commented 2 years ago

@aFelipeSP , so far i managed to get a working report. still stuck with these four points

1. The section is overlapping with the header, i thought this would align itself automatically

import pandas as pd
from pdfme import build_pdf

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()

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"}]},
                            {".": ["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,
                }
            ],
        }
    ],
}

with open("document.pdf", "wb") as f:
    build_pdf(document, f)

image

2. How can i change the header to take header4 from page 2 on wards instead of header3

3. How to get the total number of pages so i can show page 1 of page xx

4. The header from the running section can fit only limited content, how can i increase that to fit more contents?, i tested with repeating the header mor than 5 times and only the first 3 headers where appearing

aFelipeSP commented 2 years ago

Hi @sheneeb ,

  1. The running sections are placed in an absolute position in all the pages of a section, so you have to make sure they fit in the top or bottom of the page. You should set a smaller y position on the running section, and make the size of the font smaller so it doesn't overlap with the contents of the page. You can set the height and width of any running section too.
  2. You have to create a new section in the sections key of the document dict, and set the running_sections of the new section to include the header4, and not the header3.
  3. You have to build the document once to get the number of pages and run it one more time to include the number of pages in the document this time, like this:
    
    import pandas as pd
    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)


4. You can set the height of a running section. Set a bigger height for the header you are defining, big enough to fit he content you want. If it takes a lot of space you can increase the top margin of the section.  
sheneeb commented 2 years ago

@aFelipeSP , can you help with the overlapping issue, how to set a bigger height for the running section, i tried adding the following to header3 in your modified code and still doesn't seem to change

 'style': {'border_width': 0, 'margin_left': 70, 'margin_right': 70, 'margin_bottom': 20, 'margin_top':30},
                "content": [

unable to find in the documentation search on setting width and height for running section, please help

sheneeb commented 2 years ago

resolved with "style": {"margin": [103,20,20,10]},