mirror of
https://github.com/documenso/documenso.git
synced 2026-08-15 02:53:32 +10:00
Use our fork of `skia-canvas` for rendering which handles encoding characters correctly with the caveat font and other similar fonts that can group glyphs like ligatures. This resolves issues with pdf text extraction where characters were unable to be extracted due to lacking any data within the cmaps.
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
// sort-imports-ignore
|
|
import '../konva/skia-backend';
|
|
|
|
import type { FieldWithSignature } from '@documenso/prisma/types/field-with-signature';
|
|
import type { Canvas } from '@documenso/skia-canvas';
|
|
import Konva from 'konva';
|
|
|
|
import { renderField } from '../../universal/field-renderer/render-field';
|
|
import { ensureFontLibrary } from './helpers';
|
|
|
|
type InsertFieldInPDFV2Options = {
|
|
pageWidth: number;
|
|
pageHeight: number;
|
|
fields: FieldWithSignature[];
|
|
};
|
|
|
|
export const insertFieldInPDFV2 = async ({ pageWidth, pageHeight, fields }: InsertFieldInPDFV2Options) => {
|
|
ensureFontLibrary();
|
|
|
|
let stage: Konva.Stage | null = new Konva.Stage({ width: pageWidth, height: pageHeight });
|
|
let layer: Konva.Layer | null = 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();
|
|
|
|
stage = null;
|
|
layer = null;
|
|
|
|
return pdf;
|
|
};
|