How to convert the a .json payload to a .pdf file (python)

Modified on Thu, 6 Jun at 11:26 AM

Summary: 

  • Objective: Use a python script to convert out .json payload files to .pdf.

  • Requirements: 

    • coding platform (i.e., Visual Studio Code, Google Colab, etc)

    • code language: python

    • Libraries: json, fpdf


Example:

import json
from fpdf import FPDF

def json_to_pdf(json_path, pdf_path):
    # Read JSON file
    with open(json_path, 'r') as f:
        data = json.load(f)
    
    # Convert JSON data to a pretty printed string
    json_str = json.dumps(data, indent=4)
    
    # Create a PDF instance
    pdf = FPDF()
    pdf.set_auto_page_break(auto=True, margin=15)
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    
    # Split the string into lines
    lines = json_str.split('\n')
    
    # Add each line to the PDF
    for line in lines:
        pdf.multi_cell(0, 10, line)
    
    # Save the PDF to the specified path
    pdf.output(pdf_path)

# Usage example
json_path = '/Users/FLINKS/Downloads/python/GAD_Sample.json'
pdf_path = '/Users/FLINKS/Downloads/python/GAD_Sample PDF result.pdf'
json_to_pdf(json_path, pdf_path)



Other option - Client Dashboard:
It's also possible to extract a PDF with the KYC, IBV and Transaction data directly from the Client Dashboard.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article