Jump to content

HtmlMemo Print Revisited


andyhill

Recommended Posts

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);

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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');    

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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