Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convert html to judgment #2048

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions peachjam/models/core_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,25 @@ def extract_content_from_source_file(self):

return result

def prepare_content_html_for_pdf(self):
return self.content_html.encode("utf-8")

def convert_html_to_pdf(self):
with tempfile.NamedTemporaryFile(suffix=".html") as html_file:
html_file.write(self.prepare_content_html_for_pdf())
html_file.flush()
html_file.seek(0)

pdf, _ = soffice_convert(html_file, "html", "pdf")
filename = slugify(self.case_name)
SourceFile.objects.update_or_create(
document=self,
defaults={
"file": File(pdf, name=f"{filename}.pdf"),
"mimetype": "application/pdf",
},
)

def is_most_recent(self):
"""Is this the most recent document for this work?

Expand Down
13 changes: 13 additions & 0 deletions peachjam/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ def convert_source_file_to_pdf(source_file_id):
log.info("Conversion to PDF done")


@background(queue="peachjam", remove_existing_tasks=True)
def convert_html_to_pdf(doc_id):
from peachjam.models import CoreDocument

doc = CoreDocument.objects.filter(id=doc_id).first()
logger.info(f"Creating PDF from HTML for document {doc_id}")
if not doc:
logger.warning("Document not found")
return
doc.convert_html_to_pdf()
logger.info("Done")


@background(queue="peachjam", remove_existing_tasks=True)
def rank_works():
from peachjam.graph.ranker import GraphRanker
Expand Down
Loading