Jump to content

Print UniHTMLFrame-Content?


erich.wanker

Recommended Posts

Hello Folks :-)

 

i try to print the content of a UniHTMLFrame .. but find no solution ..

 

 

 

i tried out:

extevent: window.print
UniSession.AddJs(UniHTMLFrame1.jsname+'.iframeEl.dom.contentWindow.print();');

and

var frm = BERICHT.UniHTMLFrame1.iframe;
if (frm) {
printIframe(frm);

any ideas?

 

THANX

Link to comment
Share on other sites

  • 1 month later...

Hi! I have an issue with printing too. I'm using the Chrome brouser and try to print a TUniHTMLFrame. The code is simple:

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
 UniHTMLFrame1.HTML.Text :=
  '<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' +
  '<title>HTML Document</title> </head> <body> <p> <b> This text is bold,<i>and this one is also italic</i></b></p></body></html>';
 UniSession.AddJs(UniHTMLFrame1.jsname + '.iframeEl.dom.contentWindow.print();');
end;

Dpr:

  object UniHTMLFrame1: TUniHTMLFrame
    Left = 8
    Top = 8
    Width = 785
    Height = 465
    Hint = ''
  end

And I get the ajax error:

 

Cannot read property 'dom' of undefined

 
O17.iframeEl.dom.contentWindow.print();O17.update(" <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>HTML Document</title> </head> <body> <p> <b> This text is bold,<i>and this one is also italic</i></b></p></body></html>\n",true);
 
Any suggestions?
Link to comment
Share on other sites

For print html, i'm using uniHtmlMemo but i think it must work with unihtmlframe.

 

i have a dedicated unimemo for print working .

 

in memoExport.Clientsevent.extevents

function dirtychange(sender, isDirty, eOpts)
{
    ajaxRequest(sender, "changeforprint", []);
}

memoexport.onAjaxevent :

 if EventName = 'changeforprint' then
  begin
    if MemoExport.Text<>'' then
    begin
      UniSession.AddJs(
      'var browserName = navigator.userAgent.toLowerCase(); '+
      'if ( browserName.indexOf("msie") != -1)  '+
      '{'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.focus();'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.print();'+
      '} '+
     'else '+
     '{'+
       'if(browserName.indexOf("trident") != -1) '+
       '{'+
         MemoExport.jsname+'.iframeEl.dom.contentWindow.focus();'+
         MemoExport.jsname+'.iframeEl.dom.contentWindow.document.execCommand(''print'', false, null);'+
       '}'+
       'else '+
       '{'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.focus();'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.print();'+
       '};'+
      '};');
      MemoExport.Text:='';
    end;

if you want print a html, i load it in memoexport

MemoExport.Text:= GetHtmlPrint;

Important : the uniMemoHtml must be visible = true if you don't want see it set top = -1000;

Link to comment
Share on other sites

Sorry, this code doesn't works:

UniHTMLFrame1.JSInterface.JSCode('Ext.defer(function(){'#1'.iframe.contentWindow.print()}, 100);');


Message from browser console:

 

VM265:1 Uncaught TypeError: Cannot read property 'contentWindow' of undefined

    at eval (eval at AjaxSuccess (ext-unicommon-min.js:8), <anonymous>:1:33)

Link to comment
Share on other sites

for test, could you try with 2 button.

Button1click :

UniHTMLFrame1.HTML.Text :=
'<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' +
'<title>HTML Document</title> </head> <body> <p> <b> This text is bold,<i>and this one is also italic</i></b></p></body></html>';

 

button2click :

UniHTMLFrame1.JSInterface.JSCode('Ext.defer(function(){'#1'.iframe.contentWindow.print()}, 100);');

O17.iframeEl.dom.contentWindow.print();O17.update(" <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>HTML Document</title> </head> <body> <p> <b> This text is bold,<i>and this one is also italic</i></b></p></body></html>\n",true);

it seems unigui launch print before load html. so if there is no html in frame, there is no dom/content but maybe i am wrong

Link to comment
Share on other sites

for test, could you try with 2 button.

Button1click :

UniHTMLFrame1.HTML.Text :=

'<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' +

'<title>HTML Document</title> </head> <body> <p> <b> This text is bold,<i>and this one is also italic</i></b></p></body></html>';

 

button2click :

UniHTMLFrame1.JSInterface.JSCode('Ext.defer(function(){'#1'.iframe.contentWindow.print()}, 100);');

O17.iframeEl.dom.contentWindow.print();O17.update(" <!DOCTYPE html> <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>HTML Document</title> </head> <body> <p> <b> This text is bold,<i>and this one is also italic</i></b></p></body></html>\n",true);

it seems unigui launch print before load html. so if there is no html in frame, there is no dom/content but maybe i am wrong

 

Yes, you are right.

 

For this case need to use OnFrameLoaded event, like this:

http://forums.unigui.com/index.php?/topic/8811-switch-between-url-and-html-of-urlframe/&do=findComment&comment=45422

Link to comment
Share on other sites

  • 1 year later...
On 8/16/2017 at 4:27 PM, delagoutte said:

For print html, i'm using uniHtmlMemo but i think it must work with unihtmlframe.

 

i have a dedicated unimemo for print working .

 

in memoExport.Clientsevent.extevents


function dirtychange(sender, isDirty, eOpts)
{
    ajaxRequest(sender, "changeforprint", []);
}

memoexport.onAjaxevent :


 if EventName = 'changeforprint' then
  begin
    if MemoExport.Text<>'' then
    begin
      UniSession.AddJs(
      'var browserName = navigator.userAgent.toLowerCase(); '+
      'if ( browserName.indexOf("msie") != -1)  '+
      '{'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.focus();'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.print();'+
      '} '+
     'else '+
     '{'+
       'if(browserName.indexOf("trident") != -1) '+
       '{'+
         MemoExport.jsname+'.iframeEl.dom.contentWindow.focus();'+
         MemoExport.jsname+'.iframeEl.dom.contentWindow.document.execCommand(''print'', false, null);'+
       '}'+
       'else '+
       '{'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.focus();'+
        MemoExport.jsname+'.iframeEl.dom.contentWindow.print();'+
       '};'+
      '};');
      MemoExport.Text:='';
    end;

if you want print a html, i load it in memoexport


MemoExport.Text:= GetHtmlPrint;

Important : the uniMemoHtml must be visible = true if you don't want see it set top = -1000;

Thank you, your example worked for me!

Link to comment
Share on other sites

i have a new method if you want :

add uses ExtPascalUtils;

Add a TuniHtmlMemo (MemoExport) on your form and set "top" to -1000 to hide it (don't use visible property)

on ClientEvent.Extevents add this code in "push" :

function push(sender, html, eOpts)
{
  if ((html.trim() != "")&&(html.trim() != "&#8203;")){
    var browserName = navigator.userAgent.toLowerCase(); 
      if ( browserName.indexOf("msie") != -1)  
      {
        sender.iframeEl.dom.contentWindow.focus();
        sender.iframeEl.dom.contentWindow.print();
      } 
      else 
     {
       if(browserName.indexOf("trident") != -1) 
       {
         sender.iframeEl.dom.contentWindow.focus();
         sender.iframeEl.dom.contentWindow.document.execCommand('print', false, null);
       }
       else 
       {
        sender.iframeEl.dom.contentWindow.focus();
        sender.iframeEl.dom.contentWindow.print();
       };
      };
  }

}

Now all is ready.

for printing html do this :

procedure TMainForm.PrintText(Sender: TObject; aValue : string);
begin
  UniSession.AddJS('if (!'+MemoExport.JSName+'.initialized){'+MemoExport.JSName+'.initialized=true;};');// i must add this for chrome at some users. sometimes the component is not initialized
  UniSession.AddJS(MemoExport.JSName+'.textareaEl.dom.value='+ StrToJS(aValue)+';'+MemoExport.JSName+'.pushValue();')
end;


..
procedure TMainForm.BtnPrintSelClick(Sender: TObject);
begin
  PrintText(Sender,GetExportHtml);
end;

 

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

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...