Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: update watermark management

Marc Seitz8b938c8a2bb5
diff --git a/pages/api/mupdf/annotate-document.ts b/pages/api/mupdf/annotate-document.ts
index 859cc533..8a96acac 100644
--- a/pages/api/mupdf/annotate-document.ts
+++ b/pages/api/mupdf/annotate-document.ts
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import fontkit from "@pdf-lib/fontkit";
-import { PDFDocument, StandardFonts, degrees, rgb } from "pdf-lib";
+import { PDFDocument, PDFFont, StandardFonts, degrees, rgb } from "pdf-lib";
import {
getFileNameWithPdfExtension,
@@ -10,9 +10,9 @@ import {
safeTemplateReplace,
} from "@/lib/utils";
-// This function can run for a maximum of 120 seconds
+// This function can run for a maximum of 300 seconds
export const config = {
- maxDuration: 180,
+ maxDuration: 300,
};
interface WatermarkConfig {
@@ -68,13 +68,12 @@ async function insertWatermark(
config: WatermarkConfig,
viewerData: ViewerData,
pageIndex: number,
+ font: PDFFont, // Pre-embedded font passed in
): Promise<void> {
const pages = pdfDoc.getPages();
const page = pages[pageIndex];
const { width, height } = page.getSize();
- const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
-
// Safely replace template variables with whitelisted values only
const rawWatermarkText = safeTemplateReplace(config.text, viewerData);
@@ -266,36 +265,142 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
originalFileName?: string;
};
+ // Validate required fields
+ if (!url || typeof url !== "string") {
+ return res.status(400).json({ error: "Invalid or missing URL" });
+ }
+
+ if (!watermarkConfig || typeof watermarkConfig !== "object") {
+ return res
+ .status(400)
+ .json({ error: "Invalid or missing watermark config" });
+ }
+
+ if (!numPages || typeof numPages !== "number" || numPages <= 0) {
+ return res.status(400).json({ error: "Invalid page count" });
+ }
+
+ if (numPages > 1000) {
+ return res.status(400).json({
+ error: "Document too large",
+ details: "Maximum 1000 pages supported",
+ });
+ }
+
+ const startTime = Date.now();
+
try {
- // Fetch the PDF data
+ // Fetch the PDF data with timeout
let response: Response;
try {
- response = await fetch(url);
+ const fetchStart = Date.now();
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), 60000); // 60 second timeout for fetch
+
+ response = await fetch(url, {
+ signal: controller.signal,
+ headers: {
+ Accept: "application/pdf",
+ },
+ });
+
+ clearTimeout(timeoutId);
+
+ if (!response.ok) {
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
+ }
+
+ console.log(`PDF fetch took ${Date.now() - fetchStart}ms`);
} catch (error) {
+ const errorMsg = error instanceof Error ? error.message : String(error);
log({
- message: `Failed to fetch PDF in conversion process with error: \n\n Error: ${error}`,
+ message: `Failed to fetch PDF in conversion process with error: \n\n Error: ${errorMsg}\nURL: ${url}`,
type: "error",
mention: true,
});
- throw new Error(`Failed to fetch pdf`);
+
+ if (errorMsg.includes("aborted")) {
+ throw new Error(`Timeout fetching PDF (exceeded 60s)`);
+ }
+ throw new Error(`Failed to fetch PDF: ${errorMsg}`);
}
// Convert the response to a buffer
+ const bufferStart = Date.now();
const pdfBuffer = await response.arrayBuffer();
+ const sizeInMB = pdfBuffer.byteLength / 1024 / 1024;
+ console.log(
+ `Buffer conversion took ${Date.now() - bufferStart}ms, size: ${sizeInMB.toFixed(2)}MB`,
+ );
- // Load the PDF document
- const pdfDoc = await PDFDocument.load(pdfBuffer);
+ // Load the PDF document with optimizations
+ const loadStart = Date.now();
+ const pdfDoc = await PDFDocument.load(pdfBuffer, {
+ updateMetadata: false, // Skip metadata updates for performance
+ ignoreEncryption: false, // Respect encryption
+ });
+ console.log(`PDF load took ${Date.now() - loadStart}ms`);
- // Register fontkit
+ // Register fontkit and embed font ONCE
pdfDoc.registerFontkit(fontkit);
+ const fontStart = Date.now();
+ const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
+ console.log(`Font embedding took ${Date.now() - fontStart}ms`);
+
+ // Calculate optimal batch size based on document size and page count
+ // Larger documents = smaller batches to prevent memory spikes
+ const calculateBatchSize = (pages: number, sizeMB: number): number => {
+ if (sizeMB > 50 || pages > 200) return 5; // Large docs: 5 pages at a time
+ if (sizeMB > 20 || pages > 100) return 10; // Medium docs: 10 pages
+ return 20; // Smaller docs: 20 pages at a time
+ };
+
+ const BATCH_SIZE = calculateBatchSize(numPages, sizeInMB);
+ const watermarkStart = Date.now();
+
+ console.log(`Processing ${numPages} pages in batches of ${BATCH_SIZE}`);
+
+ for (let batchStart = 0; batchStart < numPages; batchStart += BATCH_SIZE) {
+ const batchEnd = Math.min(batchStart + BATCH_SIZE, numPages);
+ const batchPromises = [];
+
+ for (let i = batchStart; i < batchEnd; i++) {
+ batchPromises.push(
+ insertWatermark(pdfDoc, watermarkConfig, viewerData, i, font).catch(
+ (error) => {
+ const errMsg =
+ error instanceof Error ? error.message : String(error);
+ console.error(`Error watermarking page ${i + 1}:`, errMsg);
+ throw new Error(`Failed to watermark page ${i + 1}: ${errMsg}`);
+ },
+ ),
+ );
+ }
- // Add watermark to each page
- for (let i = 0; i < numPages; i++) {
- await insertWatermark(pdfDoc, watermarkConfig, viewerData, i);
+ await Promise.all(batchPromises);
+ const progress = ((batchEnd / numPages) * 100).toFixed(1);
+ console.log(
+ `Processed pages ${batchStart + 1}-${batchEnd} of ${numPages} (${progress}%)`,
+ );
}
- // Save the modified PDF
- const pdfBytes = await pdfDoc.save();
+ console.log(`All watermarking took ${Date.now() - watermarkStart}ms`);
+
+ // Save the modified PDF with optimizations
+ const saveStart = Date.now();
+ const pdfBytes = await pdfDoc.save({
+ useObjectStreams: false, // Better compatibility, slight size increase
+ addDefaultPage: false, // Don't add default page
+ objectsPerTick: 50, // Process objects in smaller chunks for better memory management
+ });
+ const finalSizeMB = pdfBytes.length / 1024 / 1024;
+ console.log(
+ `PDF save took ${Date.now() - saveStart}ms, final size: ${finalSizeMB.toFixed(2)}MB`,
+ );
+
+ console.log(
+ `Total processing time: ${Date.now() - startTime}ms for ${numPages} pages`,
+ );
// Set appropriate headers
res.setHeader("Content-Type", "application/pdf");
@@ -308,16 +413,44 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
return;
} catch (error) {
+ const errorMessage = error instanceof Error ? error.message : String(error);
+ const elapsedTime = Date.now() - startTime;
+
+ // Determine appropriate status code based on error type
+ let statusCode = 500;
+ let errorType = "Failed to apply watermark";
+
+ if (errorMessage.includes("Timeout") || errorMessage.includes("timeout")) {
+ statusCode = 504;
+ errorType = "Request timeout";
+ } else if (
+ errorMessage.includes("too large") ||
+ errorMessage.includes("Maximum")
+ ) {
+ statusCode = 413;
+ errorType = "Document too large";
+ } else if (
+ errorMessage.includes("fetch") ||
+ errorMessage.includes("HTTP")
+ ) {
+ statusCode = 502;
+ errorType = "Failed to fetch document";
+ } else if (errorMessage.includes("Failed to watermark page")) {
+ statusCode = 500;
+ errorType = "Watermarking error";
+ }
+
log({
- message: `Failed to convert page with error: \n\n Error: ${error}`,
+ message: `${errorType} after ${elapsedTime}ms: ${errorMessage}\n\nDocument: ${originalFileName || "unknown"}\nPages: ${numPages}\nURL: ${url?.substring(0, 100)}...`,
type: "error",
- mention: true,
+ mention: elapsedTime > 120000, // Only mention if it took more than 2 minutes
});
- // Return proper error response instead of throwing
- res.status(500).json({
- error: "Failed to apply watermark",
- details: (error as Error).message,
+ // Return proper error response
+ res.status(statusCode).json({
+ error: errorType,
+ details: errorMessage,
+ processingTime: elapsedTime,
});
return;
}