omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Issue with flattening PDF

    Pythonista
    3
    21
    10727
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JonB
      JonB last edited by

      Why are you using BytesIO instead of a file?

      outputStream = file(r"output.pdf", "wb")
      pdf_writer.write(outputStream)
      outputStream.close()

      1 Reply Last reply Reply Quote 0
      • CastroSATT
        CastroSATT @cvp last edited by

        @cvp sorry when I said didn’t work I meant it kinda of worked the issue is it made the pdf but still with editable forms and not ready only it’s weird

        cvp 3 Replies Last reply Reply Quote 0
        • cvp
          cvp @CastroSATT last edited by cvp

          @CastroSATT Sorry, I didn't understand correctly your request. If this a problem of filled form, I can't help you. 😢 I'm sure somebody here will help you

          I suppose you got your code from here and here

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp @CastroSATT last edited by

            @CastroSATT Try this, it should work

                    if writer_annot.get('/T')[:len(field)] == field:
            
            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @CastroSATT last edited by

              @CastroSATT Please, tell me if it is ok for you, and if not, could you post your input pdf.

              1 Reply Last reply Reply Quote 0
              • CastroSATT
                CastroSATT last edited by

                Sorry still no go it makes the pdf but adds my dictionary to the form which is perfect
                I just can’t make the text read only so everyone can see it even In preiview mode

                Template PDF

                1 Reply Last reply Reply Quote 0
                • CastroSATT
                  CastroSATT last edited by

                  So this is a complete template as you can see clean but after processing

                  This is what it looks like

                  Name and first line of address only
                  after processing

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @CastroSATT last edited by

                    @CastroSATT I understand. I did test a little code with another pdf sample, and the output was read only. I'll test...

                    1 Reply Last reply Reply Quote 0
                    • CastroSATT
                      CastroSATT last edited by

                      thanks for the help it’s on my snagging list

                      cvp 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @CastroSATT last edited by cvp

                        @CastroSATT Try this pdf with

                        from PyPDF2 import PdfFileReader, PdfFileWriter
                        from PyPDF2.generic import BooleanObject, NameObject, IndirectObject, NumberObject
                        
                        TEMPLATE_PATH = 'SampleForm-1.pdf'
                        OUTPUT_PATH = 'out1.pdf'
                        data_dict = {
                            'Name':'test',
                            'Surname':'test'
                        }
                        
                        if __name__ == '__main__':
                            input_file = PdfFileReader(open(TEMPLATE_PATH, "rb"))
                        
                            output_file = PdfFileWriter()
                            page = input_file.getPage(0)
                            output_file.addPage(page)
                            output_file.updatePageFormFieldValues(page, data_dict)
                            page = output_file.getPage(0)
                        
                            for j in range(0, len(page['/Annots'])):
                              writer_annot = page['/Annots'][j].getObject()
                              print(writer_annot.get('/T')) # to know what to put in data_dict
                              for field in data_dict: 
                                if writer_annot.get('/T') == None:
                                    continue
                                if writer_annot.get('/T')[:len(field)] == field:
                                    writer_annot.update({
                                        NameObject("/Ff"): NumberObject(1)   # make ReadOnly
                                    })
                        
                            output_stream = open(OUTPUT_PATH, "wb")
                        
                            output_file.write(output_stream)
                            output_stream.close()
                        

                        And you will see that the two fields from data_dict are readonly in out1.pdf

                        In JMA2.pdf,

                              print(writer_annot.get('/T')) # to know what to put in data_dict
                        

                        gives None for the first line, perhaps the problem comes from there ⁉️

                        1 Reply Last reply Reply Quote 0
                        • CastroSATT
                          CastroSATT last edited by

                          Yeah might be gonna download a demo of adobe try and remake the template not sure how to get rid of that none from there thanks so much will post an update in a few I’ve been using pdfscape to make the template and the template source (years ago) has been played with a few time after the years so maybe conflicts there some where but your right

                          Seems to work ok with that test one although would preferred to be uneditable but 1 thing at a time me thinks

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @CastroSATT last edited by cvp

                            @CastroSATT with the example, if you define all fields, the out pdf becomes uneditable

                            data_dict = {
                                'Name':'the',
                                'Surname':'king',
                                'email':'xxxx@yyyy.com',
                                'phone':'1234',
                                'Mobile':'1234',
                                'Street':'5th avenue',
                                'House':'3',
                                'Town':'NY',
                                'Postcode':'1234',
                                'Country':'Usa',
                                'Comments':'no'
                            }
                            
                            1 Reply Last reply Reply Quote 1
                            • CastroSATT
                              CastroSATT last edited by

                              Still having a barney

                              -Tried your original template works
                              -Tried your template altered on 2 other form makers and they then have the issue after that

                              Weird so now gonna
                              Start with a blank form and 1 text box each see what happens then

                              I think the issue has something to do with the NONE

                              -When using pdfscrape I get 1 none (needs more testing)

                              -Using pdfelement 6 pro I get 2 none one and the top 2nd at the end
                              —Maybe something to do with the water mark provided by the software, I am reaching.

                              Both using your working template (which has NO none)

                              cvp 2 Replies Last reply Reply Quote 0
                              • cvp
                                cvp @CastroSATT last edited by

                                @CastroSATT Good luck. I don't think I can help more. Sincerely, I don't know anything about pdf filling forms...

                                1 Reply Last reply Reply Quote 1
                                • cvp
                                  cvp @CastroSATT last edited by

                                  @CastroSATT I have run my little script on your JMA2.pdf and open the output pdf with PDF Expert app of Readdle, and fields are filled and read-only

                                  1 Reply Last reply Reply Quote 0
                                  • JonB
                                    JonB last edited by

                                    Castro, I don't think you ever stated what your actual problem is. are you getting an error? Or is the saved form still editable? Or not filled?
                                    What is your problem with None? Are you trying to fill the form with None?

                                    cvp 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @JonB last edited by

                                      @JonB I think that his problem is that his output pdf is filled but still editable (with pdf editors). The problem of "None" comes from my little script where the first writer_annot.get('/T') gives None... It is not a real problem but with my sample template, there is no such "none" and the output file is filled and not editable. Thus, his post r fees to this None.

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post
                                      Powered by NodeBB Forums | Contributors