Files
resume/convert.py
T

64 lines
1.9 KiB
Python
Raw Normal View History

2026-07-11 10:16:46 +08:00
import subprocess
import sys
# Install required packages
def install_packages():
packages = ['imgkit', 'Pillow']
for package in packages:
try:
__import__(package.lower().replace('-', '_'))
except ImportError:
print(f"Installing {package}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
install_packages()
import imgkit
from PIL import Image
import os
def html_to_jpg(html_file, output_file, width=2478):
"""Convert HTML file to high-quality JPG"""
# First, check if wkhtmltopdf/wkhtmltoimage is installed
try:
subprocess.run(['wkhtmltoimage', '--version'], capture_output=True, check=True)
except FileNotFoundError:
print("wkhtmltoimage not found. Please install it:")
print("sudo apt-get install wkhtmltopdf")
print("Or download from: https://wkhtmltopdf.org/downloads.html")
return False
# Convert HTML to JPG using imgkit
options = {
'format': 'jpg',
'quality': 100,
'width': width,
'enable-local-file-access': '',
'encoding': 'UTF-8',
'no-stop-slow-scripts': '',
}
try:
imgkit.from_file(html_file, output_file, options=options)
print(f"Successfully converted {html_file} to {output_file}")
# Verify the output
with Image.open(output_file) as img:
print(f"Output image size: {img.size}")
print(f"Output image mode: {img.mode}")
return True
except Exception as e:
print(f"Error converting HTML to JPG: {e}")
return False
if __name__ == "__main__":
html_file = "/home/wonder/code/resume/resume.html"
output_file = "/home/wonder/code/resume/resume_output.jpg"
if html_to_jpg(html_file, output_file):
print(f"\nOutput saved to: {output_file}")
else:
print("Conversion failed!")