Skip to content

Commit

Permalink
fixbug:修复了生成pdf体积离谱的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
rockmagma02 committed Jul 21, 2021
1 parent e182c44 commit 3fdef03
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pdf
*.png
.vscode

*.pyc

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ tqdm
requests
urllib3
beautifulsoup4
Pillow
Pillow
fpdf
12 changes: 8 additions & 4 deletions src/converter.py
Original file line number Diff line number Diff line change
@@ -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')
5 changes: 3 additions & 2 deletions src/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down

0 comments on commit 3fdef03

Please sign in to comment.