Как без ошибок добавить номера страниц в pdf с помощью pdfrw - PullRequest
0 голосов
/ 30 мая 2020

Это мой код, и он может назначать номер страницы некоторым PDF-файлам, в то время как с другими PDF-файлами он возвращает ошибку

from reportlab.pdfgen.canvas import Canvas
from pdfrw import PdfReader
from pdfrw.toreportlab import makerl
from pdfrw.buildxobj import pagexobj

input_file = "ew.pdf"
output_file = "my_file.pdf"
reader = PdfReader(input_file)
pages = [pagexobj(p) for p in reader.pages]
canvas = Canvas(output_file)
for page_num, page in enumerate(pages, start=1):
    canvas.setPageSize((page.BBox[2], page.BBox[3]))
    canvas.doForm(makerl(canvas, page))
    footer_text = "Page %s of %s" % (page_num, len(pages))
    x = 128
    canvas.saveState()
    canvas.setFont('Times-Roman', 10)
    canvas.drawString(page.BBox[2]-x, 35, footer_text)
    canvas.restoreState()
    canvas.showPage()
canvas.save()

Я не смог понять эту ошибку.

  File "D:\Python\PyQt5\Add page numbers - reportlab", line 8, in <module>
reader = PdfReader(input_file)
  File "C:\Users\Links Net\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pdfrw \pdfreader.py", line 619, in __init__
trailer, is_stream = self.parsexref(source)
File "C:\Users\Links Net\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pdfrw\pdfreader.py", line 457, in parsexref
return self.parse_xref_stream(source), True
File "C:\Users\Links Net\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pdfrw\pdfreader.py", line 367, in parse_xref_stream
source.exception('Expected dict type of /XRef')
File "C:\Users\Links Net\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pdfrw\tokens.py", line 229, in exception
raise PdfParseError(self.msg(*arg))
pdfrw.errors.PdfParseError: Expected dict type of /XRef (line=385, col=1, token='>>')
...