0

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
Nursk
  • 1
  • 2
  • Can you post more of your code? – tim the fiend Mar 22 '23 at 10:01
  • included the function with that leads to the error, before the function is run the doucment is only initialized with the self.doc = fitz.open(filepath) – Nursk Mar 22 '23 at 10:09
  • I think you should consider changing your code so you only open the PDF when you need it, considering that you have to close the document yourself. – tim the fiend Mar 22 '23 at 10:31
  • @Nursk Please use the [issue reporting site](https://github.com/pymupdf/PyMuPDF/issues) of the package! As an immediate help, please use `for spage in doc` instead of `for spage in doc.pages()`. – Jorj McKie Mar 22 '23 at 10:33
  • @Nursk if you see message "cycle in page tree" then you must have "passed" an exception when you tried to load a non-existent page. – Jorj McKie Mar 22 '23 at 11:37

0 Answers0