Chris4K commited on
Commit
1e01317
·
verified ·
1 Parent(s): 3b7d8f2

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +10 -16
utils.py CHANGED
@@ -24,6 +24,7 @@ def create_md(text, title):
24
  f.write(text[x])
25
  return title
26
 
 
27
 
28
  def convert(filename):
29
  with open(f"{filename}.md", "r") as f:
@@ -34,21 +35,14 @@ def convert(filename):
34
  f.write(html)
35
  return f"{filename}"
36
 
 
 
37
 
38
  def to_pdf(filename):
39
- if platform.system() == "Windows":
40
- path_to_wkhtmltopdf = r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
41
-
42
- # Point pdfkit configuration to wkhtmltopdf.exe
43
- config = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf)
44
-
45
- # Convert HTML file to PDF
46
- pdfkit.from_file(
47
- f"{filename}.html", output_path=f"{filename}.pdf", configuration=config
48
- )
49
- return f"{filename}.pdf"
50
- else:
51
- pdfkit.from_file(
52
- f"{filename}.html", output_path=f"{filename}.pdf", configuration=config
53
- )
54
- return f"{filename}.pdf"
 
24
  f.write(text[x])
25
  return title
26
 
27
+
28
 
29
  def convert(filename):
30
  with open(f"{filename}.md", "r") as f:
 
35
  f.write(html)
36
  return f"{filename}"
37
 
38
+ from weasyprint import HTML
39
+ import platform
40
 
41
  def to_pdf(filename):
42
+ html_file = f"{filename}.html"
43
+ pdf_file = f"{filename}.pdf"
44
+
45
+ # Convert HTML to PDF using WeasyPrint (cross-platform, no need for external binaries)
46
+ HTML(html_file).write_pdf(pdf_file)
47
+
48
+ return pdf_file