mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
Compare commits
2 Commits
7fa86fe297
...
feat/auto-
| Author | SHA1 | Date | |
|---|---|---|---|
| c615e30633 | |||
| 358ba2dd6f |
Binary file not shown.
BIN
packages/assets/project-proposal-single-recipient.pdf
Normal file
BIN
packages/assets/project-proposal-single-recipient.pdf
Normal file
Binary file not shown.
@ -10,7 +10,11 @@ import {
|
|||||||
} from '@prisma/client';
|
} from '@prisma/client';
|
||||||
|
|
||||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
import { insertFieldsFromPlaceholdersInPDF } from '@documenso/lib/server-only/pdf/auto-place-fields';
|
import {
|
||||||
|
extractPlaceholdersFromPDF,
|
||||||
|
insertFieldsFromPlaceholdersInPDF,
|
||||||
|
removePlaceholdersFromPDF,
|
||||||
|
} from '@documenso/lib/server-only/pdf/auto-place-fields';
|
||||||
import { normalizePdf as makeNormalizedPdf } from '@documenso/lib/server-only/pdf/normalize-pdf';
|
import { normalizePdf as makeNormalizedPdf } from '@documenso/lib/server-only/pdf/normalize-pdf';
|
||||||
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
||||||
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||||
@ -35,6 +39,7 @@ import { getFileServerSide } from '../../universal/upload/get-file.server';
|
|||||||
import { putPdfFileServerSide } from '../../universal/upload/put-file.server';
|
import { putPdfFileServerSide } from '../../universal/upload/put-file.server';
|
||||||
import { extractDerivedDocumentMeta } from '../../utils/document';
|
import { extractDerivedDocumentMeta } from '../../utils/document';
|
||||||
import { createDocumentAuthOptions, createRecipientAuthOptions } from '../../utils/document-auth';
|
import { createDocumentAuthOptions, createRecipientAuthOptions } from '../../utils/document-auth';
|
||||||
|
import type { EnvelopeIdOptions } from '../../utils/envelope';
|
||||||
import { buildTeamWhereQuery } from '../../utils/teams';
|
import { buildTeamWhereQuery } from '../../utils/teams';
|
||||||
import { incrementDocumentId, incrementTemplateId } from '../envelope/increment-id';
|
import { incrementDocumentId, incrementTemplateId } from '../envelope/increment-id';
|
||||||
import { getTeamSettings } from '../team/get-team-settings';
|
import { getTeamSettings } from '../team/get-team-settings';
|
||||||
@ -421,23 +426,46 @@ export const createEnvelope = async ({
|
|||||||
|
|
||||||
for (const envelopeItem of createdEnvelope.envelopeItems) {
|
for (const envelopeItem of createdEnvelope.envelopeItems) {
|
||||||
const buffer = await getFileServerSide(envelopeItem.documentData);
|
const buffer = await getFileServerSide(envelopeItem.documentData);
|
||||||
|
const pdfToProcess = Buffer.from(buffer);
|
||||||
|
|
||||||
// Use normalized PDF if normalizePdf was true, otherwise use original
|
const envelopeOptions: EnvelopeIdOptions = {
|
||||||
const pdfToProcess = normalizePdf
|
|
||||||
? await makeNormalizedPdf(Buffer.from(buffer))
|
|
||||||
: Buffer.from(buffer);
|
|
||||||
|
|
||||||
await insertFieldsFromPlaceholdersInPDF(
|
|
||||||
pdfToProcess,
|
|
||||||
userId,
|
|
||||||
teamId,
|
|
||||||
{
|
|
||||||
type: 'envelopeId',
|
type: 'envelopeId',
|
||||||
id: createdEnvelope.id,
|
id: createdEnvelope.id,
|
||||||
},
|
};
|
||||||
|
|
||||||
|
const placeholders = await extractPlaceholdersFromPDF(pdfToProcess);
|
||||||
|
|
||||||
|
if (placeholders.length > 0) {
|
||||||
|
const pdfWithoutPlaceholders = await removePlaceholdersFromPDF(pdfToProcess);
|
||||||
|
|
||||||
|
await insertFieldsFromPlaceholdersInPDF(
|
||||||
|
pdfWithoutPlaceholders,
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
envelopeOptions,
|
||||||
requestMetadata,
|
requestMetadata,
|
||||||
envelopeItem.id,
|
envelopeItem.id,
|
||||||
|
createdEnvelope.recipients,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const titleToUse = envelopeItem.title || title;
|
||||||
|
const fileName = titleToUse.endsWith('.pdf') ? titleToUse : `${titleToUse}.pdf`;
|
||||||
|
|
||||||
|
const newDocumentData = await putPdfFileServerSide({
|
||||||
|
name: fileName,
|
||||||
|
type: 'application/pdf',
|
||||||
|
arrayBuffer: async () => Promise.resolve(pdfWithoutPlaceholders),
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.envelopeItem.update({
|
||||||
|
where: {
|
||||||
|
id: envelopeItem.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
documentDataId: newDocumentData.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const finalEnvelope = await prisma.envelope.findFirst({
|
const finalEnvelope = await prisma.envelope.findFirst({
|
||||||
|
|||||||
@ -51,9 +51,9 @@ type PlaceholderInfo = {
|
|||||||
type FieldToCreate = TFieldAndMeta & {
|
type FieldToCreate = TFieldAndMeta & {
|
||||||
envelopeItemId?: string;
|
envelopeItemId?: string;
|
||||||
recipientId: number;
|
recipientId: number;
|
||||||
pageNumber: number;
|
page: number;
|
||||||
pageX: number;
|
positionX: number;
|
||||||
pageY: number;
|
positionY: number;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
};
|
};
|
||||||
@ -327,9 +327,9 @@ export const insertFieldsFromPlaceholdersInPDF = async (
|
|||||||
...placeholder.fieldAndMeta,
|
...placeholder.fieldAndMeta,
|
||||||
envelopeItemId,
|
envelopeItemId,
|
||||||
recipientId: recipient.id,
|
recipientId: recipient.id,
|
||||||
pageNumber: placeholder.page,
|
page: placeholder.page,
|
||||||
pageX: xPercent,
|
positionX: xPercent,
|
||||||
pageY: yPercent,
|
positionY: yPercent,
|
||||||
width: widthPercent,
|
width: widthPercent,
|
||||||
height: finalHeightPercent,
|
height: finalHeightPercent,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -4,8 +4,7 @@ import type { Recipient } from '@prisma/client';
|
|||||||
import { match } from 'ts-pattern';
|
import { match } from 'ts-pattern';
|
||||||
|
|
||||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
import { createDocumentRecipients } from '@documenso/lib/server-only/recipient/create-document-recipients';
|
import { createEnvelopeRecipients } from '@documenso/lib/server-only/recipient/create-envelope-recipients';
|
||||||
import { createTemplateRecipients } from '@documenso/lib/server-only/recipient/create-template-recipients';
|
|
||||||
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||||
import { mapSecondaryIdToTemplateId } from '@documenso/lib/utils/envelope';
|
import { mapSecondaryIdToTemplateId } from '@documenso/lib/utils/envelope';
|
||||||
import type { EnvelopeIdOptions } from '@documenso/lib/utils/envelope';
|
import type { EnvelopeIdOptions } from '@documenso/lib/utils/envelope';
|
||||||
@ -229,7 +228,7 @@ export const createRecipientsFromPlaceholders = async (
|
|||||||
id: envelope.id,
|
id: envelope.id,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { recipients } = await createDocumentRecipients({
|
const { recipients } = await createEnvelopeRecipients({
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
id: envelopeId,
|
id: envelopeId,
|
||||||
@ -242,11 +241,17 @@ export const createRecipientsFromPlaceholders = async (
|
|||||||
.with(EnvelopeType.TEMPLATE, async () => {
|
.with(EnvelopeType.TEMPLATE, async () => {
|
||||||
const templateId = mapSecondaryIdToTemplateId(envelope.secondaryId ?? '');
|
const templateId = mapSecondaryIdToTemplateId(envelope.secondaryId ?? '');
|
||||||
|
|
||||||
const { recipients } = await createTemplateRecipients({
|
const envelopeId: EnvelopeIdOptions = {
|
||||||
|
type: 'templateId',
|
||||||
|
id: templateId,
|
||||||
|
};
|
||||||
|
|
||||||
|
const { recipients } = await createEnvelopeRecipients({
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
templateId,
|
id: envelopeId,
|
||||||
recipients: recipientsToCreateFiltered,
|
recipients: recipientsToCreateFiltered,
|
||||||
|
requestMetadata,
|
||||||
});
|
});
|
||||||
|
|
||||||
return recipients;
|
return recipients;
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { PDFDocument } from '@cantoo/pdf-lib';
|
import { PDFDocument } from '@cantoo/pdf-lib';
|
||||||
|
|
||||||
import { removePlaceholdersFromPDF } from './auto-place-fields';
|
|
||||||
import { AppError } from '../../errors/app-error';
|
import { AppError } from '../../errors/app-error';
|
||||||
import { flattenAnnotations } from './flatten-annotations';
|
import { flattenAnnotations } from './flatten-annotations';
|
||||||
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
||||||
@ -23,7 +22,8 @@ export const normalizePdf = async (pdf: Buffer) => {
|
|||||||
removeOptionalContentGroups(pdfDoc);
|
removeOptionalContentGroups(pdfDoc);
|
||||||
await flattenForm(pdfDoc);
|
await flattenForm(pdfDoc);
|
||||||
flattenAnnotations(pdfDoc);
|
flattenAnnotations(pdfDoc);
|
||||||
const pdfWithoutPlaceholders = await removePlaceholdersFromPDF(pdf);
|
|
||||||
|
|
||||||
return pdfWithoutPlaceholders;
|
const normalizedPdfBytes = await pdfDoc.save();
|
||||||
|
|
||||||
|
return Buffer.from(normalizedPdfBytes);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user