Search code examples
pythonprintingwxpython

Print multiple Pages with wxPython


I have a wxPython text editor that I've just added a Printing function to.

I was testing it, and printed a PDF document using MS's Print To PDF.

When I opened the pdf document, Everything looked fine at first, but when i scrolled down to bottom of the page, and there wasn't a second page where there should have been one.

Here is my code:

def printwindow(self, event):
    pd = wx.PrintData()
    pd.SetPrinterName("")
    pd.SetOrientation(wx.PORTRAIT)
    pd.SetPaperId(wx.PAPER_A4)
    pd.SetQuality(wx.PRINT_QUALITY_DRAFT)
    pd.SetColour(True)
    pd.SetNoCopies(1)
    pd.SetCollate(True)

    pdd = wx.PrintDialogData()
    pdd.SetPrintData(pd)
    pdd.SetMinPage(1)
    pdd.SetMaxPage(1)
    pdd.SetFromPage(1)
    pdd.SetToPage(1)
    pdd.SetPrintToFile(False)
    pdd.EnablePageNumbers(True)
    pdd.EnableHelp(True)

    dlg = wx.PrintDialog(self, pdd)
    if dlg.ShowModal() == wx.ID_OK:
        text = self.control.GetText()
        dc = dlg.GetPrintDC()

        dc.StartDoc("MyDoc")
        dc.StartPage()
        dc.SetMapMode(wx.MM_POINTS)

        dc.SetTextForeground("black")
        dc.SetFont(self.font)
        dc.DrawText(text, 50, 100)

        dc.EndPage()
        dc.EndDoc()
        del dc
    else:
        dlg.Destroy()

Would anyone know how to print on more than one page? The document is more than one page long.


Solution

  • Found a way around this:

    class Printer(wx.html.HtmlEasyPrinting):
    
        def __init__(self):
            HtmlEasyPrinting.__init__(self)
    
        def print(self, text, docname):
            self.SetHeader(docname)
            self.PrintText(text, docname)