Skip to content

Commit

Permalink
PRINT add shrink approach for productCode fontSize if string is to wide
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKelling committed Feb 15, 2024
1 parent 08b61bc commit b398c17
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/print/partials/product-code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import JsPDF from 'jspdf';
import { mmDpiFactor } from "../utils/mm-dpi-factor.js";

/**
* JsPDF import just for typing!
Expand All @@ -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'});
}
5 changes: 5 additions & 0 deletions src/print/utils/mm-dpi-factor.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b398c17

Please sign in to comment.