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
mxPrintPreview | Implements printing of a diagram across multiple pages. |
Functions | |
mxPrintPreview | Constructs a new print preview for the given parameters. |
Variables | |
graph | Reference to the mxGraph that should be previewed. |
pageFormat | Holds the mxRectangle that defines the page format. |
scale | Holds the scale of the print preview. |
border | The border inset around each side of every page in the preview. |
y0 | Holds the vertical offset of the output. |
autoOrigin | Specifies if the origin should be automatically computed based on the top, left corner of the actual diagram contents. |
printOverlays | Specifies if overlays should be printed. |
borderColor | Holds the color value for the page border. |
title | Holds the title of the preview window. |
pageSelector | Boolean that specifies if the page selector should be displayed. |
wnd | Reference to the preview window. |
pageCount | Holds the actual number of pages in the preview. |
Functions | |
getWindow | Returns wnd. |
getDocType | Returns the string that should go before the HTML tag in the print preview page. |
open | Shows the print preview window. |
writeHead | Writes the HEAD section into the given document, without the opening and closing HEAD tags. |
createPageSelector | Creates the page selector table. |
renderPage | Creates a DIV that prints a single page of the given graph using the given scale and returns the DIV that represents the page. |
getRoot | Returns the root cell for painting the graph. |
addGraphFragment | Adds a graph fragment to the given div. |
getCoverPages | Returns the pages to be added before the print output. |
getAppendices | Returns the pages to be added after the print output. |
print | Opens the print preview and shows the print dialog. |
close | Closes the print preview window. |