andyhill Posted July 11, 2020 Share Posted July 11, 2020 I am trying to get this Print Code to work inside a UniGui HtmlMemo - can anyone please advise why this basic code does not work with UniGui ? HtmlMemo.Lines.Clear; ///////////////////////////////////////////////////////////////////////////////// s:= ' <script> '+ ' function printPage(id) '+ ' { '+ ' var html="<html>"; '+ ' html+= document.getElementById(id).innerHTML; '+ ' html+="</html>"; '+ ' var printWin = window.open('', '', ''left=0, top=0, width=1, height=1, toolbar=0, scrollbars=0, status=0''); '+ ' printWin.document.write(html); '+ ' printWin.document.close(); '+ ' printWin.focus(); '+ ' printWin.print(); '+ ' printWin.close(); '+ ' } '+ ' </script> '+ ' <div id="block"> '; HtmlMemo.Lines.Add(s); ///////////////////////////////////////////////////////////////////////////////// add additional html code here ///////////////////////////////////////////////////////////////////////////////// s:= ' </div> '+ ' <br> '+ ' <input type="button" value="Print" onclick="printPage(''block'');"></input> '; HtmlMemo.Lines.Add(s); Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 11, 2020 Share Posted July 11, 2020 1 hour ago, andyhill said: I am trying to get this Print Code to work inside a UniGui HtmlMemo - can anyone please advise why this basic code does not work with UniGui ? Hello Andy, Please attach the full code, test case to check. Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 11, 2020 Author Share Posted July 11, 2020 Project1.zip Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 12, 2020 Author Share Posted July 12, 2020 Whats happening Sherzod, I gave you the test project ? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 13, 2020 Share Posted July 13, 2020 Hello Andy, I will check. Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 14, 2020 Author Share Posted July 14, 2020 Please advise Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 14, 2020 Share Posted July 14, 2020 28 minutes ago, andyhill said: Please advise Sorry, I have not been able to solve the "problem". Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 14, 2020 Author Share Posted July 14, 2020 Are you still going to try ? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 14, 2020 Share Posted July 14, 2020 Just now, andyhill said: Are you still going to try ? Yes. Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 14, 2020 Author Share Posted July 14, 2020 Thank you Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 14, 2020 Share Posted July 14, 2020 4 hours ago, andyhill said: Are you still going to try ? Hello Andy, I solved the case like this. 1. HtmlMemo.ClientEvents.ExtEvents: function afterrender(sender, eOpts) { printPage = function(id) { var html = "<html>"; html += sender.getDoc().getElementById(id).innerHTML; html += "</html>"; var printWin = window.open('', '', 'left=0, top=0, width=1, height=1, toolbar=0, scrollbars=0, status=0'); printWin.document.write(html); printWin.document.close(); printWin.focus(); printWin.print(); printWin.close(); } } 2. FormCreate: procedure TMainForm.UniFormCreate(Sender: TObject); var s: String; begin HtmlMemo.Lines.Clear; ////////////////////////////////////////////////////////////////////////////// s:= '<div id="block"> '; HtmlMemo.Lines.Add(s); ////////////////////////////////////////////////////////////////////////////// s:= ' What is Lorem Ipsum?<br><br>'+ ' Lorem Ipsum is simply dummy text of the printing and typesetting industry. '+ ' Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, '+ ' when an unknown printer took a galley of type and scrambled it to make a type specimen book. '+ ' It has survived not only five centuries, but also the leap into electronic typesetting, '+ ' remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset '+ ' sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like '+ ' Aldus PageMaker including versions of Lorem Ipsum.'; HtmlMemo.Lines.Add(s); ////////////////////////////////////////////////////////////////////////////// s:= '</div> '+ '<br> '+ '<input type="button" value="Print" onclick="top.printPage(''block'');"></input> '; HtmlMemo.Lines.Add(s); end; Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 14, 2020 Author Share Posted July 14, 2020 7-0-0 1531 Does not work ? procedure TMainForm.UniFormCreate(Sender: TObject); var s: String; begin ////////////////////////////////////////////////////////////////////////////// HtmlMemo.ClientEvents.ExtEvents.Clear; s:= 'afterrender=function afterrender(sender, eOpts) '+ '{ '+ ' printPage = function(id) '+ ' { '+ ' var html = "<html>"; '+ ' html += sender.getDoc().getElementById(id).innerHTML; '+ ' html += "</html>"; '+ ' var printWin = window.open('''', '''', ''left=0, top=0, width=1, height=1, toolbar=0, scrollbars=0, status=0''); '+ ' printWin.document.write(html); '+ ' printWin.document.close(); '+ ' printWin.focus(); '+ ' printWin.print(); '+ ' printWin.close(); '+ ' } '+ '} '; HtmlMemo.ClientEvents.ExtEvents.Add(s); ////////////////////////////////////////////////////////////////////////////// HtmlMemo.Lines.Clear; s:= '<div id="block"> '+ ' What is Lorem Ipsum?<br><br>'+ ' Lorem Ipsum is simply dummy text of the printing and typesetting industry. '+ ' Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, '+ ' when an unknown printer took a galley of type and scrambled it to make a type specimen book. '+ ' It has survived not only five centuries, but also the leap into electronic typesetting, '+ ' remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset '+ ' sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like '+ ' Aldus PageMaker including versions of Lorem Ipsum.'; HtmlMemo.Lines.Add(s); ////////////////////////////////////////////////////////////////////////////// s:= '</div> '+ '<br> '+ '<input type="button" value="Print" onclick="printPage(''block'');"></input> '; HtmlMemo.Lines.Add(s); end; Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 15, 2020 Author Share Posted July 15, 2020 ' printPage = function(id) '+ ' { '+ ' alert(''Here''); '+ ' var html = "<html>"; '+ Alert Never Fires ? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 15, 2020 Share Posted July 15, 2020 2 hours ago, andyhill said: ' printPage = function(id) '+ ' { '+ ' alert(''Here''); '+ ' var html = "<html>"; '+ Alert Never Fires ? Well, works for me: Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 15, 2020 Author Share Posted July 15, 2020 No, not here, modified project attached - plus EXE Project1-Modified.zip Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 15, 2020 Share Posted July 15, 2020 18 minutes ago, andyhill said: No, not here, modified project attached - plus EXE Project1-Modified.zip Your mistakes: 1. onclick="printPage(''block'');" Should be: onclick="top.printPage(''block'');" 16 hours ago, Sherzod said: s:= '</div> '+ '<br> '+ '<input type="button" value="Print" onclick="top.printPage(''block'');"></input> '; 2. printPage = function(id) { Alert('Here'); Should be: printPage = function(id) { alert('Here'); Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 15, 2020 Author Share Posted July 15, 2020 Sorry, I over looked that extra piece of info "top." can you please explain why I need that - thanks Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 15, 2020 Share Posted July 15, 2020 12 minutes ago, andyhill said: "top." can you please explain why I need that Since, we declared a function in the topmost window. The top property returns the topmost browser window of the current window. Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 15, 2020 Author Share Posted July 15, 2020 Thank you again Sherzod. Moving forward, is there a way to execute that function elsewhere in my form other than the input onclick event buried in the html code. For example I could place a button on the ToolBar, then on that button's click event execute that javascript function ? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted July 15, 2020 Share Posted July 15, 2020 12 minutes ago, andyhill said: For example I could place a button on the ToolBar, then on that button's click event execute that javascript function ? You can try to use like this, for example: procedure TMainForm.UniButton1Click(Sender: TObject); begin UniSession.AddJS('printPage("block")'); end; Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 15, 2020 Author Share Posted July 15, 2020 Fantastic - Thank You Again 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.