mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
♻️ 🚧 date and text field
This commit is contained in:
@ -46,6 +46,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
positionX: number;
|
positionX: number;
|
||||||
positionY: number;
|
positionY: number;
|
||||||
Recipient: { id: number };
|
Recipient: { id: number };
|
||||||
|
customText: string;
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
if (!user) return;
|
if (!user) return;
|
||||||
@ -69,6 +70,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
update: {
|
update: {
|
||||||
positionX: +body.positionX,
|
positionX: +body.positionX,
|
||||||
positionY: +body.positionY,
|
positionY: +body.positionY,
|
||||||
|
customText: body.customText,
|
||||||
},
|
},
|
||||||
create: {
|
create: {
|
||||||
documentId: +documentId,
|
documentId: +documentId,
|
||||||
@ -76,6 +78,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
page: +body.page,
|
page: +body.page,
|
||||||
positionX: +body.positionX,
|
positionX: +body.positionX,
|
||||||
positionY: +body.positionY,
|
positionY: +body.positionY,
|
||||||
|
customText: body.customText,
|
||||||
// todo refactor only one type of recipientId
|
// todo refactor only one type of recipientId
|
||||||
recipientId: body.Recipient.id,
|
recipientId: body.Recipient.id,
|
||||||
},
|
},
|
||||||
|
|||||||
@ -71,20 +71,24 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const dateFields = await prisma.field.findMany({
|
const nonSignatureFields = await prisma.field.findMany({
|
||||||
where: {
|
where: {
|
||||||
documentId: document.id,
|
documentId: document.id,
|
||||||
type: FieldType.DATE,
|
type: { in: [FieldType.DATE, FieldType.TEXT] },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const dateField of dateFields) {
|
// Insert fields other than signatures
|
||||||
|
for (const field of nonSignatureFields) {
|
||||||
documentWithInserts = await insertTextInPDF(
|
documentWithInserts = await insertTextInPDF(
|
||||||
documentWithInserts,
|
documentWithInserts,
|
||||||
new Date().toDateString(),
|
field.type === FieldType.DATE
|
||||||
dateField.positionX,
|
? new Date().toDateString()
|
||||||
dateField.positionY,
|
: field.customText || "",
|
||||||
dateField.page
|
field.positionX,
|
||||||
|
field.positionY,
|
||||||
|
field.page,
|
||||||
|
false
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
import { FieldType } from "@prisma/client";
|
import { FieldType } from "@prisma/client";
|
||||||
|
|
||||||
export const createField = function addField(
|
export const createField = (
|
||||||
e: any,
|
e: any,
|
||||||
page: number,
|
page: number,
|
||||||
selectedRecipient: any,
|
selectedRecipient: any,
|
||||||
type: FieldType = FieldType.SIGNATURE
|
type: FieldType = FieldType.SIGNATURE,
|
||||||
): any {
|
customText = ""
|
||||||
|
): any => {
|
||||||
var rect = e.target.getBoundingClientRect();
|
var rect = e.target.getBoundingClientRect();
|
||||||
const fieldSize = { width: 192, height: 96 };
|
const fieldSize = { width: 192, height: 96 };
|
||||||
var newFieldX = e.clientX - rect.left - fieldSize.width / 2; //x position within the element.
|
var newFieldX = e.clientX - rect.left - fieldSize.width / 2; //x position within the element.
|
||||||
@ -25,6 +26,7 @@ export const createField = function addField(
|
|||||||
positionX: newFieldX.toFixed(0),
|
positionX: newFieldX.toFixed(0),
|
||||||
positionY: newFieldY.toFixed(0),
|
positionY: newFieldY.toFixed(0),
|
||||||
Recipient: selectedRecipient,
|
Recipient: selectedRecipient,
|
||||||
|
customText: customText,
|
||||||
};
|
};
|
||||||
|
|
||||||
return signatureField;
|
return signatureField;
|
||||||
|
|||||||
@ -7,7 +7,8 @@ export async function insertTextInPDF(
|
|||||||
text: string,
|
text: string,
|
||||||
positionX: number,
|
positionX: number,
|
||||||
positionY: number,
|
positionY: number,
|
||||||
page: number = 0
|
page: number = 0,
|
||||||
|
useHandwritingFont = true
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const fontBytes = fs.readFileSync("public/fonts/Qwigley-Regular.ttf");
|
const fontBytes = fs.readFileSync("public/fonts/Qwigley-Regular.ttf");
|
||||||
|
|
||||||
@ -16,10 +17,11 @@ export async function insertTextInPDF(
|
|||||||
const pdfDoc = await PDFDocument.load(existingPdfBytes);
|
const pdfDoc = await PDFDocument.load(existingPdfBytes);
|
||||||
pdfDoc.registerFontkit(fontkit);
|
pdfDoc.registerFontkit(fontkit);
|
||||||
const customFont = await pdfDoc.embedFont(fontBytes);
|
const customFont = await pdfDoc.embedFont(fontBytes);
|
||||||
|
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
||||||
|
|
||||||
const pages = pdfDoc.getPages();
|
const pages = pdfDoc.getPages();
|
||||||
const pdfPage = pages[page];
|
const pdfPage = pages[page];
|
||||||
const textSize = 50;
|
const textSize = useHandwritingFont ? 50 : 15;
|
||||||
const textWidth = customFont.widthOfTextAtSize(text, textSize);
|
const textWidth = customFont.widthOfTextAtSize(text, textSize);
|
||||||
const textHeight = customFont.heightAtSize(textSize);
|
const textHeight = customFont.heightAtSize(textSize);
|
||||||
|
|
||||||
@ -27,7 +29,7 @@ export async function insertTextInPDF(
|
|||||||
x: positionX, // todo adjust for exact field size
|
x: positionX, // todo adjust for exact field size
|
||||||
y: pdfPage.getHeight() - positionY - textHeight / 2, // todo adjust for exact field size
|
y: pdfPage.getHeight() - positionY - textHeight / 2, // todo adjust for exact field size
|
||||||
size: textSize,
|
size: textSize,
|
||||||
font: customFont,
|
font: useHandwritingFont ? customFont : helveticaFont,
|
||||||
color: rgb(0, 0, 0),
|
color: rgb(0, 0, 0),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -115,6 +115,7 @@ model Field {
|
|||||||
page Int
|
page Int
|
||||||
positionX Int @default(0)
|
positionX Int @default(0)
|
||||||
positionY Int @default(0)
|
positionY Int @default(0)
|
||||||
|
customText String
|
||||||
Document Document @relation(fields: [documentId], references: [id], onDelete: Cascade)
|
Document Document @relation(fields: [documentId], references: [id], onDelete: Cascade)
|
||||||
Recipient Recipient? @relation(fields: [recipientId], references: [id], onDelete: Cascade)
|
Recipient Recipient? @relation(fields: [recipientId], references: [id], onDelete: Cascade)
|
||||||
Signature Signature?
|
Signature Signature?
|
||||||
|
|||||||
Reference in New Issue
Block a user