diff --git a/Dockerfile b/Dockerfile index 0e68381..bc3dbe6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,10 +2,9 @@ ARG PYTHON_VERSION=3.10 FROM python:$PYTHON_VERSION-slim ARG USER=pydfy-user -ENTRYPOINT ["poetry", "run", "python"] RUN apt-get update && apt-get install -y chromium chromium-driver curl -RUN curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64 && \ +RUN curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.3/tailwindcss-linux-x64 && \ chmod +x tailwindcss-linux-x64 && \ mv tailwindcss-linux-x64 /usr/local/bin/tailwindcss RUN groupadd --gid 1000 $USER && adduser --disabled-password --gecos '' --uid 1000 --gid 1000 $USER @@ -16,7 +15,7 @@ ENV PATH="/home/$USER/.local/bin:/home/$USER/pydfy/.venv/bin:${PATH}" \ PYTHONUNBUFFERED=1 \ POETRY_NO_INTERACTION=1 \ POETRY_VIRTUALENVS_IN_PROJECT=true\ - PYDFY_BUILD_DIR=/data + PYDFY_OUT=/dev/stdout RUN --mount=type=cache,target=/root/.cache pip install --user poetry==1.4.2 @@ -25,3 +24,7 @@ RUN --mount=type=cache,target=/root/.cache poetry install --with dev --all-extra COPY --chown=$USER:$USER ./ ./ RUN --mount=type=cache,target=/root/.cache poetry install --with dev --all-extras + +WORKDIR /pydfy +ENTRYPOINT ["python"] +CMD ["main.py"] diff --git a/README.md b/README.md index 6a2080d..04c161e 100644 --- a/README.md +++ b/README.md @@ -89,9 +89,9 @@ pdf.render(out="report.pdf", build_dir="/build") # Save everything in the /data directory pdf.render(out="/data/report.pdf", build_dir="/data") -# Equivalent to the previous line -os.environ["PYDFY_BUILD_DIR"] = "/data" -pdf.render(out="/data/report.pdf") +# Only save the pdf in the /data directory +os.environ["PYDFY_OUT"] = "/data/report.pdf" +pdf.render() ``` ### Custom Components @@ -133,9 +133,14 @@ You can add custom CSS to the compilation step: pdf.render(css="blue.css") ``` -Content of `my.css`, where the path should point to your pydfy install: +Content of `my.css`, where we need to copy the contents of `base.css` first: ```css -@import "../../pydfy/template/src/base.css"; +/* Tailwind setup */ +@tailwind base; @tailwind components; @tailwind utilities; +@media print { body { -webkit-print-color-adjust: exact; } } +@media print { html, body { width: 210mm; height: 297mm; font-size: 8pt; } } +@media print { thead {display: table-row-group;} } +@page { size: A4; margin: 1cm; } .pf-number-content { color: blue; @@ -151,7 +156,7 @@ You could use Docker in the development process as follows: ```shell docker build -t pydfy . cd examples/iris -docker run -v $PWD/:/data pydfy /data/main.py # The docker image is configured with PYDFY_BUILD_DIR=/data +docker run -v $PWD:/pydfy pydfy > out.pdf # The docker image is configured to run main.py and write to stdout ``` ## Contributing diff --git a/examples/custom/out.pdf b/examples/custom/out.pdf index 269d57a..bb4f7af 100644 Binary files a/examples/custom/out.pdf and b/examples/custom/out.pdf differ diff --git a/examples/iris/blue.css b/examples/iris/blue.css index 50495ff..cace9c6 100644 --- a/examples/iris/blue.css +++ b/examples/iris/blue.css @@ -1,4 +1,8 @@ -@import "../../pydfy/template/src/base.css"; +@tailwind base; @tailwind components; @tailwind utilities; +@media print { body { -webkit-print-color-adjust: exact; } } +@media print { html, body { width: 210mm; height: 297mm; font-size: 8pt; } } +@media print { thead {display: table-row-group;} } +@page { size: A4; margin: 1cm; } .pf-number-content { color: blue; diff --git a/examples/iris/main.py b/examples/iris/main.py index e4f0da9..cae4232 100644 --- a/examples/iris/main.py +++ b/examples/iris/main.py @@ -22,7 +22,7 @@ def main(): pf.PDF( [ - pf.Title("My Beautiful Report with a very long title that I'd really like to keep", col_span=2), + pf.Title("My Report Title Spanning Multiple Columns", col_span=2), pf.Image(Path(__file__).parent / "logo.png"), ], pf.Section("Documentation"), diff --git a/examples/iris/out.pdf b/examples/iris/out.pdf index 95b985b..a3f8587 100644 Binary files a/examples/iris/out.pdf and b/examples/iris/out.pdf differ diff --git a/pydfy/renderer.py b/pydfy/renderer.py index ee22300..579351e 100644 --- a/pydfy/renderer.py +++ b/pydfy/renderer.py @@ -26,7 +26,7 @@ def render( build_dir.mkdir(exist_ok=True, parents=True) if not out: - out = Path(os.getenv("PYDFY_BUILD_DIR", ".")) / Path("out.pdf") + out = Path(os.getenv("PYDFY_OUT", "./out.pdf")) out = Path(out) @@ -61,21 +61,20 @@ def _render_template(template_file: Path, out: Path, pdf_arguments: _PDF) -> Non def _compile_css(build_dir: Path, css: Path | str | None) -> None: - base = str(BASE_DIR / "pydfy" / "template" / "src" / "base.css") - out = str(build_dir / "out.css") + base = str((BASE_DIR / "pydfy" / "template" / "src" / "base.css").absolute()) + out = str((build_dir / "out.css").absolute()) - args = [ + tailwind_args = [ "tailwindcss", "-i", base if not css else str(Path(css)), "-o", out, "--content", - str(relative_to(build_dir, Path().absolute())) + "/*.html", + str(build_dir.absolute()) + "/*.html", ] - # if css: - subprocess.run(args, capture_output=True) + subprocess.run(tailwind_args, capture_output=True) def _print_to_pdf(rendered_template_file: Path, output_path: Path) -> None: diff --git a/pydfy/template/src/base.css b/pydfy/template/src/base.css index b60e30b..7604a63 100644 --- a/pydfy/template/src/base.css +++ b/pydfy/template/src/base.css @@ -1,21 +1,5 @@ -@tailwind base; -@tailwind components; -@tailwind utilities; - +@tailwind base; @tailwind components; @tailwind utilities; @media print { body { -webkit-print-color-adjust: exact; } } - -@page { - size: A4; - margin: 1cm; -} -@media print { - html, body { - width: 210mm; - height: 297mm; - font-size: 8pt; - } -} - -@media print { - thead {display: table-row-group;} -} +@media print { html, body { width: 210mm; height: 297mm; font-size: 8pt; } } +@media print { thead {display: table-row-group;} } +@page { size: A4; margin: 1cm; }