Skip to content

Commit 94ff7c6

Browse files
committed
Add script to convert HTML files to PDF
Introduces htmltopdf.py, a Python script that converts HTML files matching given patterns to PDF using xhtml2pdf. The script supports wildcard patterns, handles errors, and provides user feedback during processing.
1 parent 45ad0f2 commit 94ff7c6

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Programming/htmltopdf.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import glob
5+
from xhtml2pdf import pisa
6+
7+
def convert_html_to_pdf(file_patterns):
8+
# 1. Collect all files based on patterns
9+
files_to_process = []
10+
for pattern in file_patterns:
11+
# glob expands the wildcard pattern for Windows terminals
12+
matches = glob.glob(pattern)
13+
if not matches:
14+
print(f"Warning: No files found matching pattern '{pattern}'")
15+
files_to_process.extend(matches)
16+
17+
files_to_process = sorted(list(set(files_to_process)))
18+
19+
if not files_to_process:
20+
print("No files to process.")
21+
return
22+
23+
print(f"Found {len(files_to_process)} files. Starting conversion...\n")
24+
25+
# 2. Process files
26+
for input_file in files_to_process:
27+
base_name = os.path.splitext(input_file)[0]
28+
output_file = f"{base_name}.pdf"
29+
30+
print(f"Converting: {input_file} -> {output_file}")
31+
32+
try:
33+
# Open input file (HTML) and output file (PDF)
34+
with open(input_file, "r", encoding='utf-8') as source_html:
35+
with open(output_file, "wb") as result_pdf:
36+
# Convert
37+
pisa_status = pisa.CreatePDF(
38+
source_html, # the HTML file handle
39+
dest=result_pdf # the PDF file handle
40+
)
41+
42+
# Check for errors in the conversion log
43+
if pisa_status.err:
44+
print(f" [ERROR] Failed to convert {input_file}")
45+
else:
46+
print(f" [OK] Saved {output_file}")
47+
48+
except Exception as e:
49+
print(f" [EXCEPTION] Error converting {input_file}: {e}")
50+
51+
print("\nProcessing complete.")
52+
53+
if __name__ == "__main__":
54+
if len(sys.argv) < 2:
55+
print("Usage: python htmltopdf.py <file_pattern>")
56+
print("Example: python htmltopdf.py *.html")
57+
else:
58+
convert_html_to_pdf(sys.argv[1:])

0 commit comments

Comments
 (0)