mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
🚧 📑 pdf helpers, todos for signign
This commit is contained in:
2
packages/pdf/index.ts
Normal file
2
packages/pdf/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export { insertTextInPDF } from "./insertTextInPDF";
|
||||
export { insertImageInPDF } from "./insertImageInPDF";
|
||||
25
packages/pdf/insertImageInPDF.ts
Normal file
25
packages/pdf/insertImageInPDF.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { degrees, PDFDocument, PDFImage, rgb, StandardFonts } from "pdf-lib";
|
||||
|
||||
export async function insertImageInPDF(
|
||||
pdfAsBase64: string,
|
||||
image: string | Uint8Array | ArrayBuffer,
|
||||
positionX: number,
|
||||
positionY: number,
|
||||
page: number = 0
|
||||
): Promise<string> {
|
||||
const existingPdfBytes = pdfAsBase64;
|
||||
const pdfDoc = await PDFDocument.load(existingPdfBytes);
|
||||
const pages = pdfDoc.getPages();
|
||||
const pdfPage = pages[page];
|
||||
const pngImage = await pdfDoc.embedPng(image);
|
||||
|
||||
pdfPage.drawImage(pngImage, {
|
||||
x: positionX,
|
||||
y: positionY,
|
||||
width: pngImage.width,
|
||||
height: pngImage.height,
|
||||
});
|
||||
|
||||
const pdfAsUint8Array = await pdfDoc.save();
|
||||
return Buffer.from(pdfAsUint8Array).toString("base64");
|
||||
}
|
||||
27
packages/pdf/insertTextInPDF.ts
Normal file
27
packages/pdf/insertTextInPDF.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { degrees, PDFDocument, rgb, StandardFonts } from "pdf-lib";
|
||||
|
||||
export async function insertTextInPDF(
|
||||
pdfAsBase64: string,
|
||||
text: string,
|
||||
positionX: number,
|
||||
positionY: number,
|
||||
page: number = 0
|
||||
): Promise<string> {
|
||||
const existingPdfBytes = pdfAsBase64;
|
||||
|
||||
const pdfDoc = await PDFDocument.load(existingPdfBytes);
|
||||
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
|
||||
|
||||
const pages = pdfDoc.getPages();
|
||||
const firstPage = pages[page];
|
||||
firstPage.drawText(text, {
|
||||
x: positionX,
|
||||
y: positionY,
|
||||
size: 25,
|
||||
font: helveticaFont,
|
||||
color: rgb(0, 0, 0),
|
||||
});
|
||||
|
||||
const pdfAsUint8Array = await pdfDoc.save();
|
||||
return Buffer.from(pdfAsUint8Array).toString("base64");
|
||||
}
|
||||
7
packages/pdf/package.json
Normal file
7
packages/pdf/package.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "@documenso/pdf",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"main": "index.ts",
|
||||
"dependencies": {}
|
||||
}
|
||||
Reference in New Issue
Block a user