pygenpdf_json
It is a Fast PDF generator through 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 two ways:
- Search for a JSON file and create a new PDF file in the specified path.
- Pass a JSON string and receive a PDF file in Base64 text.
The JSON can be passed directly or using an sqlite.db file
Another way to use it is through the pygenpdf library (https://gitlab.com/numaelis/pygenpdf)
Options:
pygenpdf_json.render_json_file("file.json", "report.pdf")
file_pdf_base64 = pygenpdf_json.render_json_base64(json_string)
pygenpdf_json.render_file_from_sqlite("file.db", "report.pdf")
file_pdf_base64 = pygenpdf_json.render_base64_from_sqlite("file.db")
The structure of the document is:
{
"config": {...},
"elements": [...elements...]
}
examples:
import pygenpdf_json
pygenpdf_json.render_json_file("file.json", "file.pdf")
The file.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"
}
]
}
example base64:
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)
SQLite: To save the config: In a table called config, use a column named data (Text), and in this column, save a record with the config's JSON. For the elements: In a table called elements, use the id (autoincrement) column and the element (Text) column, and save the elements in each record in the JSON order. Optionally, for table layouts, save the rows in a new table separate from config and elements, with the structure id (autoincrement) and row (Text). Then, reassign the value to the JSON "rows" with the name of the table created.
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, or [paragraph, paragraph, paragraph] maximum 3 paragraphs
"footer_page": [paragraph, paragraph, paragraph] maximum 3 paragraphs
"head_page_count": paragraph,
"header_frame": line_style,
"footer_frame": line_style,
"page_frame": line_style,
"page_frame_width_offset": float, -> A negative value reduces the width
"header_logo" : {"left": image, "right": image}, omit what is not used
"footer_logo" : {"left": image, "right": image}, omit what is not used
"header_column_weights": [1,1,1], in case of using all three (image, paragraphs, image)
"footer_column_weights": [1,1,1], default 1
"deafault_font_size": int,
"skip_warning_overflowed": bool -> Skip the page size exceeded warning when the paragraph exceeds the layout
}
line_style:
{
"thickness":float,
"color":color,
"dash": int, "gap": int, "dash2": int, "gap2": int,
"top": bool, "right": bool, "bottom": bool, "left": bool (only frame),
"background": bool, (only frame)
"background_color": color, (only frame)
"border_radius": float, (only frame)
}
margins:
[top, right, bottom, left] float or int
style:
{
"bold":bool,
"italic":bool,
"font_family_name": string,
"color": color,
"line_spacing": float,
"size": int,
"fit_size_to": int -> auto size to minimum
}
alignment:
"left"
"center"
"right"
string_style:
{
"text": string
"bold":bool,
"italic":bool,
"font_family_name": string,
"color": color,
"line_spacing": float,
"size": int,
"fit_size_to": int -> auto size to minimum
}
color:
{"type":"rgb", "value":[int, int, int]} 0 - 255
{"type":"cmyk", "value":[int, int, int, int]}
{"type":"greyscale", "value":int}
elements
{
"type": layout,
"orientation":"vertical"
"frame": line_style,
"style": style,
"padding": margins,
"orphan": bool,
"position": [float, float],
"elements": [...elements...]
}
{
"type": layout,
"orientation":"horizontal"
"column_weights": [], array of integers
"frame": line_style,
"style": style,
"padding": margins,
"elements": [...elements column_weights.len()...]
}
{
"type": table_layout,
"frame_decorator":[[inner(bool), outer(bool), cont(bool)], line_style]
"column_weights": [], array of integers
"frame": line_style,
"style": style,
"padding": margins,
"rows": [...rows column_weights.len()...]
}
{
"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": text,
"value": string_style,
"orphan": bool,
"position": [float, float]
}
{
"type": image,
"path": string,
"base64": string,
"frame": line_style,
"padding": margins,
"alignment": alignment,
"position": [float, float],
"scale": float,
"rotation": float, +-
"dpi": float,
"source_frame": line_style,
"source_frame_offset": float, Negative decrements
}
{
"type" : "break",
"value": float, Negative values are allowed
}
{
"type" : "page_break"
}
Other important information: If you need a small PDF file size on disk, use light fonts, as they are embedded within the PDF.
Links:
https://github.com/numaelis/genpdf-json-bin
Metadata
Release files for pygenpdf-json 0.1.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pygenpdf_json-0.1.18.tar.gz | 8.2 MB | Without provenance | Details for pygenpdf_json-0.1.18.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygenpdf_json-0.1.18-cp311-cp311-manylinux_2_34_x86_64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.34+ x86-64 | Details for pygenpdf_json-0.1.18-cp311-cp311-manylinux_2_34_x86_64.whl |
Total release size: 11.1 MB
Release files / pygenpdf_json-0.1.18.tar.gz
| Download URL | pygenpdf_json-0.1.18.tar.gz |
|---|---|
| Size | 8.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
767cc1e039a4fbf99d0315bc93969e83379872c22af0bca83d24d4741d5a552f
|
|
BLAKE2b-256 checksum How to use checksums |
48a16900d2fe000d7b43b97318684a373c77027af99f3a7e0737091b074023f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / pygenpdf_json-0.1.18-cp311-cp311-manylinux_2_34_x86_64.whl
| Download URL | pygenpdf_json-0.1.18-cp311-cp311-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 2.9 MB |
| Tags | CPython 3.11 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
c92298bf8cc1d33050315024d4e80f9442be73b8ee1a890a742be2ae8234cc5e
|
|
BLAKE2b-256 checksum How to use checksums |
63973ecd0acd9e34b44357980aa583477b545ef8ccbdea75ea2ea7dd950c0c60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Security for pygenpdf-json 0.1.18
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.