-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_pdf.py
More file actions
71 lines (58 loc) · 2.5 KB
/
Copy pathexport_pdf.py
File metadata and controls
71 lines (58 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# export_pdf.py
import os
from dotenv import load_dotenv
from jinja2 import Environment, FileSystemLoader, select_autoescape
import markdown as md
import pdfkit
import json
load_dotenv()
WKHTMLTOPDF_PATH = os.getenv("WKHTMLTOPDF_PATH", "")
def _pdfkit_config():
if WKHTMLTOPDF_PATH and os.path.exists(WKHTMLTOPDF_PATH):
return pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_PATH)
# Nếu bạn đã set PATH hệ thống cho wkhtmltopdf, có thể trả về None
return None
# Mode A: Markdown -> HTML -> PDF (dễ áp dụng ngay cho tailored_resume.md)
def export_markdown_to_pdf(md_path: str, pdf_path: str, title: str = "Curriculum Vitae"):
if not os.path.exists(md_path):
raise FileNotFoundError(f"Không tìm thấy file Markdown: {md_path}")
# Đọc markdown & convert sang HTML
with open(md_path, "r", encoding="utf-8") as f:
md_text = f.read()
html_content = md.markdown(md_text, extensions=["extra", "sane_lists", "tables"])
# Load template wrapper
env = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape(["html", "xml"])
)
template = env.get_template("cv_wrapper.html")
final_html = template.render(title=title, content=html_content)
# Đảm bảo thư mục output tồn tại
os.makedirs(os.path.dirname(pdf_path), exist_ok=True)
# Xuất PDF
config = _pdfkit_config()
pdfkit.from_string(final_html, pdf_path, configuration=config)
print(f"✅ Đã xuất PDF: {pdf_path}")
# Mode B: JSON -> Template (cv_basic.html) -> PDF (nâng cao)
def export_json_to_pdf(json_path: str, pdf_path: str, template_name: str = "cv_basic.html"):
if not os.path.exists(json_path):
raise FileNotFoundError(f"Không tìm thấy file JSON: {json_path}")
with open(json_path, "r", encoding="utf-8") as f:
data = json.load(f)
env = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape(["html", "xml"])
)
template = env.get_template(template_name)
final_html = template.render(data=data)
os.makedirs(os.path.dirname(pdf_path), exist_ok=True)
config = _pdfkit_config()
pdfkit.from_string(final_html, pdf_path, configuration=config)
print(f"✅ Đã xuất PDF: {pdf_path}")
if __name__ == "__main__":
# Ví dụ chạy nhanh (Mode A) với file outputs/tailored_resume.md:
export_markdown_to_pdf(
md_path="outputs/tailored_resume.md",
pdf_path="outputs/CV_ATS.pdf",
title="CV - ATS"
)