Printing

Printing a Diagram is typically accomplished by making several images of the Diagram and either saving them, inserting them into a PDF or other document, or printing them directly from the browser. On this page we will create several images out of a large Diagram and prepare a separate window holding those images. That window is what is actually printed.

This page uses Diagram.makeSvg, which has its own introduction page: Making Images with GoJS. Depending on your situation, you may want to use raster images to print by calling Diagram.makeImage instead.

Below is the Diagram we are going to prepare for printing, scaled down to fit on this page.

Our code for print preparation is in a printDiagram function that cuts the Diagram into several images of a given width and height and renders each such page individually. Those images are in a separate window just to make it easier to manage than combining them with the contents of this page.


  // print the diagram by opening a new window holding SVG images of the diagram contents for each page
  function printDiagram(width, height) {
    if (!width) width = 700;
    if (!height) height = 960;
    const svgWindow = window.open();
    if (!svgWindow) return; // failure to open a new Window
    svgWindow.document.title = "GoJS Printing";
    svgWindow.document.body.style.margin = "0px";
    const printSize = new go.Size(width, height);
    const bnds = myDiagram.documentBounds;
    let x = bnds.x;
    let y = bnds.y;
    while (y < bnds.bottom) {
      while (x < bnds.right) {
        const svg = myDiagram.makeSvg({
          scale: 1.0,
          position: new go.Point(x, y),
          size: printSize,
          background: myDiagram.themeManager.findValue('div', 'colors'),
        });
        svgWindow.document.body.appendChild(svg);
        x += printSize.width;
      }
      x = bnds.x;
      y += printSize.height;
    }
    setTimeout(() => { svgWindow.print(); svgWindow.close(); }, 1);
  }