News for hackers. Stuff that matters.
Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by xupybd on Thursday September 12, 2024 @07:55PM (#41526888)
I think this is great but you might need to think about your point of difference. I suspect you have a product here but it might need some refining.

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")
Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by its_down_again on Thursday September 12, 2024 @08:14PM (#41526984)
That's awesome! Mind sharing the prompt you used? I'm working on something pretty similar but a little more complex—looking to convert from an Excel sheet to Markdown or JSON.
Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by xupybd on Thursday September 12, 2024 @08:21PM (#41527019)
Sure, here is the prompt.

Can you write me a program that can generate formatted PDFs from JSON? Preferably using the simplest of tools

Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by rovr138 on Thursday September 12, 2024 @08:29PM (#41527048)
If it was just that, I'd probably load onto a pandas df, then export the df to json. There's other ways, but this would be a simple enough way.

Check,

- https://pandas.pydata.org/pandas-docs/stable/reference/api/p...

- https://pandas.pydata.org/pandas-docs/stable/reference/api/p...

Try something like,

    import pandas as pd

    file_path = 'excel_file.xlsx'
    df = pd.read_excel(file_path, sheet_name='WORKSHEET NAME')
    df.to_json('output.json', orient='records', indent=4)
Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by rovr138 on Thursday September 12, 2024 @08:23PM (#41527027)
Not really. The code, as is, doesn't run.

`data` isn't defined. It missed loading the json into it.

Edit here,

I've been testing some models today. It's a great solution, easy to fix. I like the package it chose.

My head is simply on evaluating things and code. Criticizing the model and output, not directed at you.

Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by xupybd on Friday September 13, 2024 @07:51AM (#41530307)
It ran for me?
Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by xupybd on Friday September 13, 2024 @07:52AM (#41530322)
Admittedly I didn't look at the code but that was the point. To see how little effort I could put in to get a pdf from my JSON. I suspect tons of bugs but it worked for my test.
Re:Show HN: SnazzyPDF – Convert Any JSON Data to Beautifully... by patafemma on Friday September 13, 2024 @05:30AM (#41529539)
Thanks for the feedback! You're absolutely right: simple cases like rendering lists, tables or key-value pairs are straightforward and can be done with many different tools without much effort. But the devil is in the details.

Where it gets tricky is handling more complex data structures: deeply nested objects and arrays, different date formats, varying element types and adapting the layout based on the structure. SnazzyPDF's layout engine automates those decisions. The configuration options are still limited but that's what I'm focusing on improving next.