mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
🗃️ 💄 insert text in custom font, typeSignature
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
import { PDFDocument, rgb, StandardFonts } from "pdf-lib";
|
||||
import fontkit from "@pdf-lib/fontkit";
|
||||
import * as fs from "fs";
|
||||
|
||||
export async function insertTextInPDF(
|
||||
pdfAsBase64: string,
|
||||
@ -7,20 +9,25 @@ export async function insertTextInPDF(
|
||||
positionY: number,
|
||||
page: number = 0
|
||||
): Promise<string> {
|
||||
const fontBytes = fs.readFileSync("public/fonts/Qwigley-Regular.ttf");
|
||||
|
||||
const existingPdfBytes = pdfAsBase64;
|
||||
|
||||
const pdfDoc = await PDFDocument.load(existingPdfBytes);
|
||||
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
||||
pdfDoc.registerFontkit(fontkit);
|
||||
const customFont = await pdfDoc.embedFont(fontBytes);
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
const pdfPage = pages[page];
|
||||
const lineHeightEsimate = 25;
|
||||
const textSize = 50;
|
||||
const textWidth = customFont.widthOfTextAtSize(text, textSize);
|
||||
const textHeight = customFont.heightAtSize(textSize);
|
||||
|
||||
pdfPage.drawText(text, {
|
||||
x: pdfPage.getWidth() - positionX,
|
||||
y: pdfPage.getHeight() - positionY - lineHeightEsimate,
|
||||
size: 25,
|
||||
font: helveticaFont,
|
||||
x: pdfPage.getWidth() - positionX - textWidth / 2, // todo adjust for exact field size
|
||||
y: pdfPage.getHeight() - positionY - textHeight / 2, // todo adjust for exact field size
|
||||
size: textSize,
|
||||
font: customFont,
|
||||
color: rgb(0, 0, 0),
|
||||
});
|
||||
|
||||
|
||||
@ -121,7 +121,8 @@ model Signature {
|
||||
created DateTime @default(now())
|
||||
recipientId Int
|
||||
fieldId Int @unique
|
||||
signatureImageAsBase64 String
|
||||
signatureImageAsBase64 String?
|
||||
typedSignature String?
|
||||
|
||||
Recipient Recipient @relation(fields: [recipientId], references: [id], onDelete: Restrict)
|
||||
Field Field @relation(fields: [fieldId], references: [id], onDelete: Restrict)
|
||||
|
||||
Reference in New Issue
Block a user