mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
This PR is handles the changes required to support envelopes. The new envelope editor/signing page will be hidden during release. The core changes here is to migrate the documents and templates model to a centralized envelopes model. Even though Documents and Templates are removed, from the user perspective they will still exist as we remap envelopes to documents and templates.
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import { PDFDocument, StandardFonts, rgb } from '@cantoo/pdf-lib';
|
|
import fontkit from '@pdf-lib/fontkit';
|
|
|
|
import { CAVEAT_FONT_PATH } from '../../constants/pdf';
|
|
|
|
export async function insertTextInPDF(
|
|
pdfAsBase64: string,
|
|
text: string,
|
|
positionX: number,
|
|
positionY: number,
|
|
page = 0,
|
|
useHandwritingFont = true,
|
|
customFontSize?: number,
|
|
): Promise<string> {
|
|
// Fetch the font file from the public URL.
|
|
const fontResponse = await fetch(CAVEAT_FONT_PATH());
|
|
const fontCaveat = await fontResponse.arrayBuffer();
|
|
|
|
const pdfDoc = await PDFDocument.load(pdfAsBase64);
|
|
|
|
pdfDoc.registerFontkit(fontkit);
|
|
|
|
const font = await pdfDoc.embedFont(useHandwritingFont ? fontCaveat : StandardFonts.Helvetica);
|
|
|
|
const pages = pdfDoc.getPages();
|
|
const pdfPage = pages[page];
|
|
|
|
const textSize = customFontSize || (useHandwritingFont ? 50 : 15);
|
|
const textWidth = font.widthOfTextAtSize(text, textSize);
|
|
const textHeight = font.heightAtSize(textSize);
|
|
const fieldSize = { width: 250, height: 64 };
|
|
|
|
// Because pdf-lib use a bottom-left coordinate system, we need to invert the y position
|
|
// we then center the text in the middle by adding half the height of the text
|
|
// plus the height of the field and divide the result by 2
|
|
const invertedYPosition =
|
|
pdfPage.getHeight() - positionY - (fieldSize.height + textHeight / 2) / 2;
|
|
|
|
// We center the text by adding the width of the field, subtracting the width of the text
|
|
// and dividing the result by 2
|
|
const centeredXPosition = positionX + (fieldSize.width - textWidth) / 2;
|
|
|
|
pdfPage.drawText(text, {
|
|
x: centeredXPosition,
|
|
y: invertedYPosition,
|
|
size: textSize,
|
|
color: rgb(0, 0, 0),
|
|
font,
|
|
});
|
|
|
|
const pdfAsUint8Array = await pdfDoc.save();
|
|
|
|
return Buffer.from(pdfAsUint8Array).toString('base64');
|
|
}
|