Advertisement
Python253

json2pdf

Mar 14th, 2024
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: json2pdf.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a JSON file (.json) to a PDF file (.pdf).
  10. It reads the JSON file and generates a PDF with the specified 'text_content'.
  11.  
  12. Requirements:
  13. - Python 3.x
  14. - FPDF library (install using: pip install fpdf)
  15.  
  16. Usage:
  17. 1. Save this script as 'json2pdf.py'.
  18. 2. Ensure your JSON file ('example.json') is in the same directory as the script.
  19. 3. Install the FPDF library using the command: 'pip install fpdf'
  20. 4. Run the script.
  21. 5. The converted PDF file ('json2pdf.pdf') will be generated in the same directory.
  22.  
  23. Note: Adjust the 'json_filename' and 'pdf_filename' variables in the script as needed.
  24. """
  25.  
  26. from fpdf import FPDF
  27. import json
  28.  
  29. def json_to_pdf(json_filename, pdf_filename):
  30.     pdf = FPDF()
  31.     pdf.add_page()
  32.     pdf.set_font("Arial", size=12)
  33.  
  34.     with open(json_filename, 'r') as jsonfile:
  35.         data = json.load(jsonfile)
  36.         if "text_content" in data:
  37.             pdf.multi_cell(0, 10, data["text_content"])
  38.  
  39.     pdf.output(pdf_filename)
  40.  
  41. if __name__ == "__main__":
  42.     json_filename = 'example.json'
  43.     pdf_filename = 'json2pdf.pdf'
  44.     json_to_pdf(json_filename, pdf_filename)
  45.     print(f"Converted '{json_filename}' to '{pdf_filename}'.")
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement