function printContainer(input, options = {}) {
  const defaultOptions = {
    orientation: "portrait",   // 'portrait' or 'landscape'
    header: "",                // HTML string
    footer: "",                // HTML string
    logoUrl: "",               // URL of logo image
    styles: [],                // Array of stylesheet URLs
    headerHeight: 60,          // px
    footerHeight: 40,          // px
    margin: 10                 // mm
  };
  const settings = { ...defaultOptions, ...options };

  // Resolve printable HTML
  let contentHtml;
  const src = document.getElementById(input);
  contentHtml = src ? src.innerHTML : String(input || "");

  // Create hidden iframe
  const iframe = document.createElement("iframe");
  iframe.style.position = "fixed";
  iframe.style.right = "0";
  iframe.style.bottom = "0";
  iframe.style.width = "0";
  iframe.style.height = "0";
  iframe.style.border = "0";
  document.body.appendChild(iframe);

  const doc = iframe.contentDocument || iframe.contentWindow.document;

  // Build minimal document
  doc.open();
  doc.write('<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Print</title></head><body></body></html>');
  doc.close();

  // Attach requested styles
  (settings.styles || []).forEach(href => {
    if (!href) return;
    const link = doc.createElement("link");
    link.rel = "stylesheet";
    link.href = href;
    doc.head.appendChild(link);
  });

  // Print CSS: no flex, no overflow clipping; fixed header/footer; table pagination
  const style = doc.createElement("style");
  style.textContent = `
    @page {
      size: ${settings.orientation === "landscape" ? "landscape" : "portrait"};
      margin: ${settings.margin}mm;
    }
    html, body {
      margin: 0;
      padding: 0;
      -webkit-print-color-adjust: exact;
      print-color-adjust: exact;
    }
    .print-header, .print-footer {
      position: fixed;
      left: 0; right: 0;
      background: #fff;
    }
    .print-header { top: 0; height: ${settings.headerHeight}px; }
    .print-footer { bottom: 0; height: ${settings.footerHeight}px; }
    .print-body {
      /* Reserve space for header and footer on every page */
      padding-top: ${settings.headerHeight}px;
      padding-bottom: ${settings.footerHeight}px;
    }
    /* Tables paginate naturally */
    table { width: 100%; border-collapse: collapse; page-break-inside: auto; }
    thead { display: table-header-group; }
    tfoot { display: table-footer-group; }
    tr { page-break-inside: avoid; page-break-after: auto; }
    th, td { border: 1px solid #ddd; padding: 8px; }
    .noprint { display: none !important; }
  `;
  doc.head.appendChild(style);

  // Header
  if (settings.header) {
    const h = doc.createElement("div");
    h.className = "print-header";
    if (settings.logoUrl) {
      const img = doc.createElement("img");
      img.src = settings.logoUrl;
      img.alt = "Logo";
      img.style.position = "absolute";
      img.style.top = "10px";
      img.style.left = "10px";
      img.style.maxWidth = "100px";
      img.style.maxHeight = Math.max(10, settings.headerHeight - 10) + "px";
      h.appendChild(img);
    }
    const hdr = doc.createElement("div");
    hdr.innerHTML = settings.header;
    h.appendChild(hdr);
    doc.body.appendChild(h);
  }

  // Body
  const body = doc.createElement("div");
  body.className = "print-body";
  body.innerHTML = contentHtml;
  doc.body.appendChild(body);

  // Footer
  if (settings.footer) {function printContainer(input, options = {}) {
  const defaultOptions = {
    orientation: "portrait",   // 'portrait' or 'landscape'
    header: "",                // HTML string (title/content)
    footer: "",                // HTML string
    logoUrl: "",               // URL of logo image (left side)
    styles: [],                // Array of stylesheet URLs
    headerHeight: 60,          // px
    footerHeight: 40,          // px
    margin: 10                 // mm
  };
  const settings = { ...defaultOptions, ...options };

  // Resolve printable HTML
  const src = document.getElementById(input);
  const contentHtml = src ? src.innerHTML : String(input || "");

  // Create hidden iframe
  const iframe = document.createElement("iframe");
  iframe.style.position = "fixed";
  iframe.style.right = "0";
  iframe.style.bottom = "0";
  iframe.style.width = "0";
  iframe.style.height = "0";
  iframe.style.border = "0";
  document.body.appendChild(iframe);

  const doc = iframe.contentDocument || iframe.contentWindow.document;

  // Build minimal document
  doc.open();
  doc.write('<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Print</title></head><body></body></html>');
  doc.close();

  // Attach requested styles
  (settings.styles || []).forEach(href => {
    if (!href) return;
    const link = doc.createElement("link");
    link.rel = "stylesheet";
    link.href = href;
    doc.head.appendChild(link);
  });

  // Print CSS: header title centered, logo pinned left
  const style = doc.createElement("style");
  style.textContent = `
    @page {
      size: ${settings.orientation === "landscape" ? "landscape" : "portrait"};
      margin: ${settings.margin}mm;
    }
    html, body {
      margin: 0;
      padding: 0;
      -webkit-print-color-adjust: exact;
      print-color-adjust: exact;
    }
    /* Fixed header/footer */
    .print-header, .print-footer {
      position: fixed;
      left: 0; right: 0;
      background: #fff;
    }
    .print-header {
      top: 0;
      height: ${settings.headerHeight}px;
      display: flex;
      align-items: center;
      justify-content: center;    /* centers the header content */
      text-align: center;
    }
    /* Logo pinned left. Absolute so center alignment remains true */
    .print-header .print-logo {
      position: absolute;
      left: 10px;
      top: 50%;
      transform: translateY(-50%);
      max-width: 100px;
      max-height: ${Math.max(10, settings.headerHeight - 10)}px;
    }
    .print-header .print-title {
      width: 100%;
      padding: 0 120px; /* keep clear of the logo area on the left */
      box-sizing: border-box;
    }
    .print-footer {
      bottom: 0;
      height: ${settings.footerHeight}px;
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
    }
    /* Reserve space for header/footer on each page */
    .print-body {
      padding-top: ${settings.headerHeight}px;
      padding-bottom: ${settings.footerHeight}px;
    }
    /* Table pagination */
    table { width: 100%; border-collapse: collapse; page-break-inside: auto; }
    thead { display: table-header-group; }
    tfoot { display: table-footer-group; }
    tr { page-break-inside: avoid; page-break-after: auto; }
    th, td { border: 1px solid #ddd; padding: 8px; }
    .noprint { display: none !important; }
  `;
  doc.head.appendChild(style);

  // Header
  if (settings.header) {
    const header = doc.createElement("div");
    header.className = "print-header";

    if (settings.logoUrl) {
      const img = doc.createElement("img");
      img.src = settings.logoUrl;
      img.alt = "Logo";
      img.className = "print-logo";
      header.appendChild(img);
    }

    const titleWrap = doc.createElement("div");
    titleWrap.className = "print-title";
    titleWrap.innerHTML = settings.header; // centered content
    header.appendChild(titleWrap);

    doc.body.appendChild(header);
  }

  // Body
  const body = doc.createElement("div");
  body.className = "print-body";
  body.innerHTML = contentHtml;
  doc.body.appendChild(body);

  // Footer
  if (settings.footer) {
    const footer = doc.createElement("div");
    footer.className = "print-footer";
    footer.innerHTML = settings.footer;
    doc.body.appendChild(footer);
  }

  // Print then clean up this iframe only
  const win = iframe.contentWindow;
  win.onload = function() {
    try { win.focus(); } catch(e) {}
    setTimeout(() => { try { win.print(); } catch(e) {} }, 50);
  };
  win.onafterprint = function() {
    if (iframe && iframe.parentNode) iframe.parentNode.removeChild(iframe);
  };
  // Fallback if onload misses
  setTimeout(() => { try { win.focus(); win.print(); } catch(e) {} }, 800);
}



function removeNoPrintElements(doc) {
  const elements = doc.querySelectorAll(".noprint");
  elements.forEach((el) => (el.style.display = "none"));
}

    const f = doc.createElement("div");
    f.className = "print-footer";
    f.innerHTML = settings.footer;
    doc.body.appendChild(f);
  }

  // Print then clean up this iframe only
  const win = iframe.contentWindow;
  win.onload = function() {
    try { win.focus(); } catch(e) {}
    setTimeout(() => { try { win.print(); } catch(e) {} }, 50);
  };
  win.onafterprint = function() {
    if (iframe && iframe.parentNode) iframe.parentNode.removeChild(iframe);
  };

  // If onload doesn't fire due to cached CSS, still attempt print
  setTimeout(() => { try { win.focus(); win.print(); } catch(e) {} }, 800);
}


function removeNoPrintElements(doc) {
  const elements = doc.querySelectorAll(".noprint");
  elements.forEach((el) => (el.style.display = "none"));
}
