0

I have the following PDF file. I try to explore it using the following code:

from pdfrw import PdfReader, PdfWriter
from PyPDF2 import PdfFileReader
import subprocess
from endesive import pdf
from pdfreader import PDFDocument
from asn1crypto import cms
import datetime

pdf_link = 'FDA_Form_1572_prefilled_102919-signed.pdf'

# create temporary fixed file to be accessed
pdf_link_temp = 'temp.pdf'
subprocess.run(['qpdf', '--decrypt', pdf_link, pdf_link_temp])

# access created temp file
pdf_data = PdfFileReader(open(pdf_link_temp, "rb"), strict=False)
fields = pdf_data.getFields()

field_db_signature = fields['db_signature']
field_db_signature_contents = fields['db_signature'].value['/Contents']
content_info = cms.ContentInfo.load(field_db_signature_contents)

When I try to access content_info.native in debugging mode, it gives me the following:

{ValueError}Error parsing asn1crypto.x509.EmailAddress - tag should have been 22, but 30 was found
    while parsing asn1crypto.x509.NameTypeAndValue
    while parsing asn1crypto.x509.RelativeDistinguishedName
    while parsing asn1crypto.x509.RDNSequence
    while parsing asn1crypto.x509.TbsCertificate
    while parsing asn1crypto.x509.Certificate
    while parsing asn1crypto.cms.CertificateSet
    while parsing asn1crypto.cms.SignedData
    while parsing asn1crypto.cms.ContentInfo

I notice that if I keep accessing it a few times, it evaluates successfully and the error goes away. Thus, I added this to the code above:

i = 0
while i < 20:
    print(f'i = {i}')
    try:
        content_native = content_info.native
        break
    except Exception as e:
        print(f'Error: {e}')
    i += 1

Without fail, it always prints out the following before content_info.native evaluates successfully:

i = 0
<error printout>
i = 1
<error printout>
i = 2
<error printout>
i = 3

That means it always fails 3 cycles before succeeding. The <error printout> is the same as seen while debugging above. I would like to ask why this is happening. Thank you so much!

CaTx
  • 1,421
  • 4
  • 21
  • 42

0 Answers0