본문 바로가기

Spring Boot

[Python] iText7으로 PDF출력 (샘플 이미지, 표 추가 소스)

반응형

이미지와 테이블이 추가된 Python 환경에서 iText를 사용하여 PDF 출력을 생성하는 방법의 예입니다.

1. Python 환경에 iText 라이브러리를 설치합니다.

pip install itext

 

2. PDF를 생성하기 위해 Python 스크립트를 생성합니다.

from itext import Document, PageSize, Image

def generate_pdf(file_name):
    # Create a new document with custom page size
    document = Document(PageSize.A4)

    # Open the document for writing
    document.open()

    # Add an image
    image = Image("image.jpg")
    document.add(image)

    # Add a table
    table = [["Column 1", "Column 2", "Column 3"],
             ["Row 1, Column 1", "Row 1, Column 2", "Row 1, Column 3"],
             ["Row 2, Column 1", "Row 2, Column 2", "Row 2, Column 3"]]
    columns = len(table[0])
    rows = len(table)

    for row in range(rows):
        for column in range(columns):
            document.text(table[row][column])
            if column < columns - 1:
                document.text(" | ")
        document.text("\n")

    # Save the document
    document.save(file_name)

# Generate the PDF
generate_pdf("output.pdf")

 

반응형