Files
esign/packages/lib/server-only/pdf/insert-field-in-pdf-v2.ts
zeekayandHanzo Dev b54e5cef23 fix(lint): drop assignments nothing reads
ESLint 10's recommended set added no-useless-assignment, which fails on a
value no later statement can read. Fourteen places had one:

- An initial value every path overwrites before reading: `let x = null`
  ahead of a try that assigns it or leaves, the webhook response body both
  branches set, the rate-limit loop's counter, the avatar's previous id and
  the license response. They are declared without one; TypeScript still
  checks each is assigned before use.
- Konva stages and layers set to null after destroy(), just before the
  function returns and the variable goes out of scope. The nulls go, and
  the variables are const.
- Two in the v1 PDF field writer: textFieldBoxY recomputed after the only
  read of it had already been copied into adjustedFieldY, and a line reset
  to '' that both following branches overwrite.
- The folder breadcrumbs' currentFolderId, reassigned in a loop that walks
  by currentFolder.parentId; the first query reads folderId directly.

No output changes: each removed value was provably never read.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-19 05:04:46 -07:00

71 lines
1.9 KiB
TypeScript

// sort-imports-ignore
import '../konva/skia-backend';
import Konva from 'konva';
import path from 'node:path';
import type { Canvas } from 'skia-canvas';
import { FontLibrary } from 'skia-canvas';
import type { FieldWithSignature } from '@hanzo/esign-prisma/types/field-with-signature';
import { renderField } from '../../universal/field-renderer/render-field';
type InsertFieldInPDFV2Options = {
pageWidth: number;
pageHeight: number;
fields: FieldWithSignature[];
};
export const insertFieldInPDFV2 = async ({
pageWidth,
pageHeight,
fields,
}: InsertFieldInPDFV2Options) => {
const fontPath = path.join(process.cwd(), 'public/fonts');
// eslint-disable-next-line react-hooks/rules-of-hooks
FontLibrary.use({
['Caveat']: [path.join(fontPath, 'caveat.ttf')],
['Noto Sans']: [path.join(fontPath, 'noto-sans.ttf')],
['Noto Sans Japanese']: [path.join(fontPath, 'noto-sans-japanese.ttf')],
['Noto Sans Chinese']: [path.join(fontPath, 'noto-sans-chinese.ttf')],
['Noto Sans Korean']: [path.join(fontPath, 'noto-sans-korean.ttf')],
});
const stage = new Konva.Stage({ width: pageWidth, height: pageHeight });
const layer = new Konva.Layer();
// Render the fields onto the layer.
for (const field of fields) {
renderField({
scale: 1,
field: {
renderId: field.id.toString(),
...field,
width: Number(field.width),
height: Number(field.height),
positionX: Number(field.positionX),
positionY: Number(field.positionY),
},
translations: null,
pageLayer: layer,
pageWidth,
pageHeight,
mode: 'export',
});
}
stage.add(layer);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const canvas = layer.canvas._canvas as unknown as Canvas;
// Embed the SVG into the PDF
const pdf = await canvas.toBuffer('pdf');
stage.destroy();
layer.destroy();
return pdf;
};