Path:
strictdoc/features/html2pdf/html2pdf_generator.py
Lines:
122
Non-empty lines:
102
Non-empty lines covered with requirements:
102 / 102 (100.0%)
Functions:
2
Functions covered by requirements:
2 / 2 (100.0%)
1
"""2
@relation(SDOC-SRS-51, scope=file)3
"""4
5
import os
6
from pathlib import Path
7
from typing import List, Tuple
8
9
from strictdoc.core.project_config import ProjectConfig
10
from strictdoc.core.traceability_index import TraceabilityIndex
11
from strictdoc.export.html.html_generator import HTMLGenerator
12
from strictdoc.export.html.html_templates import HTMLTemplates
13
from strictdoc.export.html.renderers.link_renderer import LinkRenderer
14
from strictdoc.export.html.renderers.markup_renderer import MarkupRenderer
15
from strictdoc.features.html2pdf.generator import (
16
DocumentHTML2PDFGenerator,
17
)18
from strictdoc.features.html2pdf.pdf_print_driver import PDFPrintDriver
19
from strictdoc.helpers.exception import StrictDocException
20
from strictdoc.helpers.git_client import GitClient
21
from strictdoc.helpers.timing import measure_performance
22
23
24
class HTML2PDFGenerator:
25
@staticmethod26
def export_tree(
27
project_config: ProjectConfig,
28
traceability_index: TraceabilityIndex,
29
html_templates: HTMLTemplates,
30
output_html2pdf_root: str,
31
flat_assets: bool = False,
32
) -> None:
33
if not project_config.is_activated_html2pdf():
34
raise StrictDocException("HTML2PDF feature is not enabled")
35
36
git_client: GitClient = GitClient()
37
38
path_to_output_pdf_html_dir = os.path.join(output_html2pdf_root, "html")
39
path_to_output_pdf_pdf_dir = os.path.join(output_html2pdf_root, "pdf")
40
41
HTMLGenerator.export_assets(
42
traceability_index=traceability_index,
43
project_config=project_config,
44
export_output_html_root=path_to_output_pdf_html_dir,
45
flat_assets=flat_assets,
46
)47
48
paths_to_print: List[Tuple[str, str]] = []
49
50
for document_ in traceability_index.document_tree.document_list:
51
assert document_.meta is not None
52
53
# Skip generating the included documents, unless the option is provided.54
if not project_config.export_included_documents:
55
if document_.document_is_included():
56
continue57
58
root_path = document_.meta.get_root_path_prefix()
59
60
link_renderer = LinkRenderer(
61
root_path=root_path,
62
static_path=project_config.dir_for_sdoc_assets,
63
)64
markup_renderer = MarkupRenderer.create(
65
document_.config.get_markup(),
66
traceability_index,
67
link_renderer,
68
html_templates,
69
project_config,
70
document_,
71
)72
73
with measure_performance("Generating printable HTML document"):
74
document_content = DocumentHTML2PDFGenerator.export(
75
project_config,
76
document_,
77
traceability_index,
78
markup_renderer,
79
link_renderer,
80
git_client=git_client,
81
html_templates=html_templates,
82
)83
84
path_to_output_html_doc_dir = os.path.join(
85
path_to_output_pdf_html_dir,
86
document_.meta.output_document_dir_rel_path.relative_path,
87
)88
Path(path_to_output_html_doc_dir).mkdir(parents=True, exist_ok=True)
89
Path(path_to_output_pdf_pdf_dir).mkdir(parents=True, exist_ok=True)
90
91
path_to_output_html_doc = os.path.join(
92
path_to_output_html_doc_dir,
93
document_.meta.document_filename_base + "-PDF.html",
94
)95
96
with open(
97
path_to_output_html_doc, "w", encoding="utf8"
98
) as output_html_doc_file_:
99
output_html_doc_file_.write(document_content)
100
101
path_to_output_pdf_dir = os.path.join(
102
path_to_output_pdf_pdf_dir,
103
document_.meta.input_doc_dir_rel_path.relative_path,
104
)105
Path(path_to_output_pdf_dir).mkdir(parents=True, exist_ok=True)
106
107
path_to_output_pdf = os.path.join(
108
path_to_output_pdf_dir,
109
document_.meta.document_filename_base + ".pdf",
110
)111
112
paths_to_print.append((path_to_output_html_doc, path_to_output_pdf))
113
114
pdf_print_driver = PDFPrintDriver()
115
try:
116
pdf_print_driver.get_pdf_from_html(
117
project_config,
118
paths_to_print,
119
path_to_output_pdf_html_dir,
120
)121
except TimeoutError:
122
print("error: HTML2PDF: timeout error.") # noqa: T201