【基本原理】
利用PyPDF2的PdfFileReader模块打开pdf文件,如果不抛异常,就认为此pdf文件有效。有时打开并不抛出异常,但是有这种警告:UserWarning: startxref on same line as offset [pdf.py:1680]。这种情况pdf多半也是坏的,可进一步通过页数判断。但walker在测试中发现,对于正常pdf文件,进一步通过页数判断时有时会抛出异常。
【情形一】
pdf文件在磁盘上。
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的参数为文件名或文件对象,所以需要做一下转换。
方法一:
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
方法二:
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判断:
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
updated 2018-12-12
encoding: utf-8
author: walker
date: 2018-12-12
summary: 直接用 PDF 文件内容判断 PDF 的正确性和完整性,适用于判断下载的 PDF
import re
def isValidPDF_pathfile(pathfile):
r"""
直接用文件内容判断头尾,
参数为pdf文件全路径名
"""
content = ''
with open(pathfile, mode='rb') as f:
content = f.read()
partBegin = content[0:20]
if partBegin.find(rb'%PDF-1.') < 0:
print('Error: not find %PDF-1.')
return False
idx = content.rfind(rb'%%EOF')
if idx < 0:
print('Error: not find %%EOF')
return False
partEnd = content[(0 if idx-100 < 0 else idx-100) : idx + 5]
if not re.search(rb'startxref\s+\d+\s+%%EOF$', partEnd):
print('Error: not find startxref')
return False
return True
【相关阅读】
The PdfFileReader Class
tempfile — Generate temporary files and directories
io — Core tools for working with streams