Mercurial > gemma
diff client/src/lib/mixins.js @ 3941:7b3935a8d9ee
refactor addDiagram to mixin and remove code duplication
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Fri, 12 Jul 2019 11:02:46 +0200 |
parents | d859ec6cf2f0 |
children | 3c468ed76daf |
line wrap: on
line diff
--- a/client/src/lib/mixins.js Fri Jul 12 10:21:00 2019 +0200 +++ b/client/src/lib/mixins.js Fri Jul 12 11:02:46 2019 +0200 @@ -14,6 +14,7 @@ * * Bernhard Reiter <bernhard.reiter@intevation.de> */ import jsPDF from "jspdf-yworks"; +import svg2pdf from "svg2pdf.js"; import locale2 from "locale2"; import { mapState } from "vuex"; import { HTTP } from "@/lib/http"; @@ -133,6 +134,38 @@ ...mapState("user", ["user"]) }, methods: { + addDiagram(position, offset, width, height) { + let x = offset.x, + y = offset.y; + const svgWidth = this.millimeter2pixels(width, 80); + const svgHeight = this.millimeter2pixels(height, 80); + // draw the diagram in a separated html element to get the full size + const offScreen = document.querySelector("#offScreen"); + offScreen.style.width = `${svgWidth}px`; + offScreen.style.height = `${svgHeight}px`; + const layout = this.getPrintLayout(svgHeight); + this.renderTo({ + element: offScreen, + dimensions: this.getDimensions({ + svgWidth: svgWidth, + svgHeight: svgHeight, + ...layout + }) + }); + var svg = offScreen.querySelector("svg"); + if (["topright", "bottomright"].indexOf(position) !== -1) { + x = this.pdf.width - offset.x - width; + } + if (["bottomright", "bottomleft"].indexOf(position) !== -1) { + y = this.pdf.height - offset.y - height; + } + svg2pdf(svg, this.pdf.doc, { + xOffset: x, + yOffset: y, + scale: 0.354 + }); + offScreen.removeChild(svg); + }, getPaperDimensions(format) { const dims = { A3: {