Home > database >  How to rotate only Table, using borb (PDF library) python?
How to rotate only Table, using borb (PDF library) python?

Time:07-17

I am using borb (PDF library in python). I need to rotate Only the table. (Also the text inside the table should be rotated as per the table rotation)

  • Say for example, if I am rotating the Table to left (i.e)(90° left_side), text inside the table should also be rotated. (similarly for right rotation)
  • In short, only the Content should be rotated.

Here is my code.py

from decimal import Decimal
from pathlib import Path

from borb.pdf.canvas.layout.image.image import Image
from borb.pdf.canvas.layout.layout_element import Alignment
from borb.pdf.canvas.layout.page_layout.multi_column_layout import SingleColumnLayout
from borb.pdf.canvas.layout.page_layout.page_layout import PageLayout
from borb.pdf.canvas.color.color import HexColor
from borb.pdf.canvas.layout.table.fixed_column_width_table import FixedColumnWidthTable
from borb.pdf.canvas.layout.table.flexible_column_width_table import FlexibleColumnWidthTable
from borb.pdf.canvas.layout.table.table import TableCell
from borb.pdf.canvas.layout.text.chunk_of_text import ChunkOfText
from borb.pdf.canvas.layout.text.paragraph import Paragraph
from borb.pdf.document.document import Document
from borb.pdf.page.page import Page
from borb.pdf.pdf import PDF




def main():
    # define theme color

    # create new Document
    doc: Document = Document()

    # create new Page
    page: Page = Page()
    doc.add_page(page)

    # set PageLayout
    layout: PageLayout = SingleColumnLayout(page, horizontal_margin=Decimal(25), vertical_margin=Decimal(25))


    layout.add(Paragraph("Welcome"))


    layout.add(
        FixedColumnWidthTable( number_of_columns=1, number_of_rows=2)
            .add(Paragraph("Testing_Line1"))
            .add(Paragraph("Testing_Line2")))




    with open("output.pdf", "wb") as out_file_handle:
        PDF.dumps(out_file_handle, doc)


if __name__ == "__main__":
    main()

CodePudding user response:

disclaimer: I am the author of the borb library.

borb currently does not support rotating LayoutElement.

There are two things you could do:

  • rotate the Page
  • edit the Page content-stream, and insert a Tm operator to have the content rotated (you would then insert the Table afterwards, and apply another Tm operator to ensure the content after the Table is laid out normally again)
  • Related