- 安装 json2html
- 安装 pdfkit(需要 wkhtmltox) ``` from json2html import * import json import tempfile
class PdfConverter(object):
def __init__(self):
pass
def to_html(self, json_doc):
return json2html.convert(json=json_doc)
def to_pdf(self, html_str):
return pdfkit.from_string(html_str, None)
def main(): stowflw = { “data”: [ { “state”: “Manchester”, “quantity”: 20 }, { “state”: “Surrey”, “quantity”: 46 }, { “state”: “Scotland”, “quantity”: 36 }, { “state”: “Kent”, “quantity”: 23 }, { “state”: “Devon”, “quantity”: 43 }, { “state”: “Glamorgan”, “quantity”: 43 } ] }
pdfc = PdfConverter()
with open("sample.pdf", "wb") as pdf_fl:
pdf_fl.write(pdfc.to_pdf(pdfc.to_html(json.dumps(stowflw))))
```