Как я могу правильно загрузить содержимое json в объект FPDF?

#python #pdf-generation #amazon-emr #amazon-s3-bucket #pyfpdf

Вопрос:

Я работаю над строками, подобными приведенным ниже:

 my_string = '{"amountexvat":"0.14","amountincvat":"0.15","currency":"EUR","paymentmethods":[{"details":{"acquirerapprovalcode":"037464","acquirerid":"A0000000031010","acquirerpoiid":"gyytfft55","adyentransactionid":"1234","adyentransactiontimestamp":"2021-10-08T15:28:07.000Z","cardname":"kll","cardtype":"hhgh","entrymodes":["ICC"],"flourpaymentreference":"889999","merchantid":"888","paymentbrand":"test","paymentvariant":"visastandardcredit","poitransactionid":"12345","poitransactionreconciliationid":"1000","poitransactiontimestamp":"2021-10-08T17:28:07.000Z","terminalid":"866777","transactionamount":0.15,"transactioncurrency":"EUR"},"type":"ec"}],"products":[{"amount":1,"baseprice":"0.15","discount":"0","discountedprice":null,"itemvat":"0.01","pricepaidexvat":"0.14","pricepaidincvat":"0.15","unit":"piece","vendorchangedprice":true}],"salevat":"0.01"}'
 

С другой стороны, у меня есть объект FPDF (для создания некоторого отчета). Я хотел бы загрузить представление json из my_string объекта FPDF.

 import fpdf

pdf = FPDF()

#Cover page
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(175, 10, 'TEST - report',0,1,'C')
 

Ты знаешь, как с этим справиться?

Вид Json из my_string я имею в виду это:

введите описание изображения здесь

Я использую ноутбук jupyter на EMR AWS и попытался сохранить файл json (на основе my_string ) в хранилище S3, позже открыть json в режиме чтения и вставить текст из этого файла в pdf.

 # save json to file in bucket
s3 = boto3.client('s3')
json_object = my_string
s3.put_object(
     Body=json.dumps(json_object),
     Bucket='my_bucket',
     Key='my_key'
)

# open the json file in read mode
s3client = boto3.client('s3')
 
# create a file object using the bucket and object key
fileobj = s3client.get_object(
    Bucket='my_bucket',
    Key='my_key'
    ) 

# open the file object and read it into the variable filedata
filedata = fileobj['Body'].read()

# decode file
contents = filedata.decode('latin-1') 

# insert the texts in pdf
for x in contents:
    pdf.cell(200, 10, txt = x, ln = 1, align = 'C')
 

Unfortunately it does not work as I would like (the text is saved in the pdf report but it is not in json format). Do you have an idea how to properly write json’s contents to PFDF object?

Thanks for any ideas.