Printing HTML only prints the naked code, not the displayed Text [RESOLVED]


I created a dialog with an HTML window that shows exactly what I want to print. I tested two methods of how to print (after reading through a lot of stuff). Both methods just print out the plain html code but not the interpretation the HTML window is showing (self.html_page is a wx.html.HtmlWindow).

Version 1:
printout = wx.html.HtmlPrintout()
pd = wx.PrintDialog(self.frame)
printer = wx.Printer(pd.GetPrintDialogData())
printer.Print(self.frame, printout)

Version 2:
test = html.HtmlEasyPrinting(self.frame.g_char.char_name, self.frame)
# Set some default printer and page options.
# Black and white printing if False.
test.GetPageSetupData().SetMarginTopLeft((20, 20))
test.GetPageSetupData().SetMarginBottomRight((20, 20))

It seems I do not understand what HTML printing means for wxPython? I thought if a class is named to print html it would do so. What did I get wrong? How to fix this?

Thanks, Isrem.

I experimented with both versions of your code and used them to send a simple HTML page to my Brother MFCL2710DW printer. For both versions it printed out the displayed text and not the naked code.

I am using Python 3.10.6 + wxPython 4.2.0 gtk3 (phoenix) wxWidgets 3.2.0 + Linux Mint 21.

In addition, the Print dialog has a “Print to File” option. I used it to create a PDF file and that also contained the displayed text and not the naked code.

Do you also get a “Print to File” option? If so, what does the output look like?

Have you looked at what self.html_page.ToText() returns? Hint: it is not HTML text. Here is the documentation:


The HtmlWindow sample in the demo shows how to use HtmlEasyPrinting. Something like this in your Version 2 will probably work:


I tested Print to File and also opened the preview. Both have the text without tags in them, but not formatted. The print to paper has the complete html text with all tags and no formatting either.

I am using Ubuntu 20, Python 3.8.10, wxPython Phoenix 4.2.0

with GetOpenedPage() I get an empty page in the preview and with print to file.

OK, I solved the issue:

I had the html text in a variable that was not visible to my OnPrint event method. But I had the Window in a class wide visible variable. I thought there must be something like HTMLWindow.GetHtmlText() and the nearest I got to was the ToText() method. But Robin was right, that just gives me the pure text, stripped off of all html tags. Thats why the printer printed exactly that.

The solution was to make my variable for the html text visible to the whole class, and hand that to the printing call. Now I get my html print as I wanted.
The code now reads (with version 1):

    printout = html.HtmlPrintout()
    pd = wx.PrintDialog(self.frame)
    printer = wx.Printer(pd.GetPrintDialogData())
    printer.Print(self.frame, printout)