当我使用以下代码时
from PyPDF2 import PdfFileMerger
merge = PdfFileMerger()
for newFile in nlst:
merge.append(newFile)
merge.write('newFile.pdf')发生的事情如下:
raise utils.PdfReadError("EOF marker not found")
PyPDF2.utils.PdfReadError: EOF marker not found有人能告诉我发生了什么吗?谢谢
发布于 2017-07-31 19:53:42
PDF是一种文件格式,pdf解析器通常通过读取位于文件末尾的一些全局信息来开始读取文件。在文档的末尾需要有一行内容
%%EOF%
这是一个标记,pdf解析器知道PDF文档在这里结束,它需要的全局信息应该在此之前( startxref部分)。
我猜,您看到的错误消息意味着其中一个输入文档被截断,并且缺少%%EOF标记。
发布于 2021-02-05 14:11:44
在使用camelot和PyPDF2遇到这个问题后,我做了一些挖掘,并解决了这个问题。
文件末尾标记'%%EOF'应该是最后一行,但是一些PDF文件在该行后面放了一大块javascript,读者找不到EOF。
EOF plus javascript打开时的外观插图:
b'>>\r\n',
b'startxref\r\n',
b'275824\r\n',
b'%%EOF\r\n',
b'\n',
b'\n',
b'<script type="text/javascript">\n',
b'\twindow.parent.focus();\n',
b'</script><!DOCTYPE html>\n',
b'\n',
b'\n',
b'\n',所以你只需要在javascript开始之前截断文件。
解决方案:
def reset_eof_of_pdf_return_stream(pdf_stream_in:list):
# find the line position of the EOF
for i, x in enumerate(txt[::-1]):
if b'%%EOF' in x:
actual_line = len(pdf_stream_in)-i
print(f'EOF found at line position {-i} = actual {actual_line}, with value {x}')
break
# return the list up to that point
return pdf_stream_in[:actual_line]
# opens the file for reading
with open('data/XXX.pdf', 'rb') as p:
txt = (p.readlines())
# get the new list terminating correctly
txtx = reset_eof_of_pdf_return_stream(txt)
# write to new pdf
with open('data/XXX_fixed.pdf', 'wb' as f:
f.writelines(txtx)
fixed_pdf = PyPDF2.PdfFileReader('data/XXX_fixed.pdf')发布于 2020-03-27 10:07:26
这个问题的一个简单解决方案(找不到EOF标记)。在其他应用程序中打开您的.pdf文件(我使用的是Ubuntu18.04中的Libre office draw )。然后将文件导出为.pdf。使用此导出的.pdf文件,问题将不会继续存在。
https://stackoverflow.com/questions/45390608
复制相似问题