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();
}

Additional pages

To add additional pages before and after the output, getCoverPages and getAppendices can be used, respectively.

var preview = new mxPrintPreview(graph, 1);

preview.getCoverPages = function(w, h)
{
  return [this.renderPage(w, h, mxUtils.bind(this, function(div)
  {
    div.innerHTML = '<div style="position:relative;margin:4px;">Cover Page</p>'
  }))];
}

preview.getAppendices = function(w, h)
{
  return [this.renderPage(w, h, mxUtils.bind(this, function(div)
  {
    div.innerHTML = '<div style="position:relative;margin:4px;">Appendix</p>'
  }))];
}

preview.open();

CSS

The CSS from the original page is not carried over to the print preview.  To add CSS to the page, use the css argument in the open function or override writeHead to add the respective link tags as follows:

var writeHead = preview.writeHead;
preview.writeHead = function(doc, css)
{
  writeHead.apply(this, arguments);
  doc.writeln('<link rel="stylesheet" type="text/css" href="style.css">');
};

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)
{
  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');
  div.firstChild.appendChild(header);

  return div;
};

For adding page numbers to the graph output pages, implement a counter in the above code or override addGraphFragment instead, where div corresponds to div.firstChild in the above code.

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.
x0Holds the horizontal offset of the output.
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.
printBackgroundImageSpecifies if the background image 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.
renderPageCreates a DIV that prints a single page of the given graph using the given scale and returns the DIV that represents the page.
getRootReturns the root cell for painting the graph.
addGraphFragmentAdds a graph fragment to the given div.
insertBackgroundImageInserts the background image into the given div.
getCoverPagesReturns the pages to be added before the print output.
getAppendicesReturns the pages to be added after the print output.
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.

x0

mxPrintPreview.prototype.x0

Holds the horizontal offset of the output.

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.

printBackgroundImage

mxPrintPreview.prototype.printBackgroundImage

Specifies if the background image 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 X-UA meta tag for IE5 in quirks mode, IE8 in IE8 standards mode and edge in IE9 standards mode.

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 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.

renderPage

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

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.
contentCallback that adds the HTML content to the inner div of a page.  Takes the inner div as the argument.

getRoot

mxPrintPreview.prototype.getRoot = function()

Returns the root cell for painting the graph.

addGraphFragment

mxPrintPreview.prototype.addGraphFragment = function(dx,
dy,
scale,
pageNumber,
div,
clip)

Adds a graph fragment to the given div.

Parameters

dxHorizontal translation for the diagram.
dyVertical translation for the diagram.
scaleScale for the diagram.
pageNumberNumber of the page to be rendered.
divDiv that contains the output.
clipContains the clipping rectangle as an mxRectangle.

insertBackgroundImage

mxPrintPreview.prototype.insertBackgroundImage = function(div,
dx,
dy)

Inserts the background image into the given div.

getCoverPages

mxPrintPreview.prototype.getCoverPages = function()

Returns the pages to be added before the print output.  This returns null.

getAppendices

mxPrintPreview.prototype.getAppendices = function()

Returns the pages to be added after the print output.  This returns null.

print

mxPrintPreview.prototype.print = function(css)

Opens the print preview and shows the print dialog.

Parameters

cssOptional CSS string to be used in the head section.

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.x0
Holds the horizontal offset of the output.
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.printBackgroundImage
Specifies if the background image 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.renderPage = function(w,
h,
dx,
dy,
content)
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.getRoot = function()
Returns the root cell for painting the graph.
mxPrintPreview.prototype.addGraphFragment = function(dx,
dy,
scale,
pageNumber,
div,
clip)
Adds a graph fragment to the given div.
mxPrintPreview.prototype.insertBackgroundImage = function(div,
dx,
dy)
Inserts the background image into the given div.
mxPrintPreview.prototype.getCoverPages = function()
Returns the pages to be added before the print output.
mxPrintPreview.prototype.getAppendices = function()
Returns the pages to be added after the print output.
mxPrintPreview.prototype.print = function(css)
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