-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathmain.py
More file actions
37 lines (31 loc) · 1.04 KB
/
main.py
File metadata and controls
37 lines (31 loc) · 1.04 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
import pdfkit
import os
# Download wkhtmltopdf from https://wkhtmltopdf.org/downloads.html
# Set the path to the wkhtmltopdf executable
wkhtmltopdf_path = r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
# Configure pdfkit to use wkhtmltopdf
config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path)
# Path of HTML and PDF files
path = os.getcwd()
htmlFile = f"{path}\\index.html"
pdfFile = f"{path}\\output.pdf"
# Adding PDF Options for customized view
options = {
"page-size": "A4",
"margin-top": "0.75in",
"margin-right": "0.75in",
"margin-bottom": "0.75in",
"margin-left": "0.75in",
"encoding": "UTF-8",
"no-outline": None,
}
# Check if the HTML file exists before proceeding
if not os.path.exists(htmlFile):
print(f"HTML file does not exist at: {htmlFile}")
else:
try:
# Convert HTML to PDF
pdfkit.from_file(htmlFile, pdfFile, configuration=config, options=options)
print(f"Successfully converted HTML to PDF: {pdfFile}")
except Exception as e:
print(f"An error occurred: {e}")