Skip to content

Commit

Permalink
Add Helsinki logo to the PDF templates
Browse files Browse the repository at this point in the history
  • Loading branch information
indigane committed Dec 17, 2024
1 parent abd72a1 commit ec03695
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
static
/static
mediaroot
/oracle-data
.pytest_cache
.ruff_cache
**.xlsx
.~lock.*#
*.~*
!**/*.po
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions backend/hitas/templates/components/base.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<style>
@page {
size: A4 portrait;
@frame h_logo {-pdf-frame-content: header_logo; left: 50pt; width: 100pt; top: 35pt; height: 40pt;}
@frame h_date {-pdf-frame-content: header_date; left: 325pt; width: 100pt; top: 50pt; height: 40pt;}
@frame content_frame {left: 50pt; width: 512pt; top: 90pt; height: 632pt;}
@frame footer {-pdf-frame-content: footer; left: 50pt; width: 500pt; top: 770pt; height: 50pt;}
Expand Down Expand Up @@ -36,6 +37,7 @@
<body>
{% block header %}
<div id="header_logo"><img src="{{ logo_path }}" width="90" /></div>
<div id="header_date">{{ date_today }}</div>
<div id="header_page_1">Liite 1</div>
<div id="header_page_2">Liite 2</div>
Expand Down
18 changes: 16 additions & 2 deletions backend/hitas/views/utils/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from io import BytesIO
from typing import Any

from django.conf import settings
from django.http import HttpResponse
from django.template.loader import get_template
from django.utils import timezone
Expand All @@ -12,7 +13,14 @@
def render_to_pdf(template: str, context: dict[str, Any]) -> bytes:
"""Render given template to a pdf"""
result = BytesIO()
template = get_template(template).render(context)
template = get_template(template).render(
{
**context,
# Due to the limitations of the xhtml2pdf library a file system path
# is required instead of a Django static files url.
"logo_path": settings.BASE_DIR / "hitas/static/helsinki_kehystunnus_musta.png",
},
)
pdf = pisa.pisaDocument(BytesIO(template.encode("UTF-8")), dest=result)
if pdf.err:
raise exceptions.APIException(pdf.err)
Expand All @@ -21,7 +29,13 @@ def render_to_pdf(template: str, context: dict[str, Any]) -> bytes:

def get_pdf_response(filename: str, template: str, context: dict[str, Any]) -> HttpResponse:
context.setdefault("title", filename)
pdf = render_to_pdf(template, {**context, "date_today": datetime.strftime(timezone.now().date(), "%d.%m.%Y")})
pdf = render_to_pdf(
template,
{
**context,
"date_today": datetime.strftime(timezone.now().date(), "%d.%m.%Y"),
},
)
response = HttpResponse(pdf, content_type="application/pdf")
response.headers["Content-Disposition"] = f"attachment; filename={filename}"
return response

0 comments on commit ec03695

Please sign in to comment.