aFelipeSP / pdfme

Make PDFs easily
MIT License
312 stars 21 forks source link

Line wrapping - Table cell overflow #29

Closed milosta closed 10 months ago

milosta commented 10 months ago

When the content of a cell in a table is too long, it overflows into the next cell. It would be better to cut it and continue on the next line. image

aFelipeSP commented 10 months ago

Hi Milosta! cells wrap text automatically, so it looks like a bug. Can you share your code with me so I can find the issue?

milosta commented 10 months ago

Thank you aFelipeSP. I should have specified that it works if the text contains spaces. But it does not work with very long words, such as emails. Here is a minimal working example.

from pdfme import build_pdf

doc = {
    "style": {
        "margin_bottom": 15,
        "s": 10,
        "page_size": "a4",
        "margin": [60, 35, 60, 35],
    },
    "formats": {
        "title": {
            "b": 1,
            "s": 15,
            "cell_margin_top": 0,
            "cell_margin_bottom": 10,
            "cell_margin_left": 0,
            "cell_margin_right": 0,
            "text_align": "c",
        },
    },
    "sections": [
        {
            "content": [
                {"content": ["Some title"], "style": "title"},
                {
                    "style": {
                        "s": 8.5,
                        "border_width": 0,
                        "margin_top": 0,
                        "margin_bottom": 10,
                        "cell_margin": 3,
                    },
                    "borders": [{"pos": "h1:1000000:1;:", "color": 0.9, "width": 0.5}],
                    "widths": [1, 0.7, 1, 3],
                    "table": [
                        ["Data1", "Data2", "Data3", "Data4"],
                        [
                            "Lorem ipsum dolor sit amet",
                            "Works with spaces present",
                            "Lorem ipsum dolor sit amet",
                            "Lorem ipsum dolor sit amet",
                        ],
                        [
                            "Lorem ipsum dolor sit amet",
                            "DoesNotWorkWithoutSpaces",
                            "Lorem ipsum dolor sit amet",
                            "Lorem ipsum dolor sit amet",
                        ],
                    ],
                },
            ]
        }
    ],
}
filepath = "test.pdf"
with open(filepath, "wb") as f:
    build_pdf(doc, f)

Result looks like this image

aFelipeSP commented 10 months ago

Unfortunately this is not possible at the moment, and it will not probably be added anytime soon. What you can do is split the strings with python following any condition you like (split every 20 letters for example).