I asked Claude to create me something to do this. It worked first go.
import json from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib import colors
def create_pdf_from_json(json_file, pdf_file): # Read JSON data with open(json_file, 'r') as file: data = json.load(file)
# Create PDF document
doc = SimpleDocTemplate(pdf_file, pagesize=letter)
elements = []
# Add title
styles = getSampleStyleSheet()
elements.append(Paragraph("JSON Data Report", styles['Title']))
elements.append(Paragraph("\n", styles['Normal']))
# Create table data
table_data = [["Key", "Value"]]
for key, value in data.items():
table_data.append([str(key), str(value)])
# Create table
table = Table(table_data, colWidths=[200, 300])
table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 14),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('TEXTCOLOR', (0, 1), (-1, -1), colors.black),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 1), (-1, -1), 12),
('TOPPADDING', (0, 1), (-1, -1), 6),
('BOTTOMPADDING', (0, 1), (-1, -1), 6),
('GRID', (0, 0), (-1, -1), 1, colors.black)
]))
elements.append(table)
# Build PDF
doc.build(elements)
if __name__ == "__main__":
create_pdf_from_json("input.json", "output.pdf")