mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
fix: normalize pdf on the server
This commit is contained in:
@ -3,6 +3,7 @@
|
|||||||
import type { z } from 'zod';
|
import type { z } from 'zod';
|
||||||
|
|
||||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
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 { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
import type { RequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
||||||
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
|
||||||
@ -13,6 +14,8 @@ import { TeamMemberRole } from '@documenso/prisma/client';
|
|||||||
import { DocumentSchema } from '@documenso/prisma/generated/zod';
|
import { DocumentSchema } from '@documenso/prisma/generated/zod';
|
||||||
|
|
||||||
import { ZWebhookDocumentSchema } from '../../types/webhook-payload';
|
import { ZWebhookDocumentSchema } from '../../types/webhook-payload';
|
||||||
|
import { getFile } from '../../universal/upload/get-file';
|
||||||
|
import { putPdfFile } from '../../universal/upload/put-file';
|
||||||
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
import { triggerWebhook } from '../webhooks/trigger/trigger-webhook';
|
||||||
|
|
||||||
export type CreateDocumentOptions = {
|
export type CreateDocumentOptions = {
|
||||||
@ -22,6 +25,7 @@ export type CreateDocumentOptions = {
|
|||||||
teamId?: number;
|
teamId?: number;
|
||||||
documentDataId: string;
|
documentDataId: string;
|
||||||
formValues?: Record<string, string | number | boolean>;
|
formValues?: Record<string, string | number | boolean>;
|
||||||
|
normalizePdf?: boolean;
|
||||||
requestMetadata?: RequestMetadata;
|
requestMetadata?: RequestMetadata;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -35,6 +39,7 @@ export const createDocument = async ({
|
|||||||
externalId,
|
externalId,
|
||||||
documentDataId,
|
documentDataId,
|
||||||
teamId,
|
teamId,
|
||||||
|
normalizePdf,
|
||||||
formValues,
|
formValues,
|
||||||
requestMetadata,
|
requestMetadata,
|
||||||
}: CreateDocumentOptions): Promise<TCreateDocumentResponse> => {
|
}: CreateDocumentOptions): Promise<TCreateDocumentResponse> => {
|
||||||
@ -104,6 +109,29 @@ export const createDocument = async ({
|
|||||||
return DocumentVisibility.EVERYONE;
|
return DocumentVisibility.EVERYONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (normalizePdf) {
|
||||||
|
const documentData = await prisma.documentData.findFirst({
|
||||||
|
where: {
|
||||||
|
id: documentDataId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (documentData) {
|
||||||
|
const buffer = await getFile(documentData);
|
||||||
|
|
||||||
|
const normalizedPdf = await makeNormalizedPdf(Buffer.from(buffer));
|
||||||
|
|
||||||
|
const newDocumentData = await putPdfFile({
|
||||||
|
name: title.endsWith('.pdf') ? title : `${title}.pdf`,
|
||||||
|
type: 'application/pdf',
|
||||||
|
arrayBuffer: async () => Promise.resolve(normalizedPdf),
|
||||||
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line require-atomic-updates
|
||||||
|
documentDataId = newDocumentData.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await prisma.$transaction(async (tx) => {
|
return await prisma.$transaction(async (tx) => {
|
||||||
const document = await tx.document.create({
|
const document = await tx.document.create({
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@ -16,7 +16,6 @@ import {
|
|||||||
export const removeOptionalContentGroups = (document: PDFDocument) => {
|
export const removeOptionalContentGroups = (document: PDFDocument) => {
|
||||||
const context = document.context;
|
const context = document.context;
|
||||||
const catalog = context.lookup(context.trailerInfo.Root);
|
const catalog = context.lookup(context.trailerInfo.Root);
|
||||||
|
|
||||||
if (catalog instanceof PDFDict) {
|
if (catalog instanceof PDFDict) {
|
||||||
catalog.delete(PDFName.of('OCProperties'));
|
catalog.delete(PDFName.of('OCProperties'));
|
||||||
}
|
}
|
||||||
|
|||||||
18
packages/lib/server-only/pdf/normalize-pdf.ts
Normal file
18
packages/lib/server-only/pdf/normalize-pdf.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { PDFDocument } from 'pdf-lib';
|
||||||
|
|
||||||
|
import { flattenAnnotations } from './flatten-annotations';
|
||||||
|
import { flattenForm, removeOptionalContentGroups } from './flatten-form';
|
||||||
|
|
||||||
|
export const normalizePdf = async (pdf: Buffer) => {
|
||||||
|
const pdfDoc = await PDFDocument.load(pdf).catch(() => null);
|
||||||
|
|
||||||
|
if (!pdfDoc) {
|
||||||
|
return pdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeOptionalContentGroups(pdfDoc);
|
||||||
|
flattenForm(pdfDoc);
|
||||||
|
flattenAnnotations(pdfDoc);
|
||||||
|
|
||||||
|
return Buffer.from(await pdfDoc.save());
|
||||||
|
};
|
||||||
@ -8,7 +8,6 @@ import { DocumentDataType } from '@documenso/prisma/client';
|
|||||||
|
|
||||||
import { AppError } from '../../errors/app-error';
|
import { AppError } from '../../errors/app-error';
|
||||||
import { createDocumentData } from '../../server-only/document-data/create-document-data';
|
import { createDocumentData } from '../../server-only/document-data/create-document-data';
|
||||||
import { removeOptionalContentGroups } from '../../server-only/pdf/flatten-form';
|
|
||||||
|
|
||||||
type File = {
|
type File = {
|
||||||
name: string;
|
name: string;
|
||||||
@ -25,7 +24,9 @@ export const putPdfFile = async (file: File) => {
|
|||||||
() => false,
|
() => false,
|
||||||
);
|
);
|
||||||
|
|
||||||
const pdf = await PDFDocument.load(await file.arrayBuffer()).catch((e) => {
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
|
|
||||||
|
const pdf = await PDFDocument.load(arrayBuffer).catch((e) => {
|
||||||
console.error(`PDF upload parse error: ${e.message}`);
|
console.error(`PDF upload parse error: ${e.message}`);
|
||||||
|
|
||||||
throw new AppError('INVALID_DOCUMENT_FILE');
|
throw new AppError('INVALID_DOCUMENT_FILE');
|
||||||
@ -39,11 +40,7 @@ export const putPdfFile = async (file: File) => {
|
|||||||
file.name = `${file.name}.pdf`;
|
file.name = `${file.name}.pdf`;
|
||||||
}
|
}
|
||||||
|
|
||||||
removeOptionalContentGroups(pdf);
|
const { type, data } = await putFile(file);
|
||||||
|
|
||||||
const bytes = await pdf.save();
|
|
||||||
|
|
||||||
const { type, data } = await putFile(new File([bytes], file.name, { type: 'application/pdf' }));
|
|
||||||
|
|
||||||
return await createDocumentData({ type, data });
|
return await createDocumentData({ type, data });
|
||||||
};
|
};
|
||||||
|
|||||||
@ -197,6 +197,7 @@ export const documentRouter = router({
|
|||||||
teamId,
|
teamId,
|
||||||
title,
|
title,
|
||||||
documentDataId,
|
documentDataId,
|
||||||
|
normalizePdf: true,
|
||||||
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user