diff client/src/lib/mixins.js @ 3137:35c0da49eb89

client: pdf-gen: prepare pdf template for waterlevel diagram * use default template when nothings come from backend * adjust and add some functions to handle the generation of template elements
author Fadi Abbud <fadi.abbud@intevation.de>
date Thu, 02 May 2019 10:46:50 +0200
parents 57255fda7594
children 42324626f9e2
line wrap: on
line diff
--- a/client/src/lib/mixins.js	Tue Apr 30 15:33:05 2019 +0200
+++ b/client/src/lib/mixins.js	Thu May 02 10:46:50 2019 +0200
@@ -11,6 +11,7 @@
  * Author(s):
  * Markus Kottländer <markus.kottlaender@intevation.de>
  */
+import locale2 from "locale2";
 const sortTable = {
   data() {
     return {
@@ -54,4 +55,73 @@
   }
 };
 
-export { sortTable, pane };
+const pdfgen = {
+  methods: {
+    addText(position, offset, width, fontSize, color, text) {
+      text = this.replacePlaceholders(text);
+      // split the incoming string to an array, each element is a string of
+      // words in a single line
+      this.pdf.doc.setTextColor(color);
+      this.pdf.doc.setFontSize(fontSize);
+      var textLines = this.pdf.doc.splitTextToSize(text, width);
+      // x/y defaults to offset for topleft corner (normal x/y coordinates)
+      let x = offset.x;
+      let y = offset.y;
+      // if position is on the right, x needs to be calculate with pdf width and
+      // the size of the element
+      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 - this.getTextHeight(textLines.length);
+      }
+
+      this.pdf.doc.text(textLines, x, y, { baseline: "hanging" });
+    },
+    replacePlaceholders(text) {
+      if (text.includes("{date}")) {
+        text = text.replace("{date}", new Date().toLocaleString(locale2));
+      }
+      //get only day,month and year from the Date object
+      if (text.includes("{date-minor}")) {
+        var date = new Date();
+        var dt =
+          (date.getDate() < 10 ? "0" : "") +
+          date.getDate() +
+          "." +
+          (date.getMonth() + 1 < 10 ? "0" : "") +
+          (date.getMonth() + 1) +
+          "." +
+          date.getFullYear();
+        text = text.replace("{date-minor}", dt.toLocaleString(locale2));
+      }
+      if (text.includes("{user}")) {
+        text = text.replace("{user}", this.user);
+      }
+      return text;
+    },
+    addImage(url, format, position, offset, width, height) {
+      let x = offset.x;
+      let y = offset.y;
+      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;
+      }
+      let image = new Image();
+      if (url) {
+        image.src = url;
+      } else {
+        if (this.logoForPDF) {
+          image.src = this.logoForPDF;
+        } else {
+          image.src = "/img/gemma-logo-for-pdf.png";
+        }
+      }
+      this.pdf.doc.addImage(image, x, y, width, height);
+    }
+  }
+};
+
+export { sortTable, pane, pdfgen };