forked from togolife/baidu-wenku-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownPDF.py
68 lines (61 loc) · 1.86 KB
/
downPDF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import urllib2
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# pdf操作库reportlab
from reportlab.lib.pagesizes import A4, landscape
from reportlab.pdfgen import canvas
from PIL import Image # PIL是python内部模块,不是reportlab库的
import config
import log
logger = log.Log(config.log_dir, config.log_name)
class DownPDF(object):
def __init__(self, fileDir, url, info):
self.fileDir = './' + fileDir + '/'
self.URL = url
self.WkInfo = info
if not os.path.exists(self.fileDir):
os.mkdir(self.fileDir)
# 获取每页URL
@staticmethod
def geturl(urls, index):
for url in urls:
if url['pageIndex'] == index:
return url['pageLoadUrl']
return ''
def down(self):
urlBase = self.URL[self.URL.rfind('/') + 1 : self.URL.find('.html')]
reqHeader = config.reqHeaderBDWK
reqHeader['Referer'] = self.URL
i = 1
first = 0
pdfFileName = self.fileDir + self.WkInfo['title'] + '.' + self.WkInfo['docType']
while i <= self.WkInfo['totalPageNum']:
pngUrl = DownPDF.geturl(self.WkInfo['htmlUrls']['png'], i)
if len(pngUrl) == 0:
logger.error('下载文档失败,查找URL失败!')
return False
pngUrl = pngUrl.replace('\\', '')
pngUrl = pngUrl.replace(' ', '%20')
req = urllib2.Request(pngUrl, None, reqHeader)
res = urllib2.urlopen(req)
res = res.read()
imgFileName = self.fileDir + urlBase + '-'+str(i)+'.png'
fp = open(imgFileName, 'wb')
fp.write(res)
fp.close()
if first == 0:
first = 1
img = Image.open(imgFileName)
pdfHandler = canvas.Canvas(pdfFileName, pagesize=img.size)
img.close()
pdfHandler.drawImage(imgFileName, 0, 0)
pdfHandler.showPage()
os.remove(imgFileName)
if i == self.WkInfo['totalPageNum']:
pdfHandler.save()
i += 1
return True