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'):