UNPKG

gojs

Version:

Interactive diagrams, charts, and graphs, such as trees, flowcharts, orgcharts, UML, BPMN, or business diagrams

180 lines (156 loc) 7.45 kB
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Minimal GoJS Sample Generating PDF</title> <meta name="description" content="A simple demonstration of generating a PDF file in the browser, showing it in the page, and downloading it as a file." /> <!-- Copyright 1998-2023 by Northwoods Software Corporation. --> <meta charset="UTF-8"> <script src="https://unpkg.com/gojs"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script> <script src="https://unpkg.com/svg-to-pdfkit"></script> <script id="code"> // This just creates and initializes a Diagram. // The details do not really matter for this demo. var myDiagram = null; function init() { const $ = go.GraphObject.make; myDiagram = new go.Diagram("myDiagramDiv", { "grid.visible": true, "undoManager.isEnabled": true }); myDiagram.nodeTemplate = $(go.Node, "Auto", $(go.Shape, "RoundedRectangle", { strokeWidth: 0, fill: "white" }, new go.Binding("fill", "color")), $(go.TextBlock, { margin: 8 }, new go.Binding("text", "key")) ); myDiagram.model = new go.GraphLinksModel( [ { key: "Alpha", color: "lightblue" }, { key: "Beta", color: "orange" }, { key: "Gamma", color: "lightgreen" }, { key: "Delta", color: "pink" } ], [ { from: "Alpha", to: "Beta" }, { from: "Alpha", to: "Gamma" }, { from: "Beta", to: "Beta" }, { from: "Gamma", to: "Delta" }, { from: "Delta", to: "Alpha" } ]); } // This common function is called both when showing the PDF in an iframe and when downloading a PDF file. // The options include: // "pageSize", either "A4" or "LETTER" (the default) // "layout", either "portrait" (the default) or "landscape" // "margin" for the uniform page margin on each page (default is 36 pt) // "padding" instead of the Diagram.padding when adjusting the Diagram.documentBounds for the area to render // "imgWidth", size of diagram image for one page; defaults to the page width minus margins // "imgHeight", size of diagram image for one page; defaults to the page height minus margins // "parts", "background", "showTemporary", "showGrid", all are passed to Diagram.makeSvg function generatePdf(action, diagram, options) { if (!(diagram instanceof go.Diagram)) throw new Error("no Diagram provided when calling generatePdf"); if (!options) options = {}; const pageSize = (options.pageSize || "LETTER").toUpperCase(); if (pageSize !== "LETTER" && pageSize !== "A4") throw new Error("unknown page size: " + pageSize); // LETTER: 612x792 pt == 816x1056 CSS units // A4: 595.28x841.89 pt == 793.71x1122.52 CSS units let pageWidth = (pageSize === "LETTER" ? 612 : 595.28) * 96 / 72; // convert from pt to CSS units let pageHeight = (pageSize === "LETTER" ? 792 : 841.89) * 96 / 72; const layout = (options.layout || "portrait").toLowerCase(); if (layout !== "portrait" && layout !== "landscape") throw new Error("unknown layout: " + layout); if (layout === "landscape") { const temp = pageWidth; pageWidth = pageHeight; pageHeight = temp; } const margin = options.margin !== undefined ? options.margin : 36; // pt: 0.5 inch margin on each side const padding = options.padding !== undefined ? options.padding : diagram.padding; // CSS units const imgWidth = options.imgWidth !== undefined ? options.imgWidth : (pageWidth-margin/72*96*2); // CSS units const imgHeight = options.imgHeight !== undefined ? options.imgHeight : (pageHeight-margin/72*96*2); // CSS units const pageOptions = { size: pageSize, margin: margin, // unit is pt layout: layout }; require(["blob-stream", "pdfkit"], (blobStream, PDFDocument) => { const doc = new PDFDocument(pageOptions); const stream = doc.pipe(blobStream()); const bnds = diagram.documentBounds; // add some descriptive text //doc.text(diagram.nodes.count + " nodes, " + diagram.links.count + " links Diagram size: " + bnds.width.toFixed(2) + " x " + bnds.height.toFixed(2)); const db = diagram.documentBounds.copy().subtractMargin(diagram.padding).addMargin(padding); const p = db.position; // iterate over page areas of document bounds for (let j = 0; j < db.height; j += imgHeight) { for (let i = 0; i < db.width; i += imgWidth) { // if any page has no Parts partially or fully in it, skip rendering that page const r = new go.Rect(p.x + i, p.y + j, imgWidth, imgHeight); if (diagram.findPartsIn(r, true, false).count === 0) continue; if (i > 0 || j > 0) doc.addPage(pageOptions); const makeOptions = {}; if (options.parts !== undefined) makeOptions.parts = options.parts; if (options.background !== undefined) makeOptions.background = options.background; if (options.showTemporary !== undefined) makeOptions.showTemporary = options.showTemporary; if (options.showGrid !== undefined) makeOptions.showGrid = options.showGrid; makeOptions.scale = 1; makeOptions.position = new go.Point(p.x + i, p.y + j); makeOptions.size = new go.Size(imgWidth, imgHeight); const svg = diagram.makeSvg(makeOptions); SVGtoPDF(doc, svg, margin, margin, null); } } doc.end(); stream.on('finish', () => action(stream.toBlob('application/pdf'))); }); } // Two different uses of generatePdf: one shows the PDF document in the page, // the other downloads it as a file and the user specifies where to save it. const pdfOptions = // shared by both ways of generating PDF { showTemporary: true, // default is false // layout: "landscape", // instead of "portrait" // pageSize: "A4" // instead of "LETTER" }; function showPdf() { generatePdf(blob => { const datauri = window.URL.createObjectURL(blob); const frame = document.getElementById("myFrame"); if (frame) { frame.style.display = "block"; frame.src = datauri; setTimeout(() => window.URL.revokeObjectURL(datauri), 1); } }, myDiagram, pdfOptions); } function downloadPdf() { generatePdf(blob => { const datauri = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.style = "display: none"; a.href = datauri; a.download = "myDiagram.pdf"; document.body.appendChild(a); requestAnimationFrame(() => { a.click(); window.URL.revokeObjectURL(datauri); document.body.removeChild(a); }); }, myDiagram, pdfOptions); } </script> </head> <body onload="init()"> <div id="sample"> <div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:400px"></div> <div><button onclick="showPdf()">Show PDF</button> <button onclick="downloadPdf()">Download PDF</button></div> <iframe id="myFrame" style="display:none; width:1000px; height:1000px"></iframe> </div> </body> </html>