Skip to content

Commit

Permalink
Reduce tailwind css and make it required for extensions
Browse files Browse the repository at this point in the history
Use absolute path for base.css and user supplied css
Use better pdf title
Write to STDOUT in the Docker image
Update README.md
  • Loading branch information
Donnype committed Jul 3, 2024
1 parent 0e1447e commit 6486d2f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 38 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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"]
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
Binary file modified examples/custom/out.pdf
Binary file not shown.
6 changes: 5 additions & 1 deletion examples/iris/blue.css
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/iris/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Binary file modified examples/iris/out.pdf
Binary file not shown.
13 changes: 6 additions & 7 deletions pydfy/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
24 changes: 4 additions & 20 deletions pydfy/template/src/base.css
Original file line number Diff line number Diff line change
@@ -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; }

0 comments on commit 6486d2f

Please sign in to comment.