Skip to main content

Python bindings to Rust's genpdf-json library (PDF generator)

Project description

pygenpdf_json

pygenpdf_json uses genpdf-json(https://github.com/numaelis/genpdf-json-rs) thanks to pyo3 and maturin

genpdf-json uses rckive-genpdf and generates a PDF from JSON data.

rckive-genpdf is a user-friendly PDF generator written in pure Rust.

The library can be used in 3 ways:

  1. Search for a JSON file and create a new PDF file in the specified path.
  2. Pass a JSON string and receive a PDF file in Base64 text.
  3. passing a SQLite database

Another way to use it is through the pygenpdf library (https://gitlab.com/numaelis/pygenpdf)

Quick explanation:

import pygenpdf_json
pygenpdf_json.render_json_file("file.json", "file.pdf")

The json file must have config and elements:

{
    "config": {
        "title": "report genpdf", 
        "style": {}, 
        "page_size": "A4", 
        "fonts": [], 
        "default_font": {"font_family_name": "LiberationSans", "dir": "/usr/share/fonts/truetype/liberation"}, 
        "head_page": {"type": "paragraph", "value": [{"text": "report genpdf-rs", "bold": true, "size": 8, "italic": true}], "alignment": "right"}, 
        "margins": [7, 10, 10, 10], "line_spacing": 1.0
        }, 
    "elements": [
                    {
                        "type": "layout", 
                        "orientation": "vertical", 
                        "elements": [
                                        {
                                            "type": "paragraph", 
                                            "value": [{"text": "Invoice", "bold": true, "size": 25}], 
                                            "alignment": "center"}, 
                                        {"type": "break", "value": 1}
                                    ], 
                        "frame": {"thickness": 0.1}, 
                        "padding": [1, 1, 1, 1]
                    }, 
                    {"type": "break", "value": 2}, 
                    {
                        "type": "paragraph", 
                        "value": [{"text": "details", "bold": true, "size": 12}], 
                        "alignment": "center"
                    }                
            ]
}
import pygenpdf_json

string_json = """{
        "config": {
            "title": "report genpdf", 
            "style": {}, 
            "page_size": "A4", 
            "fonts": [], 
            "default_font": {"font_family_name": "LiberationSans", "dir": "/usr/share/fonts/truetype/liberation"}, 
            "head_page": {"type": "paragraph", "value": [{"text": "report genpdf-rs", "bold": true, "size": 8, "italic": true}], "alignment": "right"}, 
            "margins": [7, 10, 10, 10], "line_spacing": 1.0
            }, 
        "elements": [
                        {
                            "type": "layout", 
                            "orientation": "vertical", 
                            "elements": [
                                            {
                                                "type": "paragraph", 
                                                "value": [{"text": "Invoice", "bold": true, "size": 25}], 
                                                "alignment": "center"}, 
                                            {"type": "break", "value": 1}
                                        ], 
                            "frame": {"thickness": 0.1}, 
                            "padding": [1, 1, 1, 1]
                        }, 
                        {"type": "break", "value": 2}, 
                        {
                            "type": "paragraph", 
                            "value": [{"text": "details", "bold": true, "size": 12}], 
                            "alignment": "center"
                        }                
                ]
    }"""

pdf_string_base64 = pygenpdf_json.render_json_base64(string_json)
print(pdf_string_base64)

To use SQLite, the database must have the following structure: CREATE TABLE config ( id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT )

CREATE TABLE elements ( id INTEGER PRIMARY KEY AUTOINCREMENT, element TEXT )

import pygenpdf_json

render_file_from_sqlite("database.db", "output.pdf")

#or

pdf_string_base64 = pygenpdf_json.render_base64_from_sqlite("database.db")

type support

config:

"config":{
    "title":"", 
    "style": style, 
    "page_size": string or [float, float]  -> "A4", "Legal", "Letter", or [200,200]
    "fonts" : [{"font_family_name":"", dir:""}],
    "default_font": {"font_family_name":"", dir:""}
    "line_spacing": float,
    "margins": [float, float, float, float],
    "head_page": paragraph,
    "head_page_count", paragraph,
    "deafault_font_size" int
}

line_style:

 {
    "thickness":float
    "color":color
 }

margins:

    [top, right, bottom, left]

style:

 {
    "bold":bool,
    "italic":bool,
    "font_family": string,
    "color": color,
    "line_spacing": float
 }

alignment:

    "left"
    "center"
    "right"

string_style:

 {
    "text": string
    "bold":bool,
    "italic":bool,
    "font_family": string,
    "color": color,
    "line_spacing": float
 }

color:

    {"type":"rgb", "value":[int, int, int]}
    {"type":"cmyk", "value":[int, int, int, int]}
    {"type":"greyscale", "value":int}

elements

 {
    "type": layout,
    "orientation":"vertical"
    "frame": line_style,
    "style": style,
    "padding": margins,
    "elements": [...elements...]
 }
 
 {
    "type": layout,
    "orientation":"horizontal"
    "column_weights": [],
    "frame": line_style,
    "style": style,
    "padding": margins,
    "elements": [...elements column_weights...] -> 
 }
 
 {
    "type": table_layout,
    "frame_decorator":[[inner(bool), outer(bool), cont(bool)], line_style]
    "column_weights": [],
    "frame": line_style,
    "style": style,
    "padding": margins,
    "rows": [...row column_weights...] -> 
 }
 
 {
    "type": unordered_list,
    "frame": line_style,
    "style": style,
    "padding": margins,
    "elements": [...elements...]
    "bullet": string
 }
 
 {
    "type": ordered_list,
    "frame": line_style,
    "style": style,
    "padding": margins,
    "elements": [...elements...]
    "start": int
 }
 
 {
    "type": paragraph,
    "frame": line_style,
    "style": style,
    "padding": margins,
    "value": [...string_style...]
    "alignment" alignment
    "bullet": string
 }
 
 {
    "type": image,
    "path": string,
    "base64": string,
    "frame": line_style,    
    "padding": margins,    
    "alignment" alignment
    "position": [float, float],
    "scale": float,
    "rotation", float  +-
    "dpi": float    
 }
 
 {
    "type" : "break", 
    "value": float
 }
 
 {  
    "type" : "page_break"
 }
 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygenpdf_json-0.1.2.tar.gz (3.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygenpdf_json-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

File details

Details for the file pygenpdf_json-0.1.2.tar.gz.

File metadata

  • Download URL: pygenpdf_json-0.1.2.tar.gz
  • Upload date:
  • Size: 3.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for pygenpdf_json-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1c8f865d7cfebf6024e30370f04448586aa9213b7c3128b2602d81f4892a23ac
MD5 a6bf5bd8f08fd924446530f09537065a
BLAKE2b-256 1d0469934d65ff61c7f67096283f80925f8f4a04ce13c2cf617e88d1fc7448e6

See more details on using hashes here.

File details

Details for the file pygenpdf_json-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pygenpdf_json-0.1.2-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 b01fcbf97fcf5b33860d7b8fc99d89f95a963d3756603600841e9b69f173b4d0
MD5 44b9311360bf76cb5218a6e82d6eb9dd
BLAKE2b-256 f295453bd06894071f79bb91dff688cc2a4e4f63779ca50b0be724dcaf7bac49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page