mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
fix: custom text with multiline throwing an error
took me hours
This commit is contained in:
@ -107,7 +107,7 @@ export const TextField = ({ field, recipient }: TextFieldProps) => {
|
|||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: 'Error',
|
title: 'Error',
|
||||||
description: 'An error occurred while removing the signature.',
|
description: 'An error occurred while removing the text.',
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -134,7 +134,7 @@ export const TextField = ({ field, recipient }: TextFieldProps) => {
|
|||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
|
|
||||||
<div className="">
|
<div className="">
|
||||||
<Label htmlFor="signature">Custom Text</Label>
|
<Label htmlFor="custom-text">Custom Text</Label>
|
||||||
|
|
||||||
<Textarea
|
<Textarea
|
||||||
id="custom-text"
|
id="custom-text"
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
// https://github.com/Hopding/pdf-lib/issues/20#issuecomment-412852821
|
||||||
import fontkit from '@pdf-lib/fontkit';
|
import fontkit from '@pdf-lib/fontkit';
|
||||||
import { PDFDocument, StandardFonts } from 'pdf-lib';
|
import { PDFDocument, StandardFonts } from 'pdf-lib';
|
||||||
|
|
||||||
@ -73,13 +74,17 @@ export const insertFieldInPDF = async (pdf: PDFDocument, field: FieldWithSignatu
|
|||||||
height: imageHeight,
|
height: imageHeight,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let textWidth = font.widthOfTextAtSize(field.customText, fontSize);
|
const longestLineInTextForWidth = field.customText
|
||||||
|
.split('\n')
|
||||||
|
.sort((a, b) => b.length - a.length)[0];
|
||||||
|
|
||||||
|
let textWidth = font.widthOfTextAtSize(longestLineInTextForWidth, fontSize);
|
||||||
const textHeight = font.heightAtSize(fontSize);
|
const textHeight = font.heightAtSize(fontSize);
|
||||||
|
|
||||||
const scalingFactor = Math.min(fieldWidth / textWidth, fieldHeight / textHeight, 1);
|
const scalingFactor = Math.min(fieldWidth / textWidth, fieldHeight / textHeight, 1);
|
||||||
|
|
||||||
fontSize = Math.max(Math.min(fontSize * scalingFactor, maxFontSize), minFontSize);
|
fontSize = Math.max(Math.min(fontSize * scalingFactor, maxFontSize), minFontSize);
|
||||||
textWidth = font.widthOfTextAtSize(field.customText, fontSize);
|
textWidth = font.widthOfTextAtSize(longestLineInTextForWidth, fontSize);
|
||||||
|
|
||||||
const textX = fieldX + (fieldWidth - textWidth) / 2;
|
const textX = fieldX + (fieldWidth - textWidth) / 2;
|
||||||
let textY = fieldY + (fieldHeight - textHeight) / 2;
|
let textY = fieldY + (fieldHeight - textHeight) / 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user