|
13 | 13 | import sys |
14 | 14 | from datetime import datetime |
15 | 15 |
|
16 | | -from reportlab.lib import colors |
17 | | -from reportlab.lib.enums import TA_RIGHT |
18 | | -from reportlab.lib.pagesizes import LETTER |
19 | | -from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet |
20 | | -from reportlab.platypus import (SimpleDocTemplate, Paragraph, Spacer, Table, |
21 | | - TableStyle) |
| 16 | +# Reportlab is required for PDF generation. If it's missing, emit a clear |
| 17 | +# error message on stderr so callers can surface the failure to users. |
| 18 | +try: |
| 19 | + from reportlab.lib import colors |
| 20 | + from reportlab.lib.enums import TA_RIGHT |
| 21 | + from reportlab.lib.pagesizes import LETTER |
| 22 | + from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet |
| 23 | + from reportlab.platypus import ( |
| 24 | + SimpleDocTemplate, |
| 25 | + Paragraph, |
| 26 | + Spacer, |
| 27 | + Table, |
| 28 | + TableStyle, |
| 29 | + ) |
| 30 | +except ModuleNotFoundError as exc: # pragma: no cover - exercised via JS |
| 31 | + print(str(exc), file=sys.stderr) |
| 32 | + sys.exit(1) |
22 | 33 |
|
23 | 34 |
|
24 | 35 | def _load_profile(base_dir): |
@@ -174,8 +185,10 @@ def main(): |
174 | 185 |
|
175 | 186 | buffer = io.BytesIO() |
176 | 187 | generate_invoice(buffer, invoice_data) |
177 | | - pdf_bytes = buffer.getvalue() |
| 188 | + buffer.seek(0) |
| 189 | + pdf_bytes = buffer.read() |
178 | 190 | sys.stdout.write(base64.b64encode(pdf_bytes).decode("ascii")) |
| 191 | + sys.stdout.flush() |
179 | 192 |
|
180 | 193 |
|
181 | 194 | if __name__ == "__main__": |
|
0 commit comments