from io import BytesIO
from typing import List
import PyPDF2
def merge_pdfs(pdf_bytes: List[bytes]) -> bytes:
merger: PyPDF2.PdfFileMerger = PyPDF2.PdfFileMerger()
try:
for b in pdf_bytes:
merger.append(PyPDF2.PdfFileReader(BytesIO(b))) if b else None
ret: BytesIO = BytesIO()
merger.write(ret)
ret.seek(0)
return ret.read()
finally:
merger.close()
bytes <-> File読み書きは割愛。