Options
All
  • Public
  • Public/Protected
  • All
Menu
example
var preview = new mxPrintPreview(graph);
preview.open();

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

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

example
var preview = new mxPrintPreview(graph, 1);

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

preview.getAppendices(w, h)
{
  return [this.renderPage(w, h, 0, 0, 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:

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

Padding

To add a padding to the page in the preview (but not the print output), use the following code:

example
preview.writeHead(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>');
};

Headers

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

example
var oldRenderPage = renderPage;
renderPage(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.

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.

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

example
var preview = new mxPrintPreview(graph);
preview.getDoctype()
{
  return '<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=5,IE=8" ><![endif]-->';
};
preview.open();

Hierarchy

  • mxPrintPreview

Index

Constructors

constructor

  • new mxPrintPreview(graph: mxGraph, scale?: number, pageFormat?: "", border?: number, x0?: number, y0?: number, borderColor?: string, title?: string, pageSelector?: boolean): mxPrintPreview
  • Parameters

    • graph: mxGraph

      mxGraph to be previewed.

    • Optional scale: number

      Optional scale of the output. Default is 1 / mxGraph.pageScale.

    • Optional pageFormat: ""

      mxRectangle that specifies the page format (in pixels).

    • Optional border: number

      Border in pixels along each side of every page. Note that the actual print function in the browser will add another border for printing. This should match the page format of the printer. Default uses the mxGraph.pageFormat of the given graph.

    • Optional x0: number

      Optional left offset of the output. Default is 0.

    • Optional y0: number

      Optional top offset of the output. Default is 0.

    • Optional borderColor: string

      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.

    • Optional title: string

      Optional string that is used for the window title. Default is 'Printer-friendly version'.

    • Optional pageSelector: boolean

      Optional boolean that specifies if the page selector should appear in the window with the print preview. Default is true.

    Returns mxPrintPreview

Properties

autoOrigin

autoOrigin: boolean

Specifies if the origin should be automatically computed based on the top, left corner of the actual diagram contents. The required offset will be added to x0 and y0 in open.

default

true

backgroundColor

backgroundColor: string

Holds the color value for the page background color.

default

'#ffffff'

border

border: number

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

default

0

borderColor

borderColor: string

Holds the color value for the page border.

clipping

clipping: boolean

Specifies is clipping should be used to avoid creating too many cell states in large diagrams. The bounding box of the cells in the original diagram is used if this is enabled.

default

true

graph

graph: mxGraph

Reference to the mxGraph that should be previewed.

marginBottom

marginBottom: number

The margin at the bottom of the page (number).

default

0

marginTop

marginTop: number

The margin at the top of the page (number).

default

0

pageCount

pageCount: number

Holds the actual number of pages in the preview.

pageFormat

pageFormat: mxRectangle

Holds the mxRectangle that defines the page format.

pageSelector

pageSelector: boolean

Boolean that specifies if the page selector should be displayed.

default

true

printBackgroundImage

printBackgroundImage: boolean

Specifies if the background image should be printed.

default

false

printControls

printControls: boolean

Specifies if controls (such as folding icons) should be printed. Default is false.

printOverlays

printOverlays: boolean

Specifies if overlays should be printed.

default

false

scale

scale: number

Holds the scale of the print preview.

targetWindow

targetWindow: Window

Assign any window here to redirect the rendering in open.

title

title: string

Holds the title of the preview window.

wnd

wnd: Window

Reference to the preview window.

x0

x0: number

Holds the horizontal offset of the output.

y0

y0: number

Holds the vertical offset of the output.

Methods

addGraphFragment

  • addGraphFragment(dx: number, dy: number, scale: number, pageNumber: number, div: HTMLDivElement, clip: mxRectangle): void
  • Adds a graph fragment to the given div.

    Parameters

    • dx: number

      Horizontal translation for the diagram.

    • dy: number

      Vertical translation for the diagram.

    • scale: number

      Scale for the diagram.

    • pageNumber: number

      Number of the page to be rendered.

    • div: HTMLDivElement

      Div that contains the output.

    • clip: mxRectangle

      Contains the clipping rectangle as an mxRectangle.

    Returns void

addPageBreak

  • addPageBreak(doc: Document): void

appendGraph

  • appendGraph(graph: mxGraph, scale: number, x0: number, y0: number, forcePageBreaks: boolean, keepOpen: boolean): void
  • Adds the given graph to the existing print preview.

    Parameters

    • graph: mxGraph
    • scale: number
    • x0: number
    • y0: number
    • forcePageBreaks: boolean
    • keepOpen: boolean

    Returns void

close

  • close(): void

closeDocument

  • closeDocument(): void

createPageSelector

  • createPageSelector(vpages: number, hpages: number): HTMLTableElement
  • Creates the page selector table.

    Parameters

    • vpages: number
    • hpages: number

    Returns HTMLTableElement

getAppendices

  • getAppendices(): any

getCoverPages

  • getCoverPages(): any

getDoctype

  • getDoctype(): string
  • 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.

    Returns string

getLinkForCellState

getRoot

getWindow

  • getWindow(): Window

insertBackgroundImage

  • insertBackgroundImage(div: HTMLDivElement, dx: number, dy: number): void
  • Inserts the background image into the given div.

    Parameters

    • div: HTMLDivElement
    • dx: number
    • dy: number

    Returns void

open

  • open(css?: string, targetWindow?: Window, forcePageBreaks?: boolean, keepOpen?: boolean): Window
  • Shows the print preview window. The window is created here if it does not exist.

    Parameters

    • Optional css: string

      Optional CSS string to be used in the head section.

    • Optional targetWindow: Window

      Optional window that should be used for rendering. If this is specified then no HEAD tag, CSS and BODY tag will be written.

    • Optional forcePageBreaks: boolean
    • Optional keepOpen: boolean

    Returns Window

print

  • print(css?: string): void
  • Opens the print preview and shows the print dialog.

    Parameters:

    Parameters

    • Optional css: string

      Optional CSS string to be used in the head section.

    Returns void

renderPage

  • renderPage(w: number, h: number, dx?: number, dy?: number, content?: Function, pageNumber?: number): HTMLDivElement
  • 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

    • w: number

      Width of the page in pixels.

    • h: number

      Height of the page in pixels.

    • Optional dx: number

      Optional horizontal page offset in pixels (used internally).

    • Optional dy: number

      Optional vertical page offset in pixels (used internally).

    • Optional content: Function

      Callback that adds the HTML content to the inner div of a page. Takes the inner div as the argument.

    • Optional pageNumber: number

      Integer representing the page number.

    Returns HTMLDivElement

writeHead

  • writeHead(doc: Document, css: string): void
  • Writes the HEAD section into the given document, without the opening and closing HEAD tags.

    Parameters

    • doc: Document
    • css: string

    Returns void

writePostfix

  • writePostfix(doc: Document): any
  • Called before closing the body of the page. This implementation is empty.

    Parameters

    • doc: Document

    Returns any

Generated using TypeDoc