You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyPDF2处理单页PDF字节串遇流错误,Google Document AI无输出求助

特定单页PDF转换字节串列表的异常与Google Document AI无输出问题
  • 异常表现:处理某特定单页PDF时抛出错误:Invalid stream (index 0) within object 14 0: Stream has ended unexpectedly
  • 矛盾现象:输出字节串转存为PDF后可正常打开,但Google Document AI处理该字节串列表时无任何输出
  • 影响范围:仅该特定PDF出现问题,其他PDF处理均正常

相关函数代码:

from PyPDF2 import PdfReader, PdfWriter
import io
from typing import List

def pdf_to_list(byte_string: bytes) -> List[bytes]:
    pdf_pages = []
    with io.BytesIO(byte_string) as stream:
        pdf = PdfReader(stream, strict = False)
        num_pages = len(pdf.pages)
        for page_number in range(num_pages):
            pdf_writer = PdfWriter()
            pdf_writer.add_page(pdf.pages[page_number])
            output_stream = io.BytesIO()
            pdf_writer.write(output_stream)
            output_stream.seek(0)
            pdf_pages.append(output_stream.read())
    return pdf_pages

def save_bytestring_as_pdf(bytestring: bytes, file_path: str) -> None:
    with open(file_path, 'wb') as file:
        file.write(bytestring)
    print(f'Bytestring saved as PDF: {file_path}')

我可提供该问题PDF用于测试,求问问题原因及解决方法。

内容的提问来源于stack exchange,提问作者Atilio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 18:48:13