MCPcopy Create free account
hub / github.com/ActiveState/code / JSONtoPDF

Function JSONtoPDF

recipes/Python/578979_Convert_JSPDF/recipe-578979.py:9–32  ·  view source on GitHub ↗
(json_data)

Source from the content-addressed store, hash-verified

7 sys.exit(1)
8
9def JSONtoPDF(json_data):
10 # Get the data values from the JSON string json_data.
11 try:
12 data = json.loads(json_data)
13 pdf_filename = data['pdf_filename']
14 font_name = data['font_name']
15 font_size = data['font_size']
16 header = data['header']
17 footer = data['footer']
18 lines = data['lines']
19 except Exception as e:
20 error_exit("Invalid JSON data: {}".format(e.message))
21 # Generate the PDF using the data values.
22 try:
23 with PDFWriter(pdf_filename) as pw:
24 pw.setFont(font_name, font_size)
25 pw.setHeader(header)
26 pw.setFooter(footer)
27 for line in lines:
28 pw.writeLine(line)
29 except IOError as ioe:
30 error_exit("IOError while generating PDF file: {}".format(ioe.message))
31 except Exception as e:
32 error_exit("Error while generating PDF file: {}".format(e.message))
33
34def testJSONtoPDF():
35 fil = open('the-man-in-the-arena.txt')

Callers 1

testJSONtoPDFFunction · 0.85

Calls 5

setFontMethod · 0.80
writeLineMethod · 0.80
error_exitFunction · 0.70
loadsMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected