|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import hashlib |
6 | | -import os |
7 | 6 | import tempfile |
8 | 7 | import textwrap |
9 | 8 | import uuid |
@@ -609,33 +608,33 @@ def generate_word(self, body_html, metadata): |
609 | 608 | """ |
610 | 609 |
|
611 | 610 | reference_docx = "core/static/reference.docx" |
| 611 | + output = BytesIO() |
612 | 612 |
|
613 | 613 | # Convert the HTML to a temporary docx file |
614 | | - with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp_file: |
| 614 | + with tempfile.NamedTemporaryFile(suffix=".docx", prefix="docx_") as tmp_file: |
615 | 615 | output_path = tmp_file.name |
616 | 616 |
|
617 | | - pypandoc.convert_text( |
618 | | - html_string, |
619 | | - "docx", |
620 | | - format="html", |
621 | | - outputfile=output_path, |
622 | | - extra_args=["--reference-doc", reference_docx], |
623 | | - ) |
624 | | - |
625 | | - # Create a BytesIO object to store the output of the temporary docx file |
626 | | - with open(output_path, "rb") as f: |
627 | | - output = BytesIO(f.read()) |
| 617 | + pypandoc.convert_text( |
| 618 | + html_string, |
| 619 | + "docx", |
| 620 | + format="html", |
| 621 | + outputfile=output_path, |
| 622 | + extra_args=["--reference-doc", reference_docx], |
| 623 | + ) |
628 | 624 |
|
629 | | - # Remove the temporary docx file |
630 | | - os.remove(output_path) |
| 625 | + # Create a BytesIO object to store the output of the temporary docx file |
| 626 | + with open(output_path, "rb") as f: |
| 627 | + output = BytesIO(f.read()) |
631 | 628 |
|
| 629 | + # Ensure the pointer is at the beginning |
632 | 630 | output.seek(0) |
633 | 631 |
|
634 | 632 | response = FileResponse( |
635 | 633 | output, |
636 | 634 | content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
637 | 635 | ) |
638 | 636 | response["Content-Disposition"] = f"attachment; filename={self.title}.docx" |
| 637 | + |
639 | 638 | return response |
640 | 639 |
|
641 | 640 | def generate_document(self, body, body_type, export_format): |
|
0 commit comments