From 3fdef031065e8c3dce09ab090c98a4b384316ab4 Mon Sep 17 00:00:00 2001 From: rockmagma02 Date: Wed, 21 Jul 2021 12:02:57 +0800 Subject: [PATCH] =?UTF-8?q?fixbug:=E4=BF=AE=E5=A4=8D=E4=BA=86=E7=94=9F?= =?UTF-8?q?=E6=88=90pdf=E4=BD=93=E7=A7=AF=E7=A6=BB=E8=B0=B1=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + requirements.txt | 3 ++- src/converter.py | 12 ++++++++---- src/downloader.py | 5 +++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index c546b8d..f6004e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.pdf *.png +.vscode *.pyc diff --git a/requirements.txt b/requirements.txt index 60f55bb..c18a25f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ tqdm requests urllib3 beautifulsoup4 -Pillow \ No newline at end of file +Pillow +fpdf \ No newline at end of file diff --git a/src/converter.py b/src/converter.py index b868fed..b7f3282 100644 --- a/src/converter.py +++ b/src/converter.py @@ -1,11 +1,15 @@ from PIL import Image +from fpdf import FPDF import os def converter(imgs, PDFPath = './PDF/', title = 'unknow'): print('generating a pdf......') os.makedirs(PDFPath, exist_ok=True) bookPath = PDFPath + title +'.pdf' - if len(imgs) == 1: - imgs[0].save(bookPath, 'PDF') - elif len(imgs) >= 1: - imgs[0].save(bookPath, 'PDF', save_all = True, quality=100, append_images = imgs[1:]) + cover = Image.open(imgs[0]) + width, height = cover.size + pdf = FPDF(unit = 'pt', format = [width, height]) + for page in imgs: + pdf.add_page() + pdf.image(page, 0, 0) + pdf.output(bookPath, 'F') diff --git a/src/downloader.py b/src/downloader.py index d12bc50..8ceefcd 100644 --- a/src/downloader.py +++ b/src/downloader.py @@ -7,8 +7,9 @@ def imgDownloaderSingle(imgUrl, page, title = 'unknow', timeout = 60, targetPath os.makedirs(targetPath, exist_ok=True) r = requests.get(imgUrl, timeout = timeout) img = Image.open(BytesIO(r.content)) - img.save(targetPath+title+'{:03d}'.format(page)+'.png', 'PNG') - imgs.append(img) + imgFile = targetPath+title+'{:03d}'.format(page)+'.png' + img.save(imgFile, 'PNG') + imgs.append(imgFile) return imgs def imgDownloaderTotal(urlIter, quality = 'high'):