Skip to content

Commit

Permalink
pyinvoice: check if weasyprint is available before trying to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixSchwarz committed Dec 16, 2022
1 parent 3f9aa50 commit b4f3046
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/schwarz/pyinvoice/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .check import check_invoice, MetaInfo
from .config import parse_config
from .parser import InvoiceParser
from .pdf_generator import generate_pdf
from .pdf_generator import generate_pdf, is_weasyprint_available


__all__ = ['cli_main']
Expand Down Expand Up @@ -74,5 +74,9 @@ def cli_main():
if found_error:
return

if not is_weasyprint_available():
print('"weasyprint" not found, please install the weasyprint package.')
return

generate_pdf(invoice, invoice_cfg, target_path=pdf_path, with_logo=with_logo)

9 changes: 8 additions & 1 deletion src/schwarz/pyinvoice/pdf_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import jinja2


__all__ = ['generate_pdf']
__all__ = ['generate_pdf', 'is_weasyprint_available']

def is_weasyprint_available():
try:
subprocess.run(['weasyprint', '--version'], capture_output=True, shell=False)
except FileNotFoundError:
return False
return True

def generate_pdf(invoice, invoice_cfg, target_path, *, with_logo=False):
template_path = Path(invoice_cfg['template'])
Expand Down

0 comments on commit b4f3046

Please sign in to comment.