import pdfkit
import argparse
import os

# Command-line arguments
parser = argparse.ArgumentParser(description='Generate PDF from HTML with logo and watermark.')
parser.add_argument('--html', type=str, required=True, help='Path to input HTML file.')
parser.add_argument('--logo', type=str, required=False, help='Path to logo image (optional).')
parser.add_argument('--watermark', type=str, required=False, help='Watermark text (optional).')
parser.add_argument('--output', type=str, default='output.pdf', help='Output PDF filename.')
args = parser.parse_args()

# Path to wkhtmltopdf (adjust this if needed)
wkhtmltopdf_path = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path)

# Load HTML
with open(args.html, "r", encoding="utf-8") as f:
    html_content = f.read()

# Add logo (optional)
if args.logo and os.path.exists(args.logo):
    logo_path = os.path.abspath(args.logo).replace('\\', '/')
    logo_uri = f'file:///{logo_path}'
    logo_html = f'<div style="text-align:right;"><img src="{logo_uri}" style="width:100px;"></div>'
    html_content = logo_html + html_content

# Add watermark (optional)
if args.watermark:
    watermark_html = f"""
    <div style="position: fixed; top: 45%; left: 20%; width: 60%; 
                text-align: center; font-size: 60px; color: rgba(150,150,150,0.15); 
                transform: rotate(-30deg); z-index: -1;">
        {args.watermark}
    </div>
    """
    html_content = watermark_html + html_content

# PDF options
options = {
    'enable-local-file-access': ''
}

# Generate PDF
try:
    pdfkit.from_string(html_content, args.output, configuration=config, options=options)
    print(f"✅ PDF generated: {args.output}")
except Exception as e:
    print(f"❌ Error: {e}")
