This pdfrw code overwrites the pdf pages, but I am not sure why?

48 Views Asked by At

I input a dictionary with 5 rows, so I get 5 pdf pages. When printing the pages I get different pdf content, however in the final pdf I always get 5 pages with content from the last row. Even printing the page just before addpage gives 5 different pages, but final sendfile gives 5 identical pages...

for value in values:
            i = i + 1
            p = 0
            ppage = ''
            for page in pdf.pages:
                if p == 0:
                    print(i)
                    print(p)
                    p = 1
                    annotations = page['/Annots']
                    for annotation in annotations:
                        if annotation['/Subtype']=='/Widget':
                            if not annotation['/T']:
                                annotation=annotation['/Parent']
                                #print (annotation)
                                if annotation['/T']:
                                    #print(annotation['/T'])
                                    #print(type(annotation['/T']))
                                    key = annotation['/T'].strip('()')
                                    mkey = ''.join(filter(lambda z: not z.isdigit(), key))
                                    
                                    if mkey==text_function:
                                        annotation.update(pdfrw.PdfDict(V='{}'.format(value['function']),Ff=ff))
                                        annotation.update(pdfrw.PdfDict(T=(mkey+str(i))))
                                        print(value['function'])
                                    if mkey==text_name:
                                        annotation.update(pdfrw.PdfDict(V='{}'.format(value['name']),Ff=ff))
                                        annotation.update(pdfrw.PdfDict(T=(mkey+str(i))))
                                        print(value['name'])
                                    if mkey==text_desc:
                                        text = ''
                                        print(str(value['desc']))
                                        if pd.isnull(str(value['desc'])) or str(value['desc'])=='nan':
                                            text = str(value['text']) + "\n"
                                        else:
                                            text = str(value['text']) + "\n" + str(value['desc'])
                                        annotation.update(pdfrw.PdfDict(V='{}'.format(text),Ff=ff))
                                        annotation.update(pdfrw.PdfDict(T=(mkey+str(i))))
            ppage = page
            
            print(ppage)
### It works until this point
### somehow adding pages or writing corrupts other pages?
            result = pdfs.addpage(ppage)
            
        buf = io.BytesIO()
        pdfs.write(buf)
        buf.seek(0)
        reopen = pdfrw.PdfReader(buf)
        
        reopen.Root.AcroForm = pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))
        buf = io.BytesIO()
        pdfrw.PdfWriter().write(buf, reopen)
        buf.seek(0)

        return send_file(buf, as_attachment=True, download_name= str(filename) + ".pdf", mimetype='application/pdf')

I do not see a reason really why it gives me 5 identical pages, while print(ppage) gives 5 different page content???

0

There are 0 best solutions below