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';
|
||||
|
||||
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 { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
|
||||
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 { extractDerivedDocumentMeta } from '../../utils/document';
|
||||
import { createDocumentAuthOptions, createRecipientAuthOptions } from '../../utils/document-auth';
|
||||
import type { EnvelopeIdOptions } from '../../utils/envelope';
|
||||
import { buildTeamWhereQuery } from '../../utils/teams';
|
||||
import { incrementDocumentId, incrementTemplateId } from '../envelope/increment-id';
|
||||
import { getTeamSettings } from '../team/get-team-settings';
|
||||
@ -421,23 +426,46 @@ export const createEnvelope = async ({
|
||||
|
||||
for (const envelopeItem of createdEnvelope.envelopeItems) {
|
||||
const buffer = await getFileServerSide(envelopeItem.documentData);
|
||||
const pdfToProcess = Buffer.from(buffer);
|
||||
|
||||
// Use normalized PDF if normalizePdf was true, otherwise use original
|
||||
const pdfToProcess = normalizePdf
|
||||
? await makeNormalizedPdf(Buffer.from(buffer))
|
||||
: Buffer.from(buffer);
|
||||
|
||||
await insertFieldsFromPlaceholdersInPDF(
|
||||
pdfToProcess,
|
||||
userId,
|
||||
teamId,
|
||||
{
|
||||
const envelopeOptions: EnvelopeIdOptions = {
|
||||
type: 'envelopeId',
|
||||
id: createdEnvelope.id,
|
||||
},
|
||||
};
|
||||
|
||||
const placeholders = await extractPlaceholdersFromPDF(pdfToProcess);
|
||||
|
||||
if (placeholders.length > 0) {
|
||||
const pdfWithoutPlaceholders = await removePlaceholdersFromPDF(pdfToProcess);
|
||||
|
||||
await insertFieldsFromPlaceholdersInPDF(
|
||||
pdfWithoutPlaceholders,
|
||||
userId,
|
||||
teamId,
|
||||
envelopeOptions,
|
||||
requestMetadata,
|
||||
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({
|
||||
|
||||
@ -51,9 +51,9 @@ type PlaceholderInfo = {
|
||||
type FieldToCreate = TFieldAndMeta & {
|
||||
envelopeItemId?: string;
|
||||
recipientId: number;
|
||||
pageNumber: number;
|
||||
pageX: number;
|
||||
pageY: number;
|
||||
page: number;
|
||||
positionX: number;
|
||||
positionY: number;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
@ -327,9 +327,9 @@ export const insertFieldsFromPlaceholdersInPDF = async (
|
||||
...placeholder.fieldAndMeta,
|
||||
envelopeItemId,
|
||||
recipientId: recipient.id,
|
||||
pageNumber: placeholder.page,
|
||||
pageX: xPercent,
|
||||
pageY: yPercent,
|
||||
page: placeholder.page,
|
||||
positionX: xPercent,
|
||||
positionY: yPercent,
|
||||
width: widthPercent,
|
||||
height: finalHeightPercent,
|
||||
});
|
||||
|
||||
@ -4,8 +4,7 @@ import type { Recipient } from '@prisma/client';
|
||||
import { match } from 'ts-pattern';
|
||||
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { createDocumentRecipients } from '@documenso/lib/server-only/recipient/create-document-recipients';
|
||||
import { createTemplateRecipients } from '@documenso/lib/server-only/recipient/create-template-recipients';
|
||||
import { createEnvelopeRecipients } from '@documenso/lib/server-only/recipient/create-envelope-recipients';
|
||||
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||
import { mapSecondaryIdToTemplateId } from '@documenso/lib/utils/envelope';
|
||||
import type { EnvelopeIdOptions } from '@documenso/lib/utils/envelope';
|
||||
@ -229,7 +228,7 @@ export const createRecipientsFromPlaceholders = async (
|
||||
id: envelope.id,
|
||||
};
|
||||
|
||||
const { recipients } = await createDocumentRecipients({
|
||||
const { recipients } = await createEnvelopeRecipients({
|
||||
userId,
|
||||
teamId,
|
||||
id: envelopeId,
|
||||
@ -242,11 +241,17 @@ export const createRecipientsFromPlaceholders = async (
|
||||
.with(EnvelopeType.TEMPLATE, async () => {
|
||||
const templateId = mapSecondaryIdToTemplateId(envelope.secondaryId ?? '');
|
||||
|
||||
const { recipients } = await createTemplateRecipients({
|
||||
const envelopeId: EnvelopeIdOptions = {
|
||||
type: 'templateId',
|
||||
id: templateId,
|
||||
};
|
||||
|
||||
const { recipients } = await createEnvelopeRecipients({
|
||||
userId,
|
||||
teamId,
|
||||
templateId,
|
||||
id: envelopeId,
|
||||
recipients: recipientsToCreateFiltered,
|
||||
requestMetadata,
|
||||
});
|
||||
|
||||
return recipients;
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import { PDFDocument } from '@cantoo/pdf-lib';
|
||||
|
||||
import { removePlaceholdersFromPDF } from './auto-place-fields';
|
||||
import { AppError } from '../../errors/app-error';
|
||||
import { flattenAnnotations } from './flatten-annotations';
|
||||
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
||||
@ -23,7 +22,8 @@ export const normalizePdf = async (pdf: Buffer) => {
|
||||
removeOptionalContentGroups(pdfDoc);
|
||||
await flattenForm(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