Skip to content

Commit

Permalink
use weasyprint to generate pdfs from html + css
Browse files Browse the repository at this point in the history
  • Loading branch information
damonmcc committed Oct 2, 2024
1 parent f0a5edc commit da37bc1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
13 changes: 9 additions & 4 deletions dcpy/lifecycle/package/generate_metadata_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
DEFAULT_DATA_DICTIONARY_TEMPLATE_PATH = (
RESOURCES_PATH / "data_dictionary_template.jinja"
)
DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH = RESOURCES_PATH / "data_dictionary.css"


def generate_pdf_from_html(output_html_path: Path, output_pdf_path: Path) -> Path:
def generate_pdf_from_html(
output_html_path: Path,
output_pdf_path: Path,
stylesheet_path: Path,
) -> Path:
subprocess.run(
[
"pandoc",
"weasyprint",
output_html_path,
"-o",
output_pdf_path,
"--pdf-engine=weasyprint",
"-s",
stylesheet_path,
],
check=True,
)
Expand Down
22 changes: 22 additions & 0 deletions dcpy/lifecycle/package/resources/data_dictionary.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
html {
color: #393939;
font-family: Fira Sans;
font-size: 11pt;
font-weight: 300;
line-height: 1.5;
}
h1 {
string-set: top-right-text content();
}
@page {
@top-left {
content: url("DCP_Logo.svg");
}
@top-right {
content: string(top-right-text);
}
@bottom-right-corner {
content: counter(page);
text-align: center;
}
}
11 changes: 2 additions & 9 deletions dcpy/test/lifecycle/package/test_generate_data_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@ class TestDataDictionary(TestCase):
output_pdf_path = TEMP_DATA_PATH / "metadata.pdf"
output_xlsx_path = TEMP_DATA_PATH / "my_data_dictionary.pdf"

def test_generate_html_from_yaml(self):
html_path = generate_metadata_assets.generate_html_from_yaml(
yaml_file_path=self.yaml_file_path,
output_html_path=self.output_html_path,
html_template_path=generate_metadata_assets.DEFAULT_DATA_DICTIONARY_TEMPLATE_PATH,
)
assert html_path.exists()

def test_generate_pdf_from_html(self):
def test_generate_pdf_from_yaml(self):
html_path = generate_metadata_assets.generate_html_from_yaml(
yaml_file_path=self.yaml_file_path,
output_html_path=self.output_html_path,
Expand All @@ -34,6 +26,7 @@ def test_generate_pdf_from_html(self):
pdf_path = generate_metadata_assets.generate_pdf_from_html(
output_html_path=html_path,
output_pdf_path=self.output_pdf_path,
stylesheet_path=generate_metadata_assets.DEFAULT_DATA_DICTIONARY_STYLESHEET_PATH,
)
assert pdf_path.exists()

Expand Down

0 comments on commit da37bc1

Please sign in to comment.