diff client/src/lib/mixins.js @ 3993:6672b780722f

client: pdf-gen: improve addtext for pdf * calculate default width dynamically form text * avoid to render part of text outside the pdf
author Fadi Abbud <fadi.abbud@intevation.de>
date Wed, 17 Jul 2019 15:45:56 +0200
parents 2f024d6189ca
children c40f8b026de3
line wrap: on
line diff
--- a/client/src/lib/mixins.js	Wed Jul 17 15:12:23 2019 +0200
+++ b/client/src/lib/mixins.js	Wed Jul 17 15:45:56 2019 +0200
@@ -218,7 +218,6 @@
         this.pdf.doc.setFont("linbiolinum", "normal");
         let defaultFontSize = 11,
           defaultColor = "black",
-          defaultWidth = 70,
           defaultTextColor = "black",
           defaultBorderColor = "white",
           defaultBgColor = "white",
@@ -261,7 +260,7 @@
               this.addText(
                 e.position,
                 e.offset || defaultOffset,
-                e.width || defaultWidth,
+                e.width,
                 e.fontsize || defaultFontSize,
                 e.color || defaultTextColor,
                 e.text || ""
@@ -319,12 +318,21 @@
       this.pdf.doc.setFontStyle("normal");
       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 (!width) {
+        width = this.pdf.doc.getTextWidth(text);
+      }
+      var textLines = this.pdf.doc.splitTextToSize(text, width);
+      if (
+        ["topright", "topleft"].indexOf(position) !== -1 &&
+        y < this.getTextHeight(1)
+      ) {
+        y = this.getTextHeight(1);
+      }
       if (["topright", "bottomright"].indexOf(position) !== -1) {
         x = this.pdf.width - offset.x - width;
       }