import pdfkit
import argparse
import os

# --- Command-line arguments ---
parser = argparse.ArgumentParser(description='Generate PDF from HTML with logo and one 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='Single watermark text.')
parser.add_argument('--output', type=str, default='output.pdf', help='Output PDF filename.')
args = parser.parse_args()

# wkhtmltopdf path (adjust if needed)
wkhtmltopdf_path = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path)

# Read HTML file
with open(args.html, "r", encoding="utf-8") as f:
    html_content = f.read()

# Add logo at top-right (fully visible, no opacity)
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; padding: 10px;">
      <img src="{logo_uri}" style="width:120px;">
    </div>
    '''
    html_content = logo_html + html_content

# Add only one centered, large watermark rotated 60° in faint blue
if args.watermark:
    watermark_html = f"""
    <style>
    .single-watermark {{
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%) rotate(60deg);
        font-size: 120px;
        color: rgba(0, 0, 255, 0.08);
        font-weight: bold;
        z-index: -1;
        pointer-events: none;
        white-space: nowrap;
    }}
    </style>
    <div class="single-watermark">{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}")
