2017-04-01 10 views
0

fpdf를 사용하여 디렉토리의 모든 이미지를 동일한 pdf 출력으로 어떻게 출력합니까? 나는 pdf로 출력하기 위하여 폴더에있는 마지막 파일을 얻거나 각 심상을위한 다수 pdfs를 출력 할 수있다, 그러나 카탈로그 같이 동일한 pdf에있는 모든 심상 아닙니다. for 루프이지만 제대로 정렬 할 수는 없습니다. 아직fpdf 여러 이미지가있는 단일 출력

from fpdf import FPDF 
from PIL import Image 
import glob 
import os 


# set here 
image_directory = '/home/User/Desktop/images/' 
extensions = ('*.jpg','*.jpeg','*.png','*.gif') 


# set 0 if you want to fit pdf to image 
# unit : pt 
margin = 10 

imagelist=[] 
for ext in extensions: 
    imagelist.extend(glob.glob(os.path.join(image_directory,ext))) 

pdf = FPDF(unit="pt", format=[width + 2*margin, height + 2*margin]) 
pdf.add_page() 
cover = Image.open(imagePath) 
width, height = cover.size 

for imagePath in imagelist: 
    pdf.image(imagePath, margin, margin) 
    destination = os.path.splitext(imagePath)[0] 
pdf.output(destination + ".pdf", "F") 

답변

1

하지 FPDF는 하나의 PDF 출력으로 이미지 파일의 반복을 그룹화되지하지만 img2pdf 파일 이름 목록이 아주 멋지게 일을 받아 확실한 이유.

import img2pdf 

imagefiles = ["test1.jpg", "test2.png"] 

with open("images.pdf","wb") as f: 
    f.write(img2pdf.convert(imagefiles)) 
+0

위대한 작품! 공유해 주셔서 감사합니다. – Xonshiz