File size: 1,560 Bytes
33c0070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.graphics.shapes import Drawing, Line
import re

def generate_pdf(data):
    """Generate a PDF file from the json essay data"""
    title = re.sub(r'[^a-zA-Z0-9]', '_', data['header'])
    filename = title + '.pdf'
    pdf = SimpleDocTemplate(filename, pagesize=A4, title=title)

    styles = getSampleStyleSheet()
    story = []
    line = Drawing(450, 1)
    line.add(Line(0, 0, 450, 0))

    header = Paragraph(data['header'], styles['Title'])
    story.append(header)
    story.append(Spacer(1, 6))

    entry = Paragraph(data['entry'], styles['BodyText'])
    story.append(entry)
    story.append(Spacer(1, 6))

    for para in data['paragraphs']:
        sub_header = Paragraph(para['sub_header'], styles['Heading2'])
        paragraph = Paragraph(para['paragraph'], styles['BodyText'])

        story.append(sub_header)
        story.append(Spacer(1, 3))
        story.append(paragraph)
        story.append(Spacer(1, 6))

    conclusion_title = Paragraph('Conclusion', styles['Heading2'])
    conclusion = Paragraph(data['conclusion'], styles['BodyText'])
    story.append(conclusion_title)
    story.append(conclusion)
    story.append(Spacer(1, 6))

    story.append(line)
    story.append(Spacer(1, 3))
    seo_keywords = Paragraph("Seo Keywords: "+", ".join( data['seo_keywords']), styles['BodyText'])
    story.append(seo_keywords)
    pdf.build(story)

    return filename