Table Of Contents

Previous topic

pdfState

Next topic

pubsub

This Page

phoenix_title pdfViewer

View pdf reports in a scrolled window. Contents are read from PDF file and rendered in a GraphicsContext. Show visible window contents as quickly as possible then read the whole file and build the set of drawing commands for each page. This can take time for a big file or if there are complex drawings eg. ReportLab’s colour shading inside charts. Originally read in a thread but navigation is limited until whole file is ready, so now done in main thread with a progress bar, which isn’t modal so can still do whatever navigation is possible as the content availability increases.


class_hierarchy Inheritance Diagram

Inheritance diagram for class pdfViewer

Inheritance diagram of pdfViewer


super_classes Known Superclasses

ScrolledWindow


method_summary Methods Summary

__init__  
AddBitmap Add wx.Bitmap from data, processed by filters
CachePage When page takes a ‘long’ time to render, save its contents out of
CalculateDimensions Compute the required buffer sizes to hold the viewed rectangle and
ConvertCMYK Convert CMYK values (0 to 1.0) in operand to nearest RGB
DrawFile Build set of drawing commands from PDF contents. Ideally these could be drawn
DrawPath Stroke and/or fill the defined path depending on operator
DrawTextItem  
DrawTextString word spacing only works for horizontal text (??)
FetchFonts Return the standard fonts in current page or form
GoPage  
InlineImage operand contains an image
InsertXObject XObject can be an image or a ‘form’ (an arbitrary PDF sequence)
LoadFile Read pdf file using pypdf. Assume all pages are same size, for now.
OnEraseBackground Does this need to be handled?
OnIdle Redraw on resize
OnPaint Refresh visible window with bitmap contents
OnResize Buffer size change due to client area resize.
OnScroll Recalculate and redraw visible area. CallAfter is essential for coordination.
Print Print the pdf.
ProcessOperators Interpret each operation in opslist and return in drawlist
Progress This function is called at regular intervals during Drawfile
Render Recalculate dimensions as client area may have been scrolled or resized.
RenderPage Render the set of pagedrawings
RenderPageBoundaries Show non-page areas in grey
Save A pdf-only Save.
SetFont Returns wx.Font instance from supplied pdf font information
SetZoom Positive integer or floating zoom scale will render the file at corresponding

api Class API



class pdfViewer(ScrolledWindow)

View pdf reports in a scrolled window. Contents are read from PDF file and rendered in a GraphicsContext. Show visible window contents as quickly as possible then read the whole file and build the set of drawing commands for each page. This can take time for a big file or if there are complex drawings eg. ReportLab’s colour shading inside charts. Originally read in a thread but navigation is limited until whole file is ready, so now done in main thread with a progress bar, which isn’t modal so can still do whatever navigation is possible as the content availability increases.


Methods



__init__(self, parent, id, pos, size, style)


AddBitmap(self, data, width, height, filters)

Add wx.Bitmap from data, processed by filters



CachePage(self, pageno)

When page takes a ‘long’ time to render, save its contents out of self.pdc and re-use it to minimise jerky scrolling



CalculateDimensions(self, force)

Compute the required buffer sizes to hold the viewed rectangle and the range of pages visible. Override force flag and set true if the current set of rendered pages changes



ConvertCMYK(self, operand)

Convert CMYK values (0 to 1.0) in operand to nearest RGB



DrawFile(self, frompage, topage)

Build set of drawing commands from PDF contents. Ideally these could be drawn straight into a PseudoDC and the visible section painted directly into scrolled window, but we need to be able to zoom and scale the output quickly without having to rebuild the drawing commands (slow). So roll our own command lists, one per page, into self.pagedrawings.



DrawPath(self, path, action)

Stroke and/or fill the defined path depending on operator



DrawTextItem(self, textitem, f)


DrawTextString(self, text)

word spacing only works for horizontal text (??)



FetchFonts(self, currentobject)

Return the standard fonts in current page or form



GoPage(self, pagenum)


InlineImage(self, operand)

operand contains an image



InsertXObject(self, name)

XObject can be an image or a ‘form’ (an arbitrary PDF sequence)



LoadFile(self, pdf_file)

Read pdf file using pypdf. Assume all pages are same size, for now.

Parameters:pdf_file – can be either a string holding a filename path or a file-like object.


OnEraseBackground(self, event)

Does this need to be handled?



OnIdle(self, event)

Redraw on resize



OnPaint(self, event)

Refresh visible window with bitmap contents



OnResize(self, event)

Buffer size change due to client area resize.



OnScroll(self, event)

Recalculate and redraw visible area. CallAfter is essential for coordination.



Print(self, prompt=True, printer_name=None, orientation=None)

Print the pdf.

Parameters:
  • prompt (boolean) – show the print dialog to the user (True/False). If False, the print dialog will not be shown and the pdf will be printed immediately. Default: True.
  • printer_name (string) – the name of the printer that is to receive the printout. Default: as set by the O/S.
  • orientation – select the orientation (wx.PORTRAIT or wx.LANDSCAPE) for the printout. Default: as set by the O/S.


ProcessOperators(self, opslist, pdf_fonts)

Interpret each operation in opslist and return in drawlist



Progress(self, ptype, value)

This function is called at regular intervals during Drawfile



Render(self, force=True)

Recalculate dimensions as client area may have been scrolled or resized. The smallest unit of rendering that can be done is the pdf page. So render the drawing commands for the pages in the visible rectangle into a buffer big enough to hold this set of pages. For each page, use gc.Translate to render wrt the pdf origin, which is at the bottom left corner of the page. Force re-creating the page buffer only when client view moves outside it.



RenderPage(self, gc, pagedrawings)

Render the set of pagedrawings In pdf file, bitmaps are treated as being of unit width and height and are scaled appropriately in X and Y. Cairo can handle this but not wx.GraphicsContext (although it should in theory) or wx.DC,



RenderPageBoundaries(self, gc)

Show non-page areas in grey



Save(self)

A pdf-only Save.



SetFont(self, pdfont, size)

Returns wx.Font instance from supplied pdf font information



SetZoom(self, zoomscale)

Positive integer or floating zoom scale will render the file at corresponding size where 1.0 is “actual” point size (1/72”). -1 fits page width and -2 fits page height into client area Redisplay the current page(s) at the new size