Hitesh Roy
Hitesh Roy

Reputation: 337

How to convert JSON data to PDF using python script

I want to convert JSON data to PDF which is getting from API.

example JSON data

{
    "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
        }
     ]   
}

I found this script: http://code.activestate.com/recipes/578979-convert-json-to-pdf-with-python-and-xtopdf/

but getting error

no module PDFWriter

Is there any another way to convert JSON Data PDF.

PLEASE HELP.

Upvotes: 7

Views: 31135

Answers (3)

aj7tt
aj7tt

Reputation: 129

when you hit this code it will generate a pdf for this url (API).

import pdfkit
pdfkit.from_url('https://api.covid19api.com/summary', 'india.pdf')

you can also generate an pdf from a different formats like .file, .html , .text, multiple url

import json
import requests

response = requests.get('https://api.covid19api.com/summary').text

 # loads converts a string to a JSON object
json_object = json.loads(response) 

# json. dumps converts a json object to a string
print(json.dumps(json_object, indent=1))


#different formats 
pdfkit.from_url('http://aj7t.me', 'output.pdf')
pdfkit.from_file('test.html', 'output.pdf')
pdfkit.from_string('Hello!', 'output.pdf')

👍For more information, please check the documentation!

Upvotes: 0

Ankit Gupta
Ankit Gupta

Reputation: 618

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))))
  1. install json2html
  2. install pdfkit (requires wkhtmltox)

Upvotes: 0

ralf htp
ralf htp

Reputation: 9422

the module PDFWriter is in xtopdf

PDFWriter - a core class of the xtopdf toolkit - can now be used with a Python context manager, a.k.a. the Python with statement.

( http://code.activestate.com/recipes/578790-use-pdfwriter-with-context-manager-support/ )

how to install xtopdf is in https://bitbucket.org/vasudevram/xtopdf :

Installation and usage:

To install the files, first make sure that you have downloaded and installed all the prerequisities mentioned above, including setup steps such as adding needed directories to your PYTHONPATH. Then, copy all the files in xtopdf.zip into a directory which is on your PYTHONPATH.

To use any of the Python programs, run the .py file as:

python filename.py

This will give a usage message about the correct usage and arguments expected.

To run the shell script(s), do the same as above.

Developers can look at the source code for further information.

an alternative is to use pdfdocument to create the pdf, it can be installed using pip ( https://pypi.python.org/pypi/pdfdocument )

parse the data from the json data ( How can I parse GeoJSON with Python, Parse JSON in Python ) and print it as pdf using pdfdocument ( https://pypi.python.org/pypi/pdfdocument )

  import json
  data = json.loads(datastring)

from io import BytesIO
from pdfdocument.document import PDFDocument

def say_hello():
    f = BytesIO()
    pdf = PDFDocument(f)
    pdf.init_report()
    pdf.h1('Hello World')
    pdf.p('Creating PDFs made easy.')
    pdf.generate()
    return f.getvalue()

Upvotes: 5

Related Questions