소개

document.py, Python의 Document 클래스를 위해 Aspose.PDF FOSS을 정의하는 모듈은 이를 “네이티브 PDF 엔진을 래핑하는” 클래스라고 설명합니다. 구체적으로 그 엔진은 aspose_pdf.engine.simple_pdf.SimplePdf이며: Document.load_fromSimplePdf 인스턴스를 생성하고, 저장, 최적화, 복구, 암호화와 같은 모든 문서 수준 작업은 이를 위임합니다. 렌더링 또한 마찬가지로 Page.renderDocument.render_page이 모두 aspose_pdf.engine.rasterizer을 호출합니다.

대부분의 애플리케이션 코드는 DocumentPage을 넘어서 볼 필요가 없습니다. 하지만 aspose_pdf.engine는 일반적인, import 가능한 Python 패키지이며, 파사드 아래의 여러 구성 요소가 독립적으로 유용합니다: 페이지를 픽셀로 변환하는 래스터라이저, PDF 콘텐츠 스트림을 압축·해제압축하는 스트림-필터 코덱, 그리고 PDF 보안 핸들러가 사용하는 AES-CBC 및 RC4 알고리즘을 구현하는 암호화 유틸리티. 메인 패키지와 달리 aspose_pdf.engine 서브모듈은 최상위 aspose_pdf에서 재내보내지 않으며—예외는 RasterizedPage이며 aspose_pdf.visualization를 통해 연결됩니다—따라서 아래 모든 것은 전체 점표기 경로로 import됩니다. 예를 들어 from aspose_pdf.engine.filters import StreamDecoder.

Aspose.PDF FOSS for Python는 Python 패키지인 aspose-pdf-foss-for-python이며, MIT 라이선스로 배포되고 Python 3.11 이상을 요구합니다. 최상위 모듈 이름은 aspose_pdf입니다. 핵심 패키지는 cryptographyasn1crypto에만 의존하고, 선택적 추가 기능으로 Pillow 기반 이미지 디코딩, Brotli 기반 WOFF2 폰트 지원, 그리고 HarfBuzz 기반 복잡한 텍스트 레이아웃을 제공합니다.


핵심 기능

래스터화 엔진을 이용한 페이지 렌더링

Document.render_page(page_index, dpi=..., scale=..., background=..., antialias=...)와 그 Page.render 대응물은 모두 RasterizedPage를 반환합니다 — 엔진의 래스터라이저가 만든 패키징된 RGB 래스터입니다. RasterizedPagewidth, height, 그리고 원시 pixels 바이트를 제공하며, 단일 픽셀 조회를 위한 get_pixel(), 메모리 내 인코딩 바이트를 위한 to_png() / to_tiff(), 그리고 .png 혹은 .tif/.tiff 파일을 직접 쓸 수 있는 save()을 제공합니다. 객체를 메모리에서 다루는 것(디스크에만 저장하는 것이 아니라)은 호출자가 픽셀을 검사하거나 변환한 후 보관 여부를 결정하고자 할 때 유용합니다.

from aspose_pdf import Document

with Document() as document:
    page = document.pages.add()
    page.add_text("Rendered by the engine", x=72, y=700, font_size=18)

    raster = document.render_page(0, dpi=150)
    print(raster.width, raster.height)

    corner = raster.get_pixel(0, 0)
    print("top-left pixel:", corner)

    with open("page-0.png", "wb") as handle:
        handle.write(raster.to_png())
    raster.save("page-0.tiff")

스트림 필터 엔진

PDF 콘텐츠 및 객체 스트림은 FlateDecodeLZWDecode와 같은 명명된 필터로 압축됩니다. aspose_pdf.engine.filters.StreamEncoder.encode(data, filters, decode_parms=None)StreamDecoder.decode(data, filters, decode_parms, *, limits=None, max_output_bytes=None)은 해당 필터 체인의 인코드와 디코드 측을 구현합니다 — 엔진이 문서 스트림을 쓰고 읽을 때 내부적으로 사용하는 동일한 코덱입니다. 두 함수는 단일 필터 이름이나 체인 필터 목록을 모두 허용하며, StreamDecoder.decode()는 인식되지 않은 필터 이름에 대해 PdfValidationException를 발생시켜 조용히 잘못된 바이트를 반환하는 대신 오류를 발생시킵니다.

from aspose_pdf.engine.filters import StreamDecoder, StreamEncoder

raw = b"Sample content-stream payload.\n" * 40
compressed = StreamEncoder.encode(raw, "FlateDecode")
restored = StreamDecoder.decode(compressed, "FlateDecode", None)

assert restored == raw
print(f"{len(raw)} bytes -> {len(compressed)} bytes with FlateDecode")

암호화 엔진

aspose_pdf.engine.encryption.EncryptionUtilsSimplePdf이 모든 암호화 관련 작업에 호출하는 정적 메서드 유틸리티 클래스입니다: Document.encrypt, decrypt(), change_passwords()는 모두 내부적으로 EncryptionUtils 호출로 해결됩니다. 이 클래스의 메서드는 AES-CBC와 RC4 암호화를 직접 구현하며 (encrypt_aes_cbc, decrypt_aes_cbc, encrypt_rc4, decrypt_rc4), PDF 표준 보안 핸들러 버전에 대한 키 파생 (compute_owner_key_v4, compute_user_key_v4, compute_user_owner_keys_v6), 그리고 새 문서에 필요한 무작위 16바이트 ID를 위한 generate_file_id()을 제공합니다.

from aspose_pdf.engine.encryption import EncryptionUtils

key = b"0123456789ABCDEF"  # 16 bytes -> AES-128
ciphertext = EncryptionUtils.encrypt_aes_cbc(key, b"Reviewer notes: approved")
plaintext = EncryptionUtils.decrypt_aes_cbc(key, ciphertext)
assert plaintext == b"Reviewer notes: approved"

file_id = EncryptionUtils.generate_file_id()
print(file_id.hex())

빠른 시작

패키지를 설치한 후, 단일 스크립트에서 엔진 레이어로 직접 페이지를 렌더링합니다.

asposefoss/pdf is not yet published — build from source until it ships. See the project README for build instructions.
from aspose_pdf import Document

with Document() as document:
    page = document.pages.add()
    page.add_text("Engine-rendered page", x=72, y=740, font_size=20)

    raster = document.render_page(0, dpi=150)
    raster.save("engine-render.png")

    document.save("engine-quickstart.pdf")

print("Saved engine-quickstart.pdf and engine-render.png")

지원되는 포맷

포맷확장자읽기쓰기
PDFpdf
TIFFtiff-

PDF 읽기 및 쓰기는 Aspose.PDF FOSS Python의 핵심 기능 — Document/SimplePdf 엔진 쌍은 이 게시물의 모든 코드 경로에서 PDF 문서를 로드하고 씁니다. Document.render_page, Page.render, 및 RasterizedPage.to_png()/to_tiff()는 추가로 PNG와 TIFF 래스터 출력을 생성합니다; 이는 문서 로드 형식이 아니라 페이지 래스터화 출력입니다.


오픈 소스 및 라이선스

Aspose.PDF FOSS for Python은 MIT 라이선스로 배포됩니다: 사용 제한이 없으며, 런타임 비용도 없고, 상업용이나 개인용 모두 등록 요구사항이 없습니다. 소스 코드는 github.com/aspose-pdf-foss/Aspose-PDF-FOSS-for-Python에 호스팅되며, 패키지는 PyPI에 aspose-pdf-foss-for-python로 게시됩니다.


시작하기

관련 리소스