首页 > 解决方案 > 使用 Python 获取 PDF 版本

问题描述

我需要从 PDF 文档中提取 PDF 版本。我尝试了 PDF 矿工,但它仅提供以下信息:

  1. PDF 生成器
  2. 已创建
  3. 修改的
  4. 应用

下面是我试过的代码:

from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument

fp = open("ibs.servlets.pdf", 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
parser.set_document(doc)
if len(doc.info) > 0:
   info = doc.info[0]
   print(info)

除了我可以使用的 pdf miner 之外,还有其他库吗?

标签: pythonpdfminer

解决方案


PDF 版本作为注释存储在 PDF 文件的第一行中。我找不到如何使用 pdfparser 获取此信息,但使用PyPDF2我可以手动检索此信息:

from PyPDF2.pdf import PdfFileReader
doc = PdfFileReader('ibs.servlets.pdf')
doc.stream.seek(0) # Necessary since the comment is ignored for the PDF analysis
print(doc.stream.readline().decode())

输出:

%PDF-1.5


推荐阅读