MatthiasValvekens / pyHanko

pyHanko: sign and stamp PDF files
MIT License
483 stars 71 forks source link

'int' object has no attribute 'value' on sign_pdf #247

Closed ilpadrinohack closed 1 year ago

ilpadrinohack commented 1 year ago

Hi Matthias and everybody:

With the update of pyHanko from 0.13 o the latest one, I have a problem when trying to sign a pdf document. This is my code:

     `w = IncrementalPdfFileWriter(BytesIO(MINIMAL))
      fname = fname.replace('.pdf', '-f.pdf')
                with open(fname, 'wb') as outf:

                    out = signers.PdfSigner(signature_meta=signers.PdfSignatureMetadata(
                                                    field_name='Sigdni',
                                                    reason='Firma',
                                                    location='',
                                                    use_pades_lta=True,
                                                    subfilter=fields.SigSeedSubFilter.PADES,
                                                    embed_validation_info=True,
                                                    validation_context=vc
                                                    ),
                                    signer=signerp,
                                    timestamper=None,
                                    stamp_style=style,
                                    new_field_spec=sp
                                    ).sign_pdf(w,output=outf)`

And the error is "'int' object has no attribute 'value'" I think (not sure) that the problem is on the _signpdf function.

If I change my code calling in a different way to IncrementaPdfFileWriter function as follow:

    `fname = fname.replace('.pdf', '-f.pdf')
                with open(fname, 'wb') as outf:
                    w = IncrementalPdfFileWriter(outf)

                    out = signers.PdfSigner(signature_meta=signers.PdfSignatureMetadata(
                                                    field_name='Sigdni',
                                                    reason='Firma',
                                                    location='',
                                                    use_pades_lta=True,
                                                    subfilter=fields.SigSeedSubFilter.PADES,
                                                    embed_validation_info=True,
                                                    validation_context=vc
                                                    ),
                                    signer=signerp,
                                    timestamper=None,
                                    stamp_style=style,
                                    new_field_spec=sp
                                    ).sign_pdf(w)`

I get the error "Illegal PDF header" I have been checking the documentation and haven't been able to notice any change in the implementation in the sign_pdf call.

Any help will be appreciated. Thanks in advance

MatthiasValvekens commented 1 year ago

Hi @ilpadrinohack,

The fact that your second example doesn't work is to be expected: the stream passed into the IncrementalPdfFileWriter needs to contain the "base" file. You're invoking it on an empty stream.

As for the first example: please post the full stack trace. It's somewhat likely that the error you're seeing was present all along, but surfaced only now due to various deep changes in the validation code. Whether it's a data correctness issue or a bug is impossible to tell without a stack trace.