Jump to content

Disabled Ctrl+P and Ctrl+S in UniPDFFrame


EDOM

Recommended Posts

It can hide Toolbar button(print/download) in FrameLoaded event

but how can I prevent user use Ctrl+P to print and Ctrl+S to download PDF from UniPDFFrame

procedure TUniFrame_Employee.UniPDFFrameFrameLoaded(Sender: TObject);
begin
  //Hide download button
    UniSession.AddJS (UniPDFFrame_Cert.JSName + '.iframe.contentWindow.document.'  + 'getElementById ("download").style.display = "none"');
    UniSession.AddJS (UniPDFFrame_Cert.JSName + '.iframe.contentWindow.document.'  + 'getElementById ("secondaryDownload").style.display = "none"');

  // Hide print button
  UniSession.AddJS (UniPDFFrame_Cert.JSName + '.iframe.contentWindow.document.' + 'getElementById ("print").style.display = "none"');
  UniSession.AddJS (UniPDFFrame_Cert.JSName + '.iframe.contentWindow.document.' + 'getElementById ("secondaryPrint").style.display = "none"');
end;

Link to comment
Share on other sites

2 hours ago, EDOM said:

how can I prevent user use Ctrl+P

One possible solution:

procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  // Disable printing
  UniPDFFrame1.JSInterface.JSCode(#1'.iframe.contentWindow.print=Ext.emptyFn;');
end;

 

  • Like 1
Link to comment
Share on other sites

3 hours ago, EDOM said:

but how can I prevent user use Ctrl+P to print and Ctrl+S to download PDF from UniPDFFrame

1. UniPDFFrame1.ClientEvents.UniEvents ->

function afterCreate(sender) 
{
    sender.disableKeysPrintSave = function() {
        // disable printing
        sender.iframe.contentWindow.print=Ext.emptyFn;
        // disable save
        $(sender.iframe.contentDocument).bind('keydown', function(e) {
            if (e.ctrlKey && (e.which == 83)) {
                e.preventDefault();
                return false;
            }
        });
        
    };
}

2. Usage:

procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  UniPDFFrame1.JSInterface.JSCall('disableKeysPrintSave', []);
end;

 

  • Like 2
Link to comment
Share on other sites

15 hours ago, Sherzod said:

1. UniPDFFrame1.ClientEvents.UniEvents ->


function afterCreate(sender) 
{
    sender.disableKeysPrintSave = function() {
        // disable printing
        sender.iframe.contentWindow.print=Ext.emptyFn;
        // disable save
        $(sender.iframe.contentDocument).bind('keydown', function(e) {
            if (e.ctrlKey && (e.which == 83)) {
                e.preventDefault();
                return false;
            }
        });
        
    };
}

2. Usage:


procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  UniPDFFrame1.JSInterface.JSCall('disableKeysPrintSave', []);
end;

 

Hi Sherzod
  
It works, thank you for your kind assistance!

  • Like 1
Link to comment
Share on other sites

Hi Sherzod

For security issue, I should keep logs when user have permission to download or print file
How can I detect when user triggered download and print event in UniPDFFrame ?
include clicked toolbar menu and use shortcut (Ctrl+P & Ctrl+S)

Thank you very much and looking forward to hearing from you.

 

Link to comment
Share on other sites

22 minutes ago, EDOM said:

For security issue, I should keep logs when user have permission to download or print file
How can I detect when user triggered download and print event in UniPDFFrame ?
include clicked toolbar menu and use shortcut (Ctrl+P & Ctrl+S)

Thank you very much and looking forward to hearing from you.

Hi,

Well, I think you can use Ctrl+S to track downloads.

Link to comment
Share on other sites

39 minutes ago, EDOM said:

I should keep logs when user have permission to download

I do not take into account the previous codes.

1.

function afterCreate(sender)
{
    var me = sender;
    me.saveLog = function() {
        console.log("Saved...");
        //ajaxRequest...
    };
    
    me.attachLogEvents = function() {
        me.iframe.contentDocument.getElementById('download').addEventListener('click', me.saveLog);
        me.iframe.contentDocument.getElementById('secondaryDownload').addEventListener('click', me.saveLog);
        $(me.iframe.contentDocument).bind('keydown', function(e) {
            if (e.ctrlKey && (e.which == 83)) {
                me.saveLog();
            }
        });
    };
}

2.

procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  (Sender as TUniPDFFrame).JSInterface.JSCall('attachLogEvents', []);
end;

 

Link to comment
Share on other sites

On 7/7/2020 at 5:51 PM, Sherzod said:

I do not take into account the previous codes.

1.


function afterCreate(sender)
{
    var me = sender;
    me.saveLog = function() {
        console.log("Saved...");
        //ajaxRequest...
    };
    
    me.attachLogEvents = function() {
        me.iframe.contentDocument.getElementById('download').addEventListener('click', me.saveLog);
        me.iframe.contentDocument.getElementById('secondaryDownload').addEventListener('click', me.saveLog);
        $(me.iframe.contentDocument).bind('keydown', function(e) {
            if (e.ctrlKey && (e.which == 83)) {
                me.saveLog();
            }
        });
    };
}

2.


procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  (Sender as TUniPDFFrame).JSInterface.JSCall('attachLogEvents', []);
end;

 

Hi Sherzod,
Thank you for the prompt reply.
Your solution can handle Download & Ctrl+S event without problem.
I try to add the events to Print & Ctrl+P but failed in Ctrl+P (click toolbar print button can detect)
Could you please help to check on this issue.

var me = sender;
    me.saveLog_download = function() {
        console.log("Download...");
        //ajaxRequest...
        ajaxRequest(sender, "Download", ["action=dwonload"]);
    };
    me.saveLog_print = function() {
        console.log("Print...");
        //ajaxRequest...
        ajaxRequest(sender, "Print", ["action=print"]);
    };   
    
    me.attachLogEvents = function() {
        //Download
        me.iframe.contentDocument.getElementById('download').addEventListener('click', me.saveLog_download);
        me.iframe.contentDocument.getElementById('secondaryDownload').addEventListener('click', me.saveLog_download);
        //Print
        me.iframe.contentDocument.getElementById('print').addEventListener('click', me.saveLog_print);
        me.iframe.contentDocument.getElementById('secondaryPrint').addEventListener('click', me.saveLog_print);
        
        $(me.iframe.contentDocument).bind('keydown', function(e) {
            if (e.ctrlKey && (e.which == 80)) {  //<-- didn't work
                me.saveLog_print();
            }

        });
    };     

Link to comment
Share on other sites

2 hours ago, EDOM said:

I try to add the events to Print & Ctrl+P but failed in Ctrl+P (click toolbar print button can detect)
Could you please help to check on this issue.

Hello,

I will try to analyze for print too.
But keep in mind, in any case, you cannot be sure that the user has printed the page, because he can open the print dialog and print, and also cancel printing...

  • Like 1
Link to comment
Share on other sites

23 hours ago, Sherzod said:

Hello,

I will try to analyze for print too.
But keep in mind, in any case, you cannot be sure that the user has printed the page, because he can open the print dialog and print, and also cancel printing...

Hi Sherzod,
  Thank you for all the good advice you have given me.
I am looking forward to hearing from you.

Link to comment
Share on other sites

Hi,

50 minutes ago, EDOM said:

Thank you for all the good advice you have given me.
I am looking forward to hearing from you.

Thank you.

You can try this approach:

procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  (Sender as TUniPDFFrame).JSInterface.JSCode(#1'.iframe.contentWindow.onafterprint=function(){ajaxRequest('#1', "pdialogclosed", [])};');
end;
procedure TMainForm.UniPDFFrame1AjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
  if EventName = 'pdialogclosed' then
    ShowMessage('pdialogclosed');

end;

 

  • Like 1
Link to comment
Share on other sites

On 7/10/2020 at 12:28 PM, Sherzod said:

You can try this approach:


procedure TMainForm.UniPDFFrame1FrameLoaded(Sender: TObject);
begin
  (Sender as TUniPDFFrame).JSInterface.JSCode(#1'.iframe.contentWindow.onafterprint=function(){ajaxRequest('#1', "pdialogclosed", [])};');
end;

procedure TMainForm.UniPDFFrame1AjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
  if EventName = 'pdialogclosed' then
    ShowMessage('pdialogclosed');

end;

 

Hi Sherzod,

  Thank you very much, it works perfectly!

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...