printing framework - Missing the basics?

Hi all,
I have tried to understand a mystery of wxPython's printing framework
but it seems I miss the basics or something. Please you guys help me
understand the ABCs of wxPrinting framework. Mark D pointed to me
reportlab which is great but printing directly is more plausible (and
fun?)

I would like to know procedures and tricks! Something like: to cook
rice --
1. Get hot water
2. Wash the rice
3. blah blah blah

Thanks guys!

In a nutshell, you need to create a wx.Printout class that implements the bits of the printing process that you need to do. It is like the puzzle piece that fits into the printing framework to complete the whole picture. The methods of the printout object are called from the other parts of the print framework a needed to perform the various parts of the printing task. The printout is used by both the print preview and the actual print steps, and is responsible for handling the scaling for the job and the drawing of each page. Notice that I wrote "drawing" and not "printing" of each page. That is because from your perspective you will be drawing the content of the page with a DC exactly as if you were drawing it in a window on screen. See the printing examples in the demo and in the book for a couple recipes that can be followed for simple printing needs.

···

On 9/30/09 4:25 PM, evstevemd wrote:

Hi all,
I have tried to understand a mystery of wxPython's printing framework
but it seems I miss the basics or something. Please you guys help me
understand the ABCs of wxPrinting framework. Mark D pointed to me
reportlab which is great but printing directly is more plausible (and
fun?)

I would like to know procedures and tricks!

--
Robin Dunn
Software Craftsman

I had the same problem as you in trying to figure out how to get
printing to work. The only advice I can give you is to look at the wx
demo, and to look at other peoples code in how they did it. Right or
wrong, here's how I did it. (No guarantee that this is optimal, but
it does work):

In the program initialization:
      self.PrintData = wx.PrintData()
      self.PrintData.SetPaperId(wx.PAPER_LETTER) #default paper size
      self.PageData = wx.PageSetupDialogData(self.PrintData)
      self.PageData.EnableMargins(True)

There is a printing class that needs to be set up:
class xPrintout(wx.Printout):
  def __init__(self, PageList, NumPages, PageData):
    wx.Printout.__init__(self)
    self.pagelist = PageList
    self.NumPages = NumPages
    self.PageData = PageData
    return

  def HasPage(self, page):
    if (page <= self.NumPages):
      return True
    else:
      return False

  def GetPageInfo(self):
    return (1, self.NumPages, 1, self.NumPages)

  def OnPrintPage(self, page):
    dc = self.GetDC()
    (wPPI, hPPI) = self.GetPPIPrinter()
    (width, height) = dc.GetSizeTuple()
    papersize = self.PageData.GetPaperId()
    if (papersize == wx.PAPER_LETTER):
      #Set size for 8x10 printed size
      x = int(0.25*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)
    elif (papersize == wx.PAPER_LEGAL):
      #Set size for 8x13 printed size
      x = int(0.25*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)
    elif (papersize == wx.PAPER_TABLOID):
      #Set size to 10x16 printed size
      x = int(0.50*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)
    elif (papersize == wx.PAPER_CSHEET):
      #Paper size 17x22, print size 16x21
      x = int(0.50*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)
    elif (papersize == wx.PAPER_DSHEET):
      #Paper size to 22x34; print size 21x33
      x = int(0.50*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)
    elif (papersize == wx.PAPER_ESHEET):
      #Paper size to 34x44; print size 33x43
      x = int(0.50*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)
    elif (papersize == wx.PAPER_NONE):
      #Set to specific dimensions, for now default back to 8x10
printed size
      x = int(0.25*wPPI)
      y = int(0.50*hPPI)
      wide = width - (2*x)
      high = height - (2*y)
      pagerect = wx.Rect(x, y, wide, high)

    scale = float(width)/self.pagelist[page-1].GetWidth()
    pagescale = float(pagerect.width)/self.pagelist[page-1].GetWidth()
    dc.SetUserScale(pagescale, pagescale)
    dc.DrawBitmap(self.pagelist[page-1], 0, 0, True)
    dc.EndPage()
    return True

This class actually does the printing with the OnPrintPage function.
I have the following function bound to a menu item. From the users
perspective this starts the printing process:

    def OnPrintMap(self, event):
      #need to send the correct paper size to the printer
      self.DrawMapWindow(True)

      #PrintPreviewDialog is a custom print preview that I built for
my app. The important thing about this is the self.PrintBuffer, which
is a bitmap of my screen (my app is a graphics display, not a text
document). From this PrintBuffer I create a list of pages, each sized
to the right paper size.
      dlg = PrintPreviewDialog(self.frame, self.res, self.PrintData,
self.PrintBuffer, self.MapStruct.GridExtents)
      result = dlg.ShowModal()
      if (result == False):
        return

      #PrintData holds print job settings (number of pages, num
copies, etc)
      PrintData = wx.PrintDialogData(dlg.printdata)
      PrintData.SetToPage(dlg.NumPagesTotal)

      #PageData holds info relative to the paper being used;
wx.PageSetupDialogData
      self.PageData = dlg.pagedata

      #dlg.pagelist is the list of pages to be printed. It was
created in the PrintPreviewDialog.
      printout = xPrintout(dlg.pagelist, dlg.NumPagesTotal,
self.PageData)
      printer = wx.Printer(PrintData)
      printer.Print(self.frame, printout, True)
      if (printer.GetLastError() == wx.PRINTER_ERROR):
        wx.MessageBox("There was a problem printing.\nPerhaps your
current printer is not set correctly?", "Printing", wx.OK)
      elif (printer.GetLastError() == wx.PRINTER_CANCELLED):
        pass
        #wx.MessageBox("Printing Cancelled", "Printing", wx.OK)
      else: #wx.PRINTER_NO_ERROR
        self.PrintData = wx.PrintData(printer.GetPrintDialogData
().GetPrintData())
      printout.Destroy()
      dlg.Destroy()
      return

And somehow, it works. There is some kind of magic going on in the
background I think. :slight_smile:
If you want to see my code, shoot me an email and I'll forward it on
to you.

···

On Sep 30, 5:25 pm, evstevemd <mwinjili...@gmail.com> wrote:

Hi all,
I have tried to understand a mystery of wxPython's printing framework
but it seems I miss the basics or something. Please you guys help me
understand the ABCs of wxPrinting framework. Mark D pointed to me
reportlab which is great but printing directly is more plausible (and
fun?)

I would like to know procedures and tricks! Something like: to cook
rice --
1. Get hot water
2. Wash the rice
3. blah blah blah

Thanks guys!