Jump to content

Automatically closing the process dialog (TUniPDFFrame)


Recommended Posts

Posted

Hi,

I use the TUniPDFFrame component. A PDF document is displayed in this component. During the manually started printing process, a progress dialog appears with a done button. Is there a way to automatically close this dialog after printing is complete?

image.png.2dd6bca40ab5e312e0f14999d80fe7f6.png

Best regards

Posted
16 hours ago, bbm said:

I use the TUniPDFFrame component. A PDF document is displayed in this component. During the manually started printing process, a progress dialog appears with a done button. Is there a way to automatically close this dialog after printing is complete?

Hello,

I'll check it.

  • 1 month later...
  • 4 months later...
  • 2 years later...
  • 10 months later...
  • 3 weeks later...
Posted

Hello

@bbm

@SISBLU Software

@Wilton Ergon

@Roberto Nicchi

@KingOrmon

@Frederick

One possible solution:

1. MainForm.Script ->

function setupAutoClosePrintDialog(iframe, delayMs) {
    if (!iframe || !iframe.contentWindow || !iframe.contentDocument) return;

    function watchPrintDialog() {
        var doc = iframe.contentDocument;
        var timer = null;

        if (iframe.__pdfPrintObserver) {
            iframe.__pdfPrintObserver.disconnect();
            iframe.__pdfPrintObserver = null;
        }

        var observer = new MutationObserver(function() {
            var percentSpan = doc.querySelector('.dialog [data-l10n-id="print_progress_percent"]');
            var btn = doc.querySelector('.dialog #printCancel.overlayButton');
            if (
                percentSpan && percentSpan.textContent.trim() === "100%" &&
                btn && btn.offsetParent !== null
            ) {
                if (timer) return;
                timer = setTimeout(function() {
                    var percentAgain = doc.querySelector('.dialog [data-l10n-id="print_progress_percent"]');
                    var btnAgain = doc.querySelector('.dialog #printCancel.overlayButton');
                    if (
                        percentAgain && percentAgain.textContent.trim() === "100%" &&
                        btnAgain && btnAgain.offsetParent !== null
                    ) {
                        btnAgain.click();
                        observer.disconnect();
                        iframe.__pdfPrintObserver = null;
                        //console.log("Dialog auto-closed at 100% (no text check, delay: " + delayMs + " ms)");
                    }
                }, delayMs || 2000);
            } else {
                if (timer) {
                    clearTimeout(timer);
                    timer = null;
                }
            }
        });
        observer.observe(doc.body, { childList: true, subtree: true });

        iframe.__pdfPrintObserver = observer;
    }

    iframe.contentWindow.addEventListener('beforeprint', watchPrintDialog);
}

2. Usage, OnFrameLoaded ->

procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  JSInterface.JSCallGlobalDefer('setupAutoClosePrintDialog', [JSInterface.JSStatement((Sender as TUniPDFFrame).JSName + '.iframe')], 1000);
end;

 

Posted

Thanks for the code.

I put the script code in the MainForm but the event code in another form since the report is printed from the latter.

Unfortunately, the dialogue box still remains on the screen after the print button is clicked and the print dialogue box is then cancelled.

Note: Even if I put the script code in the same form as the TUniPDFFrame control, the result is still the same.

Please see the attached video.

Posted

The code is as you have provided:-

procedure TfrmReports.pdfFrameFrameLoaded(Sender: TObject);
begin
   JSInterface.JSCallGlobalDefer('setupAutoClosePrintDialog', [JSInterface.JSStatement((Sender as TUniPDFFrame).JSName + '.iframe')], 1000);
end;

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...