mxPrintPreview

Implements printing of a diagram across multiple pages.  The following opens a print preview for an existing graph:

var preview = new mxPrintPreview(graph);
preview.open();

Use mxUtils.getScaleForPageCount as follows in order to print the graph across a given number of pages:

var pageCount = mxUtils.prompt('Enter page count', '1');

if (pageCount != null)
{
  var scale = mxUtils.getScaleForPageCount(pageCount, graph);
  var preview = new mxPrintPreview(graph, scale);
  preview.open();
}

Headers

Apart from setting the title argument in the mxPrintPreview constructor you can override renderPage as follows to add a header to any page:

var oldRenderPage = mxPrintPreview.prototype.renderPage;
mxPrintPreview.prototype.renderPage = function(w, h, dx, dy, scale, pageNumber)
{
  var div = oldRenderPage.apply(this, arguments);

  var header = document.createElement('div');
  header.style.position = 'absolute';
  header.style.top = '0px';
  header.style.width = '100%';
  header.style.textAlign = 'right';
  mxUtils.write(header, 'Your header here - Page ' + pageNumber + ' / ' + this.pageCount);
  div.firstChild.appendChild(header);

  return div;
};

Page Format

For landscape printing, use <mxConstants.PAGE_FORMAT_A4_LANDSCAPE> as the pageFormat in mxUtils.getScaleForPageCount and mxPrintPreview.  Keep in mind that one can not set the defaults for the print dialog of the operating system from JavaScript so the user must manually choose a page format that matches this setting.

You can try passing the following CSS directive to open to set the page format in the print dialog to landscape.  However, this CSS directive seems to be ignored in most major browsers, including IE.

@page {
  size: landscape;
}

Note that the print preview behaves differently in IE when used from the filesystem or via HTTP so printing should always be tested via HTTP.

If you are using a DOCTYPE in the source page you can override <getDoctype> and provide the same DOCTYPE for the print preview if required.  Here is an example for IE8 standards mode.

var preview = new mxPrintPreview(graph);
preview.getDoctype = function()
{
  return '<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=5,IE=8" ><![endif]-->';
};
preview.open();
Summary
mxPrintPreviewImplements printing of a diagram across multiple pages.
Functions
mxPrintPreviewConstructs a new print preview for the given parameters.
Variables
graphReference to the mxGraph that should be previewed.
pageFormatHolds the mxRectangle that defines the page format.
scaleHolds the scale of the print preview.
borderThe border inset around each side of every page in the preview.
y0Holds the vertical offset of the output.
autoOriginSpecifies if the origin should be automatically computed based on the top, left corner of the actual diagram contents.
printOverlaysSpecifies if overlays should be printed.
borderColorHolds the color value for the page border.
titleHolds the title of the preview window.
pageSelectorBoolean that specifies if the page selector should be displayed.
wndReference to the preview window.
pageCountHolds the actual number of pages in the preview.
Functions
getWindowReturns wnd.
getDocTypeReturns the string that should go before the HTML tag in the print preview page.
openShows the print preview window.
writeHeadWrites the HEAD section into the given document, without the opening and closing HEAD tags.
createPageSelectorCreates the page selector table.
getRootReturns the root cell for painting the graph.
renderPageCreates a DIV that prints a single page of the given graph using the given scale and returns the DIV that represents the page.
printOpens the print preview and shows the print dialog.
closeCloses the print preview window.

Functions

mxPrintPreview

function mxPrintPreview(graph,
scale,
pageFormat,
border,
x0,
y0,
borderColor,
title,
pageSelector)

Constructs a new print preview for the given parameters.

Parameters

graphmxGraph to be previewed.
scaleOptional scale of the output.  Default is 1 / mxGraph.pageScale.
borderBorder in pixels along each side of every page.  Note that the actual print function in the browser will add another border for printing.
pageFormatmxRectangle that specifies the page format (in pixels).  This should match the page format of the printer.  Default uses the mxGraph.pageFormat of the given graph.
x0Optional left offset of the output.  Default is 0.
y0Optional top offset of the output.  Default is 0.
borderColorOptional color of the page border.  Default is no border.  Note that a border is sometimes useful to highlight the printed page border in the print preview of the browser.
titleOptional string that is used for the window title.  Default is ‘Printer-friendly version’.
pageSelectorOptional boolean that specifies if the page selector should appear in the window with the print preview.  Default is true.

Variables

graph

mxPrintPreview.prototype.graph

Reference to the mxGraph that should be previewed.

pageFormat

mxPrintPreview.prototype.pageFormat

Holds the mxRectangle that defines the page format.

scale

mxPrintPreview.prototype.scale

Holds the scale of the print preview.

border

mxPrintPreview.prototype.border

The border inset around each side of every page in the preview.  This is set to 0 if autoOrigin is false.

y0

mxPrintPreview.prototype.y0

Holds the vertical offset of the output.

autoOrigin

mxPrintPreview.prototype.autoOrigin

Specifies if the origin should be automatically computed based on the top, left corner of the actual diagram contents.  If this is set to false then the values for <x0> and y0 will be overridden in open.  Default is true.

printOverlays

mxPrintPreview.prototype.printOverlays

Specifies if overlays should be printed.  Default is false.

borderColor

mxPrintPreview.prototype.borderColor

Holds the color value for the page border.

title

mxPrintPreview.prototype.title

Holds the title of the preview window.

pageSelector

mxPrintPreview.prototype.pageSelector

Boolean that specifies if the page selector should be displayed.  Default is true.

wnd

mxPrintPreview.prototype.wnd

Reference to the preview window.

pageCount

mxPrintPreview.prototype.pageCount

Holds the actual number of pages in the preview.

Functions

getWindow

mxPrintPreview.prototype.getWindow = function()

Returns wnd.

getDocType

Returns the string that should go before the HTML tag in the print preview page.  This implementation returns an empty string.

open

mxPrintPreview.prototype.open = function(css)

Shows the print preview window.  The window is created here if it does not exist.

Parameters

cssOptional CSS string to be used in the new page’s head section.

writeHead

mxPrintPreview.prototype.writeHead = function(doc,
css)

Writes the HEAD section into the given document, without the opening and closing HEAD tags.

createPageSelector

mxPrintPreview.prototype.createPageSelector = function(vpages,
hpages)

Creates the page selector table.

getRoot

mxPrintPreview.prototype.getRoot = function()

Returns the root cell for painting the graph.

renderPage

mxPrintPreview.prototype.renderPage = function(w,
h,
dx,
dy,
scale,
pageNumber)

Creates a DIV that prints a single page of the given graph using the given scale and returns the DIV that represents the page.

Parameters

wWidth of the page in pixels.
hHeight of the page in pixels.
dxHorizontal translation for the diagram.
dyVertical translation for the diagram.
scaleScale for the diagram.
pageNumberNumber of the page to be rendered.

print

mxPrintPreview.prototype.print = function()

Opens the print preview and shows the print dialog.

close

mxPrintPreview.prototype.close = function()

Closes the print preview window.

function mxPrintPreview(graph,
scale,
pageFormat,
border,
x0,
y0,
borderColor,
title,
pageSelector)
Constructs a new print preview for the given parameters.
mxPrintPreview.prototype.graph
Reference to the mxGraph that should be previewed.
Extends mxEventSource to implement a graph component for the browser.
mxPrintPreview.prototype.pageFormat
Holds the mxRectangle that defines the page format.
Extends mxPoint to implement a 2-dimensional rectangle with double precision coordinates.
mxPrintPreview.prototype.scale
Holds the scale of the print preview.
mxPrintPreview.prototype.border
The border inset around each side of every page in the preview.
mxPrintPreview.prototype.y0
Holds the vertical offset of the output.
mxPrintPreview.prototype.autoOrigin
Specifies if the origin should be automatically computed based on the top, left corner of the actual diagram contents.
mxPrintPreview.prototype.printOverlays
Specifies if overlays should be printed.
mxPrintPreview.prototype.borderColor
Holds the color value for the page border.
mxPrintPreview.prototype.title
Holds the title of the preview window.
mxPrintPreview.prototype.pageSelector
Boolean that specifies if the page selector should be displayed.
mxPrintPreview.prototype.wnd
Reference to the preview window.
mxPrintPreview.prototype.pageCount
Holds the actual number of pages in the preview.
mxPrintPreview.prototype.getWindow = function()
Returns wnd.
mxPrintPreview.prototype.open = function(css)
Shows the print preview window.
mxPrintPreview.prototype.writeHead = function(doc,
css)
Writes the HEAD section into the given document, without the opening and closing HEAD tags.
mxPrintPreview.prototype.createPageSelector = function(vpages,
hpages)
Creates the page selector table.
mxPrintPreview.prototype.getRoot = function()
Returns the root cell for painting the graph.
mxPrintPreview.prototype.renderPage = function(w,
h,
dx,
dy,
scale,
pageNumber)
Creates a DIV that prints a single page of the given graph using the given scale and returns the DIV that represents the page.
mxPrintPreview.prototype.print = function()
Opens the print preview and shows the print dialog.
mxPrintPreview.prototype.close = function()
Closes the print preview window.
getScaleForPageCount: function(pageCount,
graph,
pageFormat,
border)
Returns the scale to be used for printing the graph with the given bounds across the specifies number of pages with the given format.
mxGraph.prototype.pageScale
Specifies the scale of the background page.
mxGraph.prototype.pageFormat
Specifies the page format for the background page.
Close