【基本原理】
利用PyPDF2的PdfFileReader模块打开pdf文件,如果不抛异常,就认为此pdf文件有效。有时打开并不抛出异常,但是有这种警告:UserWarning: startxref on same line as offset [pdf.py:1680]。这种情况pdf多半也是坏的,可进一步通过页数判断。但walker在测试中发现,对于正常pdf文件,进一步通过页数判断时有时会抛出异常。
【情形一】
pdf文件在磁盘上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import traceback
from PyPDF2 import PdfFileReader
#参数为pdf文件全路径名
def isValidPDF_pathfile(pathfile):
bValid = True
try:
#PdfFileReader(open(pathfile, 'rb'))
reader = PdfFileReader(pathfile)
if reader.getNumPages() < 1: #进一步通过页数判断。
bValid = False
except:
bValid = False
print('*' + traceback.format_exc())
return bValid
|
【情形二】
pdf是来自网络的bytes数据。由于PdfFileReader的参数为文件名或文件对象,所以需要做一下转换。
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import traceback, tempfile
from PyPDF2 import PdfFileReader
#参数为bytes类型数据。利用临时文件。
def isValidPDF_bytes(pdfBytes):
bValid = True
try:
fp = tempfile.TemporaryFile()
fp.write(pdfBytes)
reader = PdfFileReader(fp)
fp.close()
if reader.getNumPages() < 1: #进一步通过页数判断。
bValid = False
except:
bValid = False
print('*' + traceback.format_exc())
return bValid
|
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import io, traceback
from PyPDF2 import PdfFileReader
#参数为bytes类型数据。利用BytesIO转换。
def isValidPDF_bytes(pdfBytes):
bValid = True
try:
b = io.BytesIO(pdfBytes)
reader = PdfFileReader(b)
if reader.getNumPages() < 1: #进一步通过页数判断。
bValid = False
except:
bValid = False
print('*' + traceback.format_exc())
return bValid
|
还可以利用PDFlib判断:
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 |
import os
from PDFlib.PDFlib import PDFlib
from PDFlib.PDFlib import PDFlibException
def isValidPdf(pathfile):
p = PDFlib()
p.set_option("license=xxxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx")
p.set_option("errorpolicy=return");
indoc = p.open_pdi_document(pathfile, 'repair=none');
print('indoc:' + str(indoc))
print('pathfile size:' + str(os.path.getsize(pathfile)) + 'B')
bValid = False
if (indoc == -1):
print('*' + p.get_errmsg())
bValid = False
else:
pageNumber = p.pcos_get_number(indoc, "length:pages")
print('pageNumber:' + str(pageNumber))
if pageNumber < 1: #页数为0
bValid = False
else:
bValid = True
if bValid:
p.close_pdi_document(indoc)
return bValid
|
参考文档:
1、The PdfFileReader Class
2、tempfile — Generate temporary files and directories
3、io — Core tools for working with streams
*** walker ***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1683680如需转载请自行联系原作者
RQSLT