| Total Complexity | 6 |
| Total Lines | 38 |
| Duplicated Lines | 0 % |
| Coverage | 72.72% |
| Changes | 0 | ||
| 1 | 1 | from lxml.etree import QName |
|
| 2 | 1 | from weasyprint import HTML, CSS |
|
| 3 | |||
| 4 | 1 | from .transform import * |
|
| 5 | 1 | from .transform.pdf_environment import PDFEnvironment |
|
| 6 | |||
| 7 | 1 | PDF_CSS = CSS(string="@page {margin: 1.0cm 1.27cm 1.1cm 0.85cm;}") |
|
| 8 | |||
| 9 | |||
| 10 | 1 | class Representable: |
|
| 11 | 1 | def html_write(self, target, templates_path=None): |
|
| 12 | if templates_path: |
||
| 13 | env = PDFEnvironment(templates_path=templates_path) |
||
| 14 | init_template = env.get_template("_init.html") |
||
| 15 | else: |
||
| 16 | init_template = PDF_INIT_TEMPLATE |
||
| 17 | |||
| 18 | init_template.stream({"c": self, "k": QName(self.tag).localname}).dump(target) |
||
| 19 | |||
| 20 | 1 | def html_str(self, templates_path=None) -> str: |
|
| 21 | 1 | if templates_path: |
|
| 22 | 1 | env = PDFEnvironment(templates_path=templates_path) |
|
| 23 | 1 | init_template = env.get_template("_init.html") |
|
| 24 | else: |
||
| 25 | 1 | init_template = PDF_INIT_TEMPLATE |
|
| 26 | |||
| 27 | 1 | return init_template.render({"c": self, "k": QName(self.tag).localname}) |
|
| 28 | |||
| 29 | 1 | def pdf_write(self, target, templates_path=None): |
|
| 30 | 1 | HTML(string=self.html_str(templates_path=templates_path)).write_pdf( |
|
| 31 | target=target, |
||
| 32 | stylesheets=[PDF_CSS] |
||
| 33 | ) |
||
| 34 | |||
| 35 | 1 | def pdf_bytes(self, templates_path=None) -> bytes: |
|
| 36 | return HTML(string=self.html_str(templates_path=templates_path)).write_pdf( |
||
| 37 | stylesheets=[PDF_CSS] |
||
| 38 | ) |
||
| 39 |