def render_pdf(self):
outpdf = PdfFileWriter()
for page in self.pages:
if page.extension == "pdf":
# the page is already a PDF so append directly
outpdf.addPage(PdfFileReader(BytesIO(page.binary)).getPage(0))
else:
# otherwise, the page is an image that needs to be converted to PDF first
buf = BytesIO()
img = Image.open(BytesIO(page.binary))
img.convert("RGB").save(buf, format="pdf")
# once image is PDF, it can be appended
outpdf.addPage(PdfFileReader(buf).getPage(0))
pdf_page_buf = BytesIO()
outpdf.write(pdf_page_buf)
return(pdf_page_buf.getvalue())
评论列表
文章目录