add pdf generator
Browse files- utils/pdfutils.py +31 -0
utils/pdfutils.py
CHANGED
|
@@ -54,6 +54,37 @@ class PDFGenerator:
|
|
| 54 |
)
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def generate_pdf(self, content: Union[str, Dict]) -> io.BytesIO:
|
| 58 |
"""Generate PDF with improved formatting"""
|
| 59 |
try:
|
|
|
|
| 54 |
)
|
| 55 |
)
|
| 56 |
|
| 57 |
+
def generate_pdf_from_text(self, text: str, filename: str, output_dir: str = "discharge_papers") -> str:
|
| 58 |
+
"""Generate a PDF file from plain text and return the file path."""
|
| 59 |
+
try:
|
| 60 |
+
if not os.path.exists(output_dir):
|
| 61 |
+
os.makedirs(output_dir)
|
| 62 |
+
filepath = os.path.join(output_dir, filename)
|
| 63 |
+
doc = SimpleDocTemplate(filepath, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=72)
|
| 64 |
+
elements = []
|
| 65 |
+
|
| 66 |
+
# Add title
|
| 67 |
+
elements.append(Paragraph("Discharge Summary", self.styles['CustomTitle']))
|
| 68 |
+
elements.append(Spacer(1, 20))
|
| 69 |
+
|
| 70 |
+
# Split text into lines and format
|
| 71 |
+
lines = text.split('\n')
|
| 72 |
+
for line in lines:
|
| 73 |
+
if line.strip():
|
| 74 |
+
if ':' in line and not line.strip().startswith('-'):
|
| 75 |
+
elements.append(Paragraph(line.strip(), self.styles['SectionHeader']))
|
| 76 |
+
elements.append(Spacer(1, 12))
|
| 77 |
+
else:
|
| 78 |
+
elements.append(Paragraph(line.strip(), self.styles['CustomContent']))
|
| 79 |
+
elements.append(Spacer(1, 8))
|
| 80 |
+
|
| 81 |
+
# Build PDF
|
| 82 |
+
doc.build(elements)
|
| 83 |
+
return filepath
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error generating PDF from text: {str(e)}")
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
def generate_pdf(self, content: Union[str, Dict]) -> io.BytesIO:
|
| 89 |
"""Generate PDF with improved formatting"""
|
| 90 |
try:
|