pdfgen-juanipis 0.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdfgen/__init__.py +17 -0
- pdfgen/api.py +69 -0
- pdfgen/assets/banner-clean.png +0 -0
- pdfgen/assets/banner.png +0 -0
- pdfgen/assets/fonts/BCDEEE_Calibri_5.ttf +0 -0
- pdfgen/assets/fonts/BCDFEE_CenturyGothic-Bold_9.ttf +0 -0
- pdfgen/assets/fonts/BCDGEE_CenturyGothic-Bold_14.ttf +0 -0
- pdfgen/assets/fonts/BCDHEE_Calibri-Bold_20.ttf +0 -0
- pdfgen/assets/fonts/BCDIEE_Calibri-Bold_25.ttf +0 -0
- pdfgen/assets/fonts/BCDJEE_Calibri_27.ttf +0 -0
- pdfgen/assets/fonts/BCDKEE_Calibri-Italic_33.ttf +0 -0
- pdfgen/assets/fonts/BCDLEE_Calibri-Italic_52.ttf +0 -0
- pdfgen/assets/fonts/BCDMEE_SegoeUI_54.ttf +0 -0
- pdfgen/assets/fonts/BCDNEE_SegoeUI_60.ttf +0 -0
- pdfgen/assets/fonts/BCDOEE_Aptos Narrow,Bold_142.ttf +0 -0
- pdfgen/assets/fonts/BCDPEE_Aptos Narrow,Bold_144.ttf +0 -0
- pdfgen/assets/fonts/BCEAEE_Aptos Narrow_149.ttf +0 -0
- pdfgen/assets/fonts/BCEBEE_Aptos Narrow_154.ttf +0 -0
- pdfgen/assets/fonts/TimesNewRomanPS-BoldMT_38.ttf +0 -0
- pdfgen/assets/logo.png +0 -0
- pdfgen/cli.py +106 -0
- pdfgen/pagination.py +1045 -0
- pdfgen/render.py +348 -0
- pdfgen/schema.json +126 -0
- pdfgen/templates/boletin.css +389 -0
- pdfgen/templates/boletin_template.html.jinja +129 -0
- pdfgen/validator.py +247 -0
- pdfgen_juanipis-0.1.3.dist-info/METADATA +170 -0
- pdfgen_juanipis-0.1.3.dist-info/RECORD +33 -0
- pdfgen_juanipis-0.1.3.dist-info/WHEEL +5 -0
- pdfgen_juanipis-0.1.3.dist-info/entry_points.txt +2 -0
- pdfgen_juanipis-0.1.3.dist-info/licenses/LICENSE +21 -0
- pdfgen_juanipis-0.1.3.dist-info/top_level.txt +1 -0
pdfgen/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""PDF generation package."""
|
|
2
|
+
|
|
3
|
+
from pdfgen.render import render_pdf, build_sample_data
|
|
4
|
+
from pdfgen.validator import validate_and_normalize
|
|
5
|
+
from pdfgen.pagination import LayoutConfig, Paginator
|
|
6
|
+
from pdfgen.api import PDFGen, PDFGenConfig, render_with_defaults
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"render_pdf",
|
|
10
|
+
"build_sample_data",
|
|
11
|
+
"validate_and_normalize",
|
|
12
|
+
"LayoutConfig",
|
|
13
|
+
"Paginator",
|
|
14
|
+
"PDFGen",
|
|
15
|
+
"PDFGenConfig",
|
|
16
|
+
"render_with_defaults",
|
|
17
|
+
]
|
pdfgen/api.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import pathlib
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Any, Dict, Optional
|
|
4
|
+
|
|
5
|
+
from pdfgen.render import render_pdf
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class PDFGenConfig:
|
|
10
|
+
root_dir: pathlib.Path
|
|
11
|
+
template_dir: pathlib.Path
|
|
12
|
+
css_path: pathlib.Path
|
|
13
|
+
fonts_conf: Optional[pathlib.Path]
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def from_root(cls, root_dir: pathlib.Path) -> "PDFGenConfig":
|
|
17
|
+
root_dir = pathlib.Path(root_dir)
|
|
18
|
+
package_root = pathlib.Path(__file__).resolve().parent
|
|
19
|
+
template_dir = root_dir / "template"
|
|
20
|
+
if not template_dir.exists():
|
|
21
|
+
template_dir = package_root / "templates"
|
|
22
|
+
css_path = template_dir / "boletin.css"
|
|
23
|
+
fonts_conf = root_dir / "fonts.conf"
|
|
24
|
+
if not fonts_conf.exists():
|
|
25
|
+
fonts_conf = None
|
|
26
|
+
return cls(
|
|
27
|
+
root_dir=root_dir,
|
|
28
|
+
template_dir=template_dir,
|
|
29
|
+
css_path=css_path,
|
|
30
|
+
fonts_conf=fonts_conf,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class PDFGen:
|
|
35
|
+
def __init__(self, config: PDFGenConfig):
|
|
36
|
+
self.config = config
|
|
37
|
+
|
|
38
|
+
def render(
|
|
39
|
+
self,
|
|
40
|
+
data: Dict[str, Any],
|
|
41
|
+
output_path: pathlib.Path,
|
|
42
|
+
paginate: bool = True,
|
|
43
|
+
validate: bool = True,
|
|
44
|
+
css_extra: Optional[str] = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
render_pdf(
|
|
47
|
+
data,
|
|
48
|
+
output_path=output_path,
|
|
49
|
+
paginate=paginate,
|
|
50
|
+
validate=validate,
|
|
51
|
+
css_extra=css_extra,
|
|
52
|
+
template_dir=self.config.template_dir,
|
|
53
|
+
css_path=self.config.css_path,
|
|
54
|
+
fonts_conf=self.config.fonts_conf,
|
|
55
|
+
root_dir=self.config.root_dir,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def render_with_defaults(
|
|
60
|
+
data: Dict[str, Any],
|
|
61
|
+
output_path: pathlib.Path,
|
|
62
|
+
root_dir: Optional[pathlib.Path] = None,
|
|
63
|
+
paginate: bool = True,
|
|
64
|
+
validate: bool = True,
|
|
65
|
+
css_extra: Optional[str] = None,
|
|
66
|
+
) -> None:
|
|
67
|
+
root = pathlib.Path(root_dir) if root_dir else pathlib.Path.cwd()
|
|
68
|
+
config = PDFGenConfig.from_root(root)
|
|
69
|
+
PDFGen(config).render(data, output_path, paginate=paginate, validate=validate, css_extra=css_extra)
|
|
Binary file
|
pdfgen/assets/banner.png
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
pdfgen/assets/logo.png
ADDED
|
Binary file
|
pdfgen/cli.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
import pathlib
|
|
4
|
+
import sys
|
|
5
|
+
import tempfile
|
|
6
|
+
|
|
7
|
+
from pdfgen.api import PDFGen, PDFGenConfig
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _build_fonts_conf(fonts_dir: pathlib.Path) -> pathlib.Path:
|
|
11
|
+
fonts_dir = pathlib.Path(fonts_dir).resolve()
|
|
12
|
+
content = f"""<?xml version=\"1.0\"?>
|
|
13
|
+
<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">
|
|
14
|
+
<fontconfig>
|
|
15
|
+
<dir>{fonts_dir}</dir>
|
|
16
|
+
<cache>
|
|
17
|
+
<dir>~/.cache/fontconfig</dir>
|
|
18
|
+
</cache>
|
|
19
|
+
</fontconfig>
|
|
20
|
+
"""
|
|
21
|
+
tmp = tempfile.NamedTemporaryFile("w", delete=False, suffix=".conf")
|
|
22
|
+
tmp.write(content)
|
|
23
|
+
tmp.flush()
|
|
24
|
+
tmp.close()
|
|
25
|
+
return pathlib.Path(tmp.name)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _load_data(path: pathlib.Path, fmt: str | None = None):
|
|
29
|
+
if str(path) == "-":
|
|
30
|
+
raw = sys.stdin.read()
|
|
31
|
+
inferred = fmt or "yaml"
|
|
32
|
+
else:
|
|
33
|
+
raw = path.read_text(encoding="utf-8")
|
|
34
|
+
inferred = fmt or path.suffix.lower().lstrip(".")
|
|
35
|
+
|
|
36
|
+
if inferred in ("yaml", "yml"):
|
|
37
|
+
try:
|
|
38
|
+
import yaml
|
|
39
|
+
except Exception as exc:
|
|
40
|
+
raise SystemExit("PyYAML is required for YAML input. Install with: pip install pyyaml") from exc
|
|
41
|
+
return yaml.safe_load(raw)
|
|
42
|
+
|
|
43
|
+
# default JSON
|
|
44
|
+
return json.loads(raw)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def main(argv=None) -> int:
|
|
48
|
+
parser = argparse.ArgumentParser(description="pdfgen CLI")
|
|
49
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
50
|
+
|
|
51
|
+
render = sub.add_parser("render", help="Render a PDF from JSON/YAML")
|
|
52
|
+
render.add_argument("input", help="Path to JSON/YAML data (or - for stdin)")
|
|
53
|
+
render.add_argument("output", help="Output PDF path")
|
|
54
|
+
render.add_argument("--root", dest="root_dir", default=".", help="Project root dir")
|
|
55
|
+
render.add_argument("--template-dir", dest="template_dir", default=None)
|
|
56
|
+
render.add_argument("--css", dest="css_path", default=None)
|
|
57
|
+
render.add_argument("--fonts-conf", dest="fonts_conf", default=None)
|
|
58
|
+
render.add_argument("--fonts-dir", dest="fonts_dir", default=None)
|
|
59
|
+
render.add_argument("--css-extra", dest="css_extra", default=None, help="Extra CSS string")
|
|
60
|
+
render.add_argument("--format", dest="fmt", default=None, help="Input format: json|yaml")
|
|
61
|
+
render.add_argument("--no-validate", action="store_true")
|
|
62
|
+
render.add_argument("--no-paginate", action="store_true")
|
|
63
|
+
|
|
64
|
+
validate = sub.add_parser("validate", help="Validate JSON/YAML input against schema")
|
|
65
|
+
validate.add_argument("input", help="Path to JSON/YAML data (or - for stdin)")
|
|
66
|
+
validate.add_argument("--root", dest="root_dir", default=".", help="Project root dir")
|
|
67
|
+
validate.add_argument("--format", dest="fmt", default=None, help="Input format: json|yaml")
|
|
68
|
+
|
|
69
|
+
args = parser.parse_args(argv)
|
|
70
|
+
|
|
71
|
+
if args.command == "validate":
|
|
72
|
+
from pdfgen.validator import validate_and_normalize
|
|
73
|
+
|
|
74
|
+
root_dir = pathlib.Path(args.root_dir)
|
|
75
|
+
data = _load_data(pathlib.Path(args.input), fmt=args.fmt)
|
|
76
|
+
_, warnings = validate_and_normalize(data, root_dir=root_dir)
|
|
77
|
+
for warning in warnings:
|
|
78
|
+
print(f"[validate] {warning}")
|
|
79
|
+
return 0 if not warnings else 1
|
|
80
|
+
|
|
81
|
+
root_dir = pathlib.Path(args.root_dir)
|
|
82
|
+
config = PDFGenConfig.from_root(root_dir)
|
|
83
|
+
|
|
84
|
+
if args.template_dir:
|
|
85
|
+
config.template_dir = pathlib.Path(args.template_dir)
|
|
86
|
+
if args.css_path:
|
|
87
|
+
config.css_path = pathlib.Path(args.css_path)
|
|
88
|
+
if args.fonts_conf:
|
|
89
|
+
config.fonts_conf = pathlib.Path(args.fonts_conf)
|
|
90
|
+
if args.fonts_dir:
|
|
91
|
+
config.fonts_conf = _build_fonts_conf(pathlib.Path(args.fonts_dir))
|
|
92
|
+
|
|
93
|
+
data = _load_data(pathlib.Path(args.input), fmt=args.fmt)
|
|
94
|
+
|
|
95
|
+
PDFGen(config).render(
|
|
96
|
+
data,
|
|
97
|
+
output_path=pathlib.Path(args.output),
|
|
98
|
+
paginate=not args.no_paginate,
|
|
99
|
+
validate=not args.no_validate,
|
|
100
|
+
css_extra=args.css_extra,
|
|
101
|
+
)
|
|
102
|
+
return 0
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
if __name__ == "__main__":
|
|
106
|
+
raise SystemExit(main())
|