I am using Fitz in python for working with pdf documents, one document i have sometimes get an RunTimeError and other times it doesnt when i iterate over the pages. When i apply a try-except clause for it, the program just stops when it encounters the error?
the line leading to the error:
self.doc = fitz.open(filepath)
for spage in self.doc.pages()
So i dont understand why it stops and not continues? And how the error only occurs sometimes, when its the exact same pdf document?
I feel like i am missing something obvious, apreciate any help,
File "C:\Users\mikae\Desktop\takstrapport\v2\backend\main\management\engine\takst.py", line 57, in adjust_pdf
for spage in self.doc.pages():
File "C:\Users\mikae\anaconda3\envs\Eiendom\lib\site-packages\fitz\fitz.py", line 5500, in pages
yield (self.load_page(pno))
File "C:\Users\mikae\anaconda3\envs\Eiendom\lib\site-packages\fitz\fitz.py", line 4002, in load_page
val = _fitz.Document_load_page(self, page_id)
RuntimeError: cycle in page tree
def adjust_pdf(self, rotate=False):
new_pdf = fitz.Document()
A4_aspect_ratio = 1.414
adjusted = False
rotate = False
r = 0
#try:
for spage in self.doc.pages():
bbox = spage.rect
width = (bbox[2] - bbox[0])
height = (bbox[3] - bbox[1])
page_aspect_ratio = height / width
# Detect Rotation
#if round(width / height, 3) == A4_aspect_ratio and rotate:
if rotate:
adjusted = True
rotate = True
else:
# Detect multiple pages on one page:
if width > height:
bbox_list = []
# Detect 2x pages
if round(page_aspect_ratio, 1) == 0.6 or round(page_aspect_ratio, 1) == 0.7:
bbox_list.append(fitz.Rect((bbox[0], bbox[1]), (bbox[2]/2, bbox[3])))
bbox_list.append(fitz.Rect((bbox[2]/2, bbox[1]), (bbox[2], bbox[3])))
# Detect 4x pages
elif round(page_aspect_ratio, 1) == 0.3 or round(page_aspect_ratio, 1) == 0.4:
bbox_list.append(fitz.Rect((bbox[0], bbox[1]), (bbox[2]/4, bbox[3])))
bbox_list.append(fitz.Rect((bbox[2]/4, bbox[1]), (bbox[2]/4*2, bbox[3])))
bbox_list.append(fitz.Rect((bbox[2]/4*2, bbox[1]), (bbox[2]/4*3, bbox[3])))
bbox_list.append(fitz.Rect((bbox[2]/4*3, bbox[1]), (bbox[2], bbox[3])))
d = fitz.Rect(spage.cropbox_position, spage.cropbox_position)
for rx in bbox_list:
rx += d # add the CropBox displacement
page = new_pdf.new_page(-1, width = rx.width, height = rx.height)
page.show_pdf_page(page.rect, self.doc, spage.number, clip = rx)
r += 1
adjusted = True
continue
try:
page = new_pdf.new_page(-1, width = width, height = height)
page.show_pdf_page(page.rect, self.doc, spage.number)
if rotate:
page.set_rotation(90)
except ValueError:
pass # If value error, the page is empty and will not be included
r += 1
rotate = False
# If changes were made replace the doc with the adjusted pdf
if adjusted:
#new_pdf.save("poster.pdf", garbage=3, deflate=True)
self.doc = new_pdf