0

I have a WPF application that uses the WebBrowser control in order to print html documents.
My problem is that from time to time some of the characters are missing from the generated documents.
I tried to replace the printer and cables and I also validated the html markup here.
This is how I generate the printing:

public static class HtmlPrint
{
    public static void Print(string html, string documentTitle)
    {
        var wb = new System.Windows.Forms.WebBrowser { DocumentText = html };
        wb.DocumentCompleted += wb_DocumentCompleted;
    }

    private static void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
    {
        var wb = ((System.Windows.Forms.WebBrowser)sender);

        wb.Print();
        wb.Dispose();
    }
}

HTML sample:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Items report</title>
<style type='text/css'>
    html, body {
        margin: 0;
        padding: 0;
        border: 0;
        width: 100%;
    }

    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
        margin-top: 10px;
    }

        table.gridtable th {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #dedede;
        }

        table.gridtable td {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #ffffff;
            /*white-space:nowrap*/
        }
</style>
</head>
<body dir="rtl">
<h3>15/12/2015 09:22:17</h3>
<h3>Items for station: 1</h3>
<br />
<table class="gridtable">
    <tr>
        <th>Location</th>
        <th>Item</th>
        <th>Description</th>
        <th>Color</th>
        <th>Size</th>
        <th>Qty</th>
        <th>Remarks</th>
    </tr>
    <tr>
        <td>AD393</td>
        <td>06957TO_GRE_S</td>
        <td>Green shirt</td>
        <td>GREEN-GRAY</td>
        <td>S(36)</td>
        <td>0</td>
        <td>AD-37-5<br />AD-38-3<br />AD-38-5<br />AD-39-0<br />AD-39-3<br />AD-39-5<br />BC-38-1</td>
    </tr>

    <tr>
        <td>CA183</td>
        <td>A100000000464</td>
        <td>White golf shirt</td>
        <td>WHITE</td>
        <td>XS/S (34-36)</td>
        <td>0</td>
        <td>AI-25-1<br />AK-25-1<br />AK-36-6<br />AK-37-6<br />AL-24-1<br />AL-25-1<br />CA-18-3</td>
    </tr>
</table>

Any ideas on how to fix it?
UPDATE
I've tried using the System.Windows.Controls.WebBrowser as follows but it prints empty pages:

public static void Print(string html)
    {
        /*
        html is the actual HTML data e.g. 
        <!DOCTYPE html>
            <html lang="he-IL" xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta charset="utf-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=9">
            ...
        */
        try
        {
            var wb = new System.Windows.Controls.WebBrowser();
            wb.LoadCompleted += Wb_LoadCompleted; //never called
            wb.Loaded += Wb_Loaded; //never called

            wb.NavigateToString(html);
            mshtml.IHTMLDocument2 doc = wb.Document as mshtml.IHTMLDocument2;
            doc.execCommand("Print", true, false);
        }
        catch (Exception ex)
        {
            //no exception raised
            throw;
        }
    }
Yoav
  • 3,326
  • 3
  • 32
  • 73
  • Does this HTML example not print correctly? Are the missing characters not the standard a-z, A-Z or 0-9 characters? – toadflakz Dec 17 '15 at 10:39
  • this HTML doesn't print correctly, although it's really inconsistent. missing chars are standard letters and numbers (a-z, A-Z and 0-9) – Yoav Dec 17 '15 at 11:02
  • Is there any reason why you're using the `System.Windows.Forms.WebBrowser` class instead of `System.Windows.Controls.WebBrowser`? – toadflakz Dec 17 '15 at 11:06
  • It's what [this](https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.print.aspx) article suggests. I'll have a look at the `System.Windows.Controls.WebBrowser` option... – Yoav Dec 17 '15 at 11:11
  • That article is specifically for WinForms tho'. I've found that mixing WPF and WinForms doesn't work particularly well - to print from the WPF control, check out this answer: http://stackoverflow.com/questions/1298969/printing-the-contents-of-a-wpf-webbrowser – toadflakz Dec 17 '15 at 11:17
  • @toadflakz Please see my update – Yoav Dec 17 '15 at 12:08

0 Answers0