How to Copy an OrgChart JS Chart Image to Clipboard
Have you ever wanted to quickly copy your organization chart and paste it into another app β like Slack, Word, or an email β as an image?
With the OrgChart JS library and the modern Clipboard API, you can easily do that!
In this tutorial, weβll show how to export your chart as an image and copy it directly to the clipboard β without downloading any files.
Full Code Example
π‘ Overview
Weβll use:
- OrgChart JS to generate the chart.
exportToSVG()
to export the chart as an SVG image.
- Canvas and Blob to convert SVG β PNG.
navigator.clipboard.write()
to copy the PNG into the clipboard.
Once done, you can press Ctrl + V to paste the image anywhere that supports pasting images (e.g., Word, Gmail, or Paint).
π§© Full Example Code
Hereβs the full working example β you can test it by pasting the code into an HTML file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Copy OrgChart Image to Clipboard</title>
<script src="https://balkan.app/js/OrgChart.js"></script>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: Helvetica;
overflow: hidden;
}
#tree {
width: 100%;
height: 100%;
}
button {
position: absolute;
top: 30px;
right: 30px;
font-size: 24px;
z-index: 11111;
}
</style>
</head>
<body>
<button id="button">Copy</button>
<div id="tree"></div>
<script>
let chart = new OrgChart(document.getElementById("tree"), {
mouseScrool: OrgChart.action.none,
nodeBinding: {
field_0: "name"
},
});
chart.onExportStart(function (args) {
// For demo: open the example in a new tab if inside an iframe
if (window.parent != window) {
if (confirm("In order to test the Code you have to open it in a new tab. Would you like to open it in a new tab?")) {
window.open("https://code.balkan.app/result/orgchart-js/copy-the-chart-to-clipboard");
}
return false;
}
// Get SVG and convert it to PNG
const svg = args.pages[0];
const svgData = new XMLSerializer().serializeToString(svg);
const svgBlob = new Blob([svgData], { type: "image/svg+xml" });
const url = URL.createObjectURL(svgBlob);
const img = new Image();
img.onload = () => {
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Convert canvas to PNG and copy it
canvas.toBlob(async (blob) => {
try {
await navigator.clipboard.write([
new ClipboardItem({ "image/png": blob })
]);
console.log("β
PNG copied to clipboard!");
} catch (err) {
console.error("β Copy failed", err);
}
}, "image/png");
};
img.src = url;
return false; // prevent download dialog
});
// Load simple chart data
chart.load([
{ id: 1, name: "Amber McKenzie" },
{ id: 2, pid: 1, name: "Ava Field" },
{ id: 3, pid: 1, name: "Peter Stevens" }
]);
// Copy button triggers export
document.getElementById("button").onclick = () => {
chart.exportToSVG();
};
</script>
</body>
</html>
βοΈ How It Works
chart.exportToSVG()
Exports the chart as an SVG image.
Convert SVG β Canvas β PNG
The SVG is drawn into a <canvas>
element so we can generate a PNG blob.
Copy to Clipboard
Using:
await navigator.clipboard.write([
new ClipboardItem({ "image/png": blob })
]);
This copies the PNG image to the system clipboard.
Paste Anywhere!
Open Word, Paint, or Gmail and press Ctrl + V β your chart appears as an image.
π§ Notes
- This works in modern browsers (Chrome, Edge, etc.) that support the asynchronous Clipboard API.
- Some browsers might require a secure context (
https://
) to allow clipboard image access.
- You can replace
"image/png"
with "image/jpeg"
or "image/webp"
if you prefer another format.
π Try It Yourself
You can test this example Here or your own HTML file.
Once it runs, click Copy, and paste your chart into any app that accepts images!
β
Result:
After clicking Copy, just hit Ctrl + V β your org chart image appears instantly!
Here is a more simple example on How to copy the JSON data.