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(); }
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();
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">'); };
To add a padding to the page in the preview (but not the print output), use the following code:
preview.writeHead = function(doc) { writeHead.apply(this, arguments); doc.writeln('<style type="text/css">'); doc.writeln('@media screen {'); doc.writeln(' body > div { padding-top:30px;padding-left:40px;box-sizing:content-box; }'); doc.writeln('}'); doc.writeln('</style>'); };
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, x, y, content, 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'); div.firstChild.appendChild(header); return div; };
The pageNumber argument contains the number of the current page, starting at 1. To display a header on the first page only, check pageNumber and add a vertical offset in the constructor call for the height of the header.
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();
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. |
marginTop | The margin at the top of the page (number). |
marginBottom | The margin at the bottom of the page (number). |
x0 | Holds the horizontal offset of the output. |
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. |
printControls | Specifies if controls (such as folding icons) should be printed. |
printBackgroundImage | Specifies if the background image should be printed. |
backgroundColor | Holds the color value for the page background color. |
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. |
targetWindow | Assign any window here to redirect the rendering in open. |
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. |
writePostfix | Called before closing the body of the page. |
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. |
insertBackgroundImage | Inserts the background image into 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. |
Opens the print preview and shows the print dialog. | |
close | 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.
graph | mxGraph to be previewed. |
scale | Optional scale of the output. Default is 1 / mxGraph.pageScale. |
border | Border in pixels along each side of every page. Note that the actual print function in the browser will add another border for printing. |
pageFormat | mxRectangle 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. |
x0 | Optional left offset of the output. Default is 0. |
y0 | Optional top offset of the output. Default is 0. |
borderColor | Optional 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. |
title | Optional string that is used for the window title. Default is ‘Printer-friendly version’. |
pageSelector | Optional boolean that specifies if the page selector should appear in the window with the print preview. Default is true. |
mxPrintPreview.prototype.graph
Reference to the mxGraph that should be previewed.
mxPrintPreview.prototype.pageFormat
Holds the mxRectangle that defines the page format.
mxPrintPreview.prototype.targetWindow
Assign any window here to redirect the rendering in open.
mxPrintPreview.prototype.getWindow = function()
Returns wnd.
mxPrintPreview.prototype.open = function( css, targetWindow )
Shows the print preview window. The window is created here if it does not exist.
css | Optional CSS string to be used in the head section. |
targetWindow | Optional window that should be used for rendering. If this is specified then no HEAD tag, CSS and BODY tag will be written. |
mxPrintPreview.prototype.renderPage = function( w, h, dx, dy, content, 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.
w | Width of the page in pixels. |
h | Height of the page in pixels. |
dx | Optional horizontal page offset in pixels (used internally). |
dy | Optional vertical page offset in pixels (used internally). |
content | Callback that adds the HTML content to the inner div of a page. Takes the inner div as the argument. |
pageNumber | Integer representing the page number. |
mxPrintPreview.prototype.addGraphFragment = function( dx, dy, scale, pageNumber, div, clip )
Adds a graph fragment to the given div.
dx | Horizontal translation for the diagram. |
dy | Vertical translation for the diagram. |
scale | Scale for the diagram. |
pageNumber | Number of the page to be rendered. |
div | Div that contains the output. |
clip | Contains the clipping rectangle as an mxRectangle. |
Constructs a new print preview for the given parameters.
function mxPrintPreview( graph, scale, pageFormat, border, x0, y0, borderColor, title, pageSelector )
Reference to the mxGraph that should be previewed.
mxPrintPreview.prototype.graph
Holds the mxRectangle that defines the page format.
mxPrintPreview.prototype.pageFormat
Holds the scale of the print preview.
mxPrintPreview.prototype.scale
The border inset around each side of every page in the preview.
mxPrintPreview.prototype.border
The margin at the top of the page (number).
mxPrintPreview.prototype.marginTop
The margin at the bottom of the page (number).
mxPrintPreview.prototype.marginBottom
Holds the horizontal offset of the output.
mxPrintPreview.prototype.x0
Holds the vertical offset of the output.
mxPrintPreview.prototype.y0
Specifies if the origin should be automatically computed based on the top, left corner of the actual diagram contents.
mxPrintPreview.prototype.autoOrigin
Specifies if overlays should be printed.
mxPrintPreview.prototype.printOverlays
Specifies if controls (such as folding icons) should be printed.
mxPrintPreview.prototype.printControls
Specifies if the background image should be printed.
mxPrintPreview.prototype.printBackgroundImage
Holds the color value for the page background color.
mxPrintPreview.prototype.backgroundColor
Holds the color value for the page border.
mxPrintPreview.prototype.borderColor
Holds the title of the preview window.
mxPrintPreview.prototype.title
Boolean that specifies if the page selector should be displayed.
mxPrintPreview.prototype.pageSelector
Reference to the preview window.
mxPrintPreview.prototype.wnd
Assign any window here to redirect the rendering in open.
mxPrintPreview.prototype.targetWindow
Shows the print preview window.
mxPrintPreview.prototype.open = function( css, targetWindow )
Holds the actual number of pages in the preview.
mxPrintPreview.prototype.pageCount
Returns wnd.
mxPrintPreview.prototype.getWindow = function()
Writes the HEAD section into the given document, without the opening and closing HEAD tags.
mxPrintPreview.prototype.writeHead = function( doc, css )
Called before closing the body of the page.
mxPrintPreview.prototype.writePostfix = function( doc )
Creates the page selector table.
mxPrintPreview.prototype.createPageSelector = function( vpages, hpages )
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.renderPage = function( w, h, dx, dy, content, pageNumber )
Returns the root cell for painting the graph.
mxPrintPreview.prototype.getRoot = function()
Adds a graph fragment to the given div.
mxPrintPreview.prototype.addGraphFragment = function( dx, dy, scale, pageNumber, div, clip )
Inserts the background image into the given div.
mxPrintPreview.prototype.insertBackgroundImage = function( div, dx, dy )
Returns the pages to be added before the print output.
mxPrintPreview.prototype.getCoverPages = function()
Returns the pages to be added after the print output.
mxPrintPreview.prototype.getAppendices = function()
Opens the print preview and shows the print dialog.
mxPrintPreview.prototype.print = function( css )
Closes the print preview window.
mxPrintPreview.prototype.close = function()
Returns the scale to be used for printing the graph with the given bounds across the specifies number of pages with the given format.
getScaleForPageCount: function( pageCount, graph, pageFormat, border )
Specifies the scale of the background page.
mxGraph.prototype.pageScale
Specifies the page format for the background page.
mxGraph.prototype.pageFormat