Skip to content

Commit a151acc

Browse files
authored
Merge pull request #2225 from thseiler/feature/custom-html2pdf-template-example
export/html2pdf: add custom html2pdf template example
2 parents 1db3b01 + 40448b7 commit a151acc

File tree

6 files changed

+236
-3
lines changed

6 files changed

+236
-3
lines changed

docs/strictdoc_01_user_guide.sdoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,35 @@ The ``source_root_path`` option supports relative paths, e.g. ``../source_root/`
29142914

29152915
[/SECTION]
29162916

2917+
[SECTION]
2918+
MID: 499fa0144cbd48c687ba0d5f9f7cb846
2919+
TITLE: Path to custom HTML2PDF template
2920+
2921+
[TEXT]
2922+
MID: 225dc4d22c084a67ae049fca1098bc1a
2923+
STATEMENT: >>>
2924+
StrictDoc uses an internal default template when exporting documents in PDF format.
2925+
2926+
If the front page, header, or footer needs to be customised, it is possible to provide a custom template with the `html2pdf_template` option.
2927+
2928+
.. code-block::
2929+
2930+
[project]
2931+
html2pdf_template = "assets/template/index.jinja"
2932+
2933+
features = [
2934+
"HTML2PDF",
2935+
]
2936+
2937+
When specified, the provided template will override the internal default template. The path can be absolute or relative to the project root.
2938+
2939+
.. note ::
2940+
2941+
An example template can be found `here <https://github.com/strictdoc-project/strictdoc/tree/main/tests/end2end/screens/pdf/view_pdf_document_custom_template/html2pdf_template>`__
2942+
<<<
2943+
2944+
[/SECTION]
2945+
29172946
[SECTION]
29182947
MID: 593a2abbdaa5470988103c70e87c4cfc
29192948
TITLE: Include/exclude document paths

strictdoc/export/html/html_generator.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,21 @@ def export_assets(
246246
message="Copying HTML2PDF.js",
247247
)
248248

249+
# Export custom html2pdf template
250+
if project_config.html2pdf_template is not None:
251+
output_custom_html2pdf_template = os.path.join(
252+
export_output_html_root,
253+
project_config.dir_for_sdoc_assets,
254+
"html2pdf_template",
255+
)
256+
sync_dir(
257+
os.path.abspath(
258+
os.path.dirname(project_config.html2pdf_template)
259+
),
260+
output_custom_html2pdf_template,
261+
message="Copying Custom HTML2PDF template assets",
262+
)
263+
249264
# Export project's assets.
250265

251266
redundant_assets: Dict[str, List[SDocRelativePath]] = {}

strictdoc/export/html/templates/screens/document/pdf/main.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@
5151
{%- include "screens/document/pdf/template/header.jinja" -%}
5252
{%- include "screens/document/pdf/template/footer.jinja" -%}
5353
{% else %}
54-
{{ view_object.custom_html2pdf_template.render() }}
54+
{{ view_object.custom_html2pdf_template.render(view_object=view_object) | safe }}
5555
{% endif %}
Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,190 @@
1-
CUSTOM TEMPLATE HERE
1+
{# This is an example template for customizing the HTML2PDF export of your StrictDoc documents.
2+
3+
Notes:
4+
5+
- This template is interpreted in a environment without loader, therefore it is not possible
6+
to use jinja extend or include directives. The template must be fully self-contained.
7+
8+
- To use Images, place them in the same folder as this template file. They will be included in the
9+
HTML export under "/_static/html2pdf_template/. As the user can constomize the static asset path,
10+
it is necessary to use the link renderer's to get the full URL to an image.
11+
12+
For example:
13+
14+
<img src="{{ view_object.render_static_url('html2pdf_template/logo_top_page.png') }}">
15+
16+
#}
17+
18+
{# customize the front page ---------------------------------------------------------------------------- #}
19+
20+
<style html2pdf-frontpage-style>
21+
.html2pdf-frontpage-grid {
22+
display: grid;
23+
height: 100%;
24+
}
25+
.html2pdf-frontpage-grid-middle {
26+
display: flex;
27+
align-items: center;
28+
justify-content: center;
29+
text-align: center;
30+
}
31+
.html2pdf-frontpage-grid-bottom {
32+
display: flex;
33+
justify-content: center;
34+
align-items: flex-end;
35+
}
36+
</style>
37+
<template html2pdf-frontpage>
38+
{#- add here frontpage html -#}
39+
<div class="html2pdf-frontpage-grid">
40+
<div class="html2pdf-frontpage-grid-top"> </div>
41+
<div class="html2pdf-frontpage-grid-middle">
42+
<h1 data-testid="document-title">
43+
<img src="{{ view_object.render_static_url('html2pdf_template/logo_top_page.png') }}"
44+
alt="Company Logo"
45+
style="width: 80%" />
46+
<br>
47+
<br>
48+
{{ view_object.document.title }}</h1>
49+
</div>
50+
<div class="html2pdf-frontpage-grid-bottom">
51+
{%- if view_object.document.config.has_meta() -%}
52+
<sdoc-meta>
53+
{%- if view_object.document.config.uid -%}
54+
<sdoc-meta-label data-testid="document-config-uid-label">UID:</sdoc-meta-label>
55+
<sdoc-meta-field data-testid="document-config-uid-field">
56+
{%- with field_content = view_object.document.config.uid %}
57+
<sdoc-autogen>{{ field_content }}</sdoc-autogen>
58+
{%- endwith -%}
59+
</sdoc-meta-field>
60+
{%- endif -%}
61+
62+
{% set document_version_ = view_object.render_document_version() %}
63+
{%- if document_version_ is not none -%}
64+
<sdoc-meta-label data-testid="document-config-version-label">VERSION:</sdoc-meta-label>
65+
<sdoc-meta-field data-testid="document-config-version-field">
66+
{%- with field_content = document_version_ %}
67+
<sdoc-autogen>{{ field_content }}</sdoc-autogen>
68+
{%- endwith -%}
69+
</sdoc-meta-field>
70+
{# FIXME: Task #1229 / Uncomment example when validation logic is implemented: #}
71+
{#
72+
{% with issue_field_name = 'meta' %}
73+
<div id="field_issue_ID" class="field_issue">
74+
<div class="field_issue-ribbon">
75+
<b>Warning:</b> '{{ issue_field_name }}' field has issue: description in details
76+
</div>
77+
</div>
78+
{% endwith %}
79+
#}
80+
{%- endif -%}
81+
82+
{% set document_date_ = view_object.render_document_date() %}
83+
{%- if document_date_ is not none -%}
84+
<sdoc-meta-label data-testid="document-config-version-label">DATE:</sdoc-meta-label>
85+
<sdoc-meta-field data-testid="document-config-version-field">
86+
{%- with field_content = document_date_ %}
87+
<sdoc-autogen>{{ field_content }}</sdoc-autogen>
88+
{%- endwith -%}
89+
</sdoc-meta-field>
90+
{%- endif -%}
91+
92+
{%- if view_object.document.config.classification -%}
93+
<sdoc-meta-label data-testid="document-config-classification-label">CLASSIFICATION:</sdoc-meta-label>
94+
<sdoc-meta-field data-testid="document-config-classification-field">
95+
{%- with field_content = view_object.document.config.classification %}
96+
<sdoc-autogen>{{ field_content }}</sdoc-autogen>
97+
{%- endwith -%}
98+
</sdoc-meta-field>
99+
{%- endif -%}
100+
</sdoc-meta>
101+
{%- endif -%}
102+
</div>
103+
</div>
104+
</template>
105+
106+
107+
{# customize the page header ---------------------------------------------------------------------------- #}
108+
109+
<style html2pdf-header-style>
110+
html2pdf-header {
111+
padding-top: 0;
112+
}
113+
.html2pdf-header {
114+
display: flex;
115+
justify-content: space-between;
116+
align-items: flex-end;
117+
column-gap: 32px;
118+
font-size: small;
119+
line-height: 1;
120+
color: rgba(0,0,0,0.5);
121+
border-bottom: 1px solid rgba(0,0,0,0.25);
122+
padding-bottom: 8px;
123+
}
124+
.html2pdf-header-left {
125+
text-align: left;
126+
flex-shrink: 0;
127+
}
128+
.html2pdf-header-right {
129+
text-align: right;
130+
font-weight: bold;
131+
}
132+
</style>
133+
<template html2pdf-header>
134+
{#- uncomment html2pdf-page-number to print page numbers: -#}
135+
{#- <div html2pdf-page-number>
136+
<span html2pdf-page-number-current></span>/<span html2pdf-page-number-total></span>
137+
</div> -#}
138+
139+
{#- add here header html: -#}
140+
<div class="html2pdf-header">
141+
<div class="html2pdf-header-left">{{ view_object.document.title }}</div>
142+
<div class="html2pdf-header-right">
143+
<img src="{{ view_object.render_static_url('html2pdf_template/logo_top_page.png') }}"
144+
alt="Company Logo"
145+
style="height: 20px;" />
146+
</div>
147+
</div>
148+
</template>
149+
150+
151+
{# customize the page footer ---------------------------------------------------------------------------- #}
152+
153+
<style html2pdf-footer-style>
154+
html2pdf-footer {
155+
padding-bottom: 0;
156+
}
157+
.html2pdf-footer {
158+
display: flex;
159+
justify-content: space-between;
160+
column-gap: 16px;
161+
font-size: small;
162+
line-height: 1;
163+
color: rgba(0,0,0,0.5);
164+
border-top: 1px solid rgba(0,0,0,0.25);
165+
padding-top: 8px;
166+
}
167+
.html2pdf-footer-left {
168+
text-align: left;
169+
}
170+
.html2pdf-footer-right {
171+
text-align: right;
172+
}
173+
.html2pdf-header-page_placeholder {
174+
width: 54px; /* max for pattern 888/888 */
175+
flex-shrink: 0;
176+
text-indent: -10000px;
177+
}
178+
</style>
179+
<template html2pdf-footer>
180+
{#- uncomment html2pdf-page-number to print page numbers: -#}
181+
<div html2pdf-page-number>
182+
<span html2pdf-page-number-current></span>/<span html2pdf-page-number-total></span>
183+
</div>
184+
{#- add here footer html -#}
185+
<div class="html2pdf-footer">
186+
<div class="html2pdf-footer-left">{{ view_object.date_today() }}</div>
187+
<div class="html2pdf-footer-right">ACME Laboratories Inc. {{view_object.document.config.classification}}</div>
188+
<div class="html2pdf-header-page_placeholder">{#- must be left empty! -#}</div>
189+
</div>
190+
</template>

tests/end2end/screens/pdf/view_pdf_document_custom_template/test_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ def test(self):
3636
screen_pdf.assert_on_pdf_document()
3737
screen_pdf.assert_not_empty_view()
3838

39-
screen_pdf.assert_text("CUSTOM TEMPLATE HERE")
39+
screen_pdf.assert_text("ACME Laboratories Inc.")

0 commit comments

Comments
 (0)