|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { GanttDomService } from './gantt-dom.service'; |
| 3 | +import html2canvas from 'html2canvas'; |
| 4 | + |
| 5 | +@Injectable() |
| 6 | +export class GanttPrintService { |
| 7 | + constructor(private ganttDomService: GanttDomService) {} |
| 8 | + |
| 9 | + private root = this.ganttDomService.root as HTMLElement; |
| 10 | + |
| 11 | + private viewer = this.ganttDomService.viewer as HTMLElement; |
| 12 | + |
| 13 | + private calendarOverlay = this.ganttDomService.calendarOverlay as HTMLElement; |
| 14 | + |
| 15 | + private setInlineStyles(targetElem: Element) { |
| 16 | + const svgElements = Array.from(targetElem.getElementsByTagName('svg')); |
| 17 | + for (const svgElement of svgElements) { |
| 18 | + this.recursElementChildren(svgElement); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + private recursElementChildren(node: SVGSVGElement | HTMLElement) { |
| 23 | + const transformProperties = [ |
| 24 | + 'fill', |
| 25 | + 'color', |
| 26 | + 'font-size', |
| 27 | + 'stroke', |
| 28 | + 'font', |
| 29 | + 'text-anchor', |
| 30 | + 'stroke-dasharray', |
| 31 | + 'shape-rendering', |
| 32 | + 'stroke-width' |
| 33 | + ]; |
| 34 | + if (!node.style) { |
| 35 | + return; |
| 36 | + } |
| 37 | + const styles = getComputedStyle(node); |
| 38 | + for (const transformProperty of transformProperties) { |
| 39 | + node.style[transformProperty] = styles[transformProperty]; |
| 40 | + } |
| 41 | + for (const child of Array.from(node.childNodes)) { |
| 42 | + this.recursElementChildren(child as SVGSVGElement); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + print(name: string = 'download') { |
| 47 | + // set print width |
| 48 | + const printWidth = this.viewer.scrollWidth - this.viewer.offsetWidth + this.root.offsetWidth; |
| 49 | + |
| 50 | + // set print height |
| 51 | + const printHeight = this.viewer.scrollHeight + this.calendarOverlay.offsetHeight; |
| 52 | + |
| 53 | + html2canvas(this.root, { |
| 54 | + // scale: 2, |
| 55 | + logging: false, |
| 56 | + allowTaint: true, |
| 57 | + width: printWidth, |
| 58 | + height: printHeight, |
| 59 | + onclone: (cloneDocument: Document) => { |
| 60 | + const ganttClass = this.root.className; |
| 61 | + const cloneGanttDom = cloneDocument.querySelector(`.${ganttClass.replace(/\s+/g, '.')}`) as HTMLElement; |
| 62 | + |
| 63 | + // change targetDom width |
| 64 | + cloneGanttDom.style.width = `${printWidth}px`; |
| 65 | + cloneGanttDom.style.height = `${printHeight}px`; |
| 66 | + cloneGanttDom.style.overflow = 'unset'; |
| 67 | + cloneGanttDom.style.flex = 'none'; |
| 68 | + |
| 69 | + // setInlineStyles for svg |
| 70 | + this.setInlineStyles(cloneGanttDom); |
| 71 | + } |
| 72 | + }).then((canvas: HTMLCanvasElement) => { |
| 73 | + const link = document.createElement('a'); |
| 74 | + const dataUrl = canvas.toDataURL('image/png'); |
| 75 | + link.download = `${name}.png`; |
| 76 | + link.href = dataUrl; |
| 77 | + link.click(); |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
0 commit comments