From b398c170c88a92ac2df4b04109965ab081c09e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20J=2E=20Kelling?= Date: Thu, 15 Feb 2024 11:42:42 +0100 Subject: [PATCH] PRINT add shrink approach for productCode fontSize if string is to wide --- dist/package.json | 2 +- package.json | 2 +- src/print/partials/product-code.js | 8 ++++++++ src/print/utils/mm-dpi-factor.js | 5 +++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dist/package.json b/dist/package.json index 3d43733..94dae2a 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "jspdf-product-label", - "version": "1.1.1", + "version": "1.1.2", "main": "src/app.js", "author": "André Kelling", "description": "Dymo Label PDF printing.", diff --git a/package.json b/package.json index 4ccb482..d62b783 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jspdf-template-labelprint", - "version": "1.1.1", + "version": "1.1.2", "main": "src/app.js", "author": "André Kelling", "description": "Dymo Label PDF template", diff --git a/src/print/partials/product-code.js b/src/print/partials/product-code.js index 8d2e690..53f671e 100644 --- a/src/print/partials/product-code.js +++ b/src/print/partials/product-code.js @@ -1,4 +1,5 @@ import JsPDF from 'jspdf'; +import { mmDpiFactor } from "../utils/mm-dpi-factor.js"; /** * JsPDF import just for typing! @@ -15,5 +16,12 @@ export default (doc, productCode, pageWidth) => { let startY = 55; doc.setFontSize(doc.vars.fontSizes.SmallFontSize); + + // check if the product code is too long for the page ONCE + const stringWidthInMm = doc.getStringUnitWidth(productCode) * mmDpiFactor; + if (stringWidthInMm > pageWidth) { + doc.setFontSize(doc.vars.fontSizes.SmallFontSize - 2); + } + doc.text(productCode, pageCenterX, startY, {align: 'center'}); } diff --git a/src/print/utils/mm-dpi-factor.js b/src/print/utils/mm-dpi-factor.js index ee11b10..ad8edf1 100644 --- a/src/print/utils/mm-dpi-factor.js +++ b/src/print/utils/mm-dpi-factor.js @@ -1 +1,6 @@ +/** + * Points or pixels divide by 72 to get inches or divide by (72/25.6) to get 'mm'. + * + * @type {number} + */ export const mmDpiFactor = 72 / 25.4; // dpi / mm