2

I am trying to print document using the method of CHtmlEditCtrl::PrintDocument() as described here (Printing In MFC Application). Below is my test code where I plan to print a pretty big table.

void CMyView::OnFilePrint()
{
    CHtmlEditCtrl PrintCtrl;
    if (!PrintCtrl.Create(NULL, WS_CHILD, CRect(0, 0, 0, 0), this, 1))
    {
        ASSERT(FALSE);
        return;
    }

    CComPtr<IHTMLDocument2> document;
    PrintCtrl.GetDocument(&document);
    WaitForComplete(document);
    CString html = 
        _T("<!doctype html>")
        _T("<html lang=\"en\">")
        _T("  <head>")
        _T("    <meta charset=\"utf-8\">")
        _T("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
        _T("    <title>HTML5 Test Page</title>")
        _T("    <style>")
        _T("      @media print")
        _T("      {")
        _T("        td { overflow-x:auto }")
        _T("      }")
        _T("    </style>")
        _T("    <style type=\"text/css\">")
        _T("      thead { display:table-header-group }")
        _T("      tfoot { display:table-footer-group; page-break-inside:avoid }")
        _T("      tbody { display:table-row-group }")
        _T("      td > div { display:block; page-break-inside:avoid }")
        _T("    </style>")
        _T("  </head>")
        _T("  <body>")
        _T("    <div id=\"top\" class=\"page\" role=\"document\">")
        _T("      <header role=\"banner\">")
        _T("        <p>This is header.</p>")
        _T("      </header>")
        _T("      <main role=\"main\">")
        _T("        <section id=\"text\">")
        _T("          <article id=\"text_tables\">")
        _T("            <table>")
        _T("            <thead>")
        _T("                <tr>")
        _T("                    <th>")
        _T("                        header comes here for each page")
        _T("                    </th>")
        _T("                </tr>")
        _T("            </thead>")
        _T("            <tfoot>")
        _T("                <tr>")
        _T("                    <td>")
        _T("                        footer comes here for each page")
        _T("                    </td>")
        _T("                </tr>")
        _T("            </tfoot>");
        _T("            <tbody>");
    for (int i = 0; i < 200; ++i)
    {
        CString str;
        str.Format(_T("%d"), i);
        html = html + "<tr><td><div>" + str + "</div></td></tr>";
    }
    html = html + "</tbody></table></article></section></main>";
    html = html + "<footer role=\"contentinfo\"><p>This is footer</p></footer></div></body></html>";
    PrintCtrl.SetDocumentHTML(html);
    WaitForComplete(document);
    //PrintCtrl.PrintPreview();
    PrintCtrl.PrintDocument();
}

The problem is for every line near the page bottom, there seems to be a mess of spacing. See attached picture below, note that 41 has more spacing from 40 and gets partially cut off. How does it happen and how to fix it?

enter image description here

I try adding repeated header and footer, below is part of the result that displaying error.

enter image description here

I try using "td {overflow-x:auto;}". The table footer of the first page is not shown correctly.

enter image description here

user180574
  • 5,681
  • 13
  • 53
  • 94
  • Does this answer your question? [Printing HTML tables without splitting a row across two pages](https://stackoverflow.com/questions/6586250/printing-html-tables-without-splitting-a-row-across-two-pages) – Andrew Truckle Dec 23 '19 at 22:49
  • I think you just have to use CSS styling to make the row not split over a page. Several of the answers to the duplicate question indicate this. – Andrew Truckle Dec 23 '19 at 22:50
  • @AndrewTruckle, Thanks. I've tried it (see my new edit), not working. One thing is that I have to use VS8 (quite old I admit) whose web controller may not recognize page-break options, I guess? Is there other way of fixing the issue? – user180574 Dec 23 '19 at 22:56
  • Besides, there is another issue why the bottom line has more spacing? If that can be fixed, it may solve the line splitting. – user180574 Dec 23 '19 at 23:12
  • I have another case where there are more than one column. The split happens so that the first column is on the first page and the rest columns are on the second page, so weird. – user180574 Dec 23 '19 at 23:14
  • I think what I did in the end is use a thead section at the top with a row of column headings. These get repeated when you print at top of each page. – Andrew Truckle Dec 23 '19 at 23:46
  • The spacing is managed by your Internet Explorer Page Setup. Change margins in there will change them in your browser control. – Andrew Truckle Dec 23 '19 at 23:47
  • @AndrewTruckle, Thanks. If you can, please check my recent edit. I add repeated header and footer and the error still exists. Note that, it seems to happen randomly (e.g., not from page 1 to page 2, but from page 2 to page 3). – user180574 Dec 23 '19 at 23:54
  • No matter what margin is set, it should apply consistently, right? Here, only "77" has more spacing from "78", why? – user180574 Dec 23 '19 at 23:56
  • That looks like a bug with browser control. The html content can be saved to file, and open/print with Internet Explorer, it look okays without that bug. But direct print preview shows the bug. – Barmak Shemirani Dec 24 '19 at 02:04
  • The control is bugged. It doesn’t work right with scaled text systems with ultra hd monitors. I raised it with ms and they have not resolved it. I had to add a Preview in External browser option to my editor with a list of browsers so those with issues could still print. – Andrew Truckle Dec 24 '19 at 07:14
  • I end up inserting page break periodically to avoid the issue. Thank you guys for the help! – user180574 Dec 24 '19 at 22:51
  • @AndrewTruckle Do you have a link to your question posted on Microsoft, or do you have a stackoverlow question posted here. Do you know if that issue occurs with Internet Explorer or is it just with html control? – Barmak Shemirani Dec 25 '19 at 16:58
  • @BarmakShemirani I had several issues. For example: https://stackoverflow.com/questions/49264984/thead-is-not-being-honored-when-using-print-preview-with-a-chtmlview-control https://stackoverflow.com/questions/43101923/repeating-row-headings-in-print-preview-for-table-thead-doesnt-work-correctly This last one is for the bug: https://stackoverflow.com/questions/52137889/chtmlview-that-is-compatible-with-ultrahd It links to MS issue: https://developercommunity.visualstudio.com/content/problem/215368/chtmlview-and-printing-on-ultrahd-computers.html – Andrew Truckle Dec 25 '19 at 17:05

1 Answers1

0

This appears to be a bug with IWebBrowser2 interface. It can be fixed with td{overflow-x: auto;}

<style>
@media print
{ 
    td{overflow-x: auto;} 
}
</style> 
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • I try your approach. It works except only for the first page where table footer is cut across. Please check my new edit and see if it can be fixed. Thank you for the help! – user180574 Dec 26 '19 at 16:56
  • You have changed the question, `` is not added, there seems to be other html errors. – Barmak Shemirani Dec 26 '19 at 21:24