Create pdf_writer.py
Browse files- pdf_writer.py +47 -0
pdf_writer.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from reportlab.lib.pagesizes import A4
|
2 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
3 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
4 |
+
from reportlab.graphics.shapes import Drawing, Line
|
5 |
+
import re
|
6 |
+
|
7 |
+
def generate_pdf(data):
|
8 |
+
"""Generate a PDF file from the json essay data"""
|
9 |
+
title = re.sub(r'[^a-zA-Z0-9]', '_', data['header'])
|
10 |
+
filename = title + '.pdf'
|
11 |
+
pdf = SimpleDocTemplate(filename, pagesize=A4, title=title)
|
12 |
+
|
13 |
+
styles = getSampleStyleSheet()
|
14 |
+
story = []
|
15 |
+
line = Drawing(450, 1)
|
16 |
+
line.add(Line(0, 0, 450, 0))
|
17 |
+
|
18 |
+
header = Paragraph(data['header'], styles['Title'])
|
19 |
+
story.append(header)
|
20 |
+
story.append(Spacer(1, 6))
|
21 |
+
|
22 |
+
entry = Paragraph(data['entry'], styles['BodyText'])
|
23 |
+
story.append(entry)
|
24 |
+
story.append(Spacer(1, 6))
|
25 |
+
|
26 |
+
for para in data['paragraphs']:
|
27 |
+
sub_header = Paragraph(para['sub_header'], styles['Heading2'])
|
28 |
+
paragraph = Paragraph(para['paragraph'], styles['BodyText'])
|
29 |
+
|
30 |
+
story.append(sub_header)
|
31 |
+
story.append(Spacer(1, 3))
|
32 |
+
story.append(paragraph)
|
33 |
+
story.append(Spacer(1, 6))
|
34 |
+
|
35 |
+
conclusion_title = Paragraph('Conclusion', styles['Heading2'])
|
36 |
+
conclusion = Paragraph(data['conclusion'], styles['BodyText'])
|
37 |
+
story.append(conclusion_title)
|
38 |
+
story.append(conclusion)
|
39 |
+
story.append(Spacer(1, 6))
|
40 |
+
|
41 |
+
story.append(line)
|
42 |
+
story.append(Spacer(1, 3))
|
43 |
+
seo_keywords = Paragraph("Seo Keywords: "+", ".join( data['seo_keywords']), styles['BodyText'])
|
44 |
+
story.append(seo_keywords)
|
45 |
+
pdf.build(story)
|
46 |
+
|
47 |
+
return filename
|