Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: conversion scaling for long documents
Marc Seitz99913c0735d6
diff --git a/pages/api/mupdf/convert-page.ts b/pages/api/mupdf/convert-page.tsindex 3c908d86..a821e385 100644--- a/pages/api/mupdf/convert-page.ts+++ b/pages/api/mupdf/convert-page.ts@@ -63,6 +63,18 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {const widthInPoints = Math.abs(lrx - ulx);const heightInPoints = Math.abs(lry - uly);+ // Validate document dimensions+ if (widthInPoints <= 0 || heightInPoints <= 0) {+ throw new Error(+ `Invalid page dimensions: ${widthInPoints} × ${heightInPoints} points`,+ );+ }++ // Log original dimensions for debugging+ console.log(+ `Original page dimensions: ${widthInPoints} × ${heightInPoints} points (${(widthInPoints / 72).toFixed(1)}" × ${(heightInPoints / 72).toFixed(1)}")`,+ );+if (pageNumber === 1) {// get the orientation of the document and update document versionconst isVertical = heightInPoints > widthInPoints;@@ -73,11 +85,56 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {});}- // Scale the document to 144 DPI- const scaleFactor = widthInPoints >= 1600 ? 2 : 3; // 2x for width >= 1600, 3x for width < 1600+ // Calculate optimal scale factor based on document dimensions and memory constraints+ const getOptimalScaleFactor = (width: number, height: number): number => {+ // Maximum reasonable pixel dimensions to prevent memory issues+ const MAX_PIXEL_DIMENSION = 8000;+ const MAX_TOTAL_PIXELS = 32_000_000; // ~32MP to stay within memory limits++ // Start with default scaling logic+ let scaleFactor = width >= 1600 ? 2 : 3;++ // Check if scaled dimensions would exceed limits+ const scaledWidth = width * scaleFactor;+ const scaledHeight = height * scaleFactor;+ const totalPixels = scaledWidth * scaledHeight;++ // Reduce scale factor if dimensions are too large+ if (+ scaledWidth > MAX_PIXEL_DIMENSION ||+ scaledHeight > MAX_PIXEL_DIMENSION ||+ totalPixels > MAX_TOTAL_PIXELS+ ) {+ // Calculate maximum safe scale factor+ const maxScaleByWidth = MAX_PIXEL_DIMENSION / width;+ const maxScaleByHeight = MAX_PIXEL_DIMENSION / height;+ const maxScaleByTotal = Math.sqrt(MAX_TOTAL_PIXELS / (width * height));++ scaleFactor = Math.min(+ maxScaleByWidth,+ maxScaleByHeight,+ maxScaleByTotal,+ );++ // Ensure minimum scale factor of 1+ scaleFactor = Math.max(1, Math.floor(scaleFactor * 10) / 10); // Round down to 1 decimal++ console.log(+ `Large document detected. Reduced scale factor from ${width >= 1600 ? 2 : 3} to ${scaleFactor}`,+ );+ }++ return scaleFactor;+ };++ const scaleFactor = getOptimalScaleFactor(widthInPoints, heightInPoints);const doc_to_screen = mupdf.Matrix.scale(scaleFactor, scaleFactor);console.log("Scale factor:", scaleFactor);+ console.log(+ "Final dimensions:",+ `${widthInPoints * scaleFactor} × ${heightInPoints * scaleFactor}`,+ );// get linksconst links = page.getLinks();@@ -85,22 +142,72 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {return { href: link.getURI(), coords: link.getBounds().join(",") };});+ // Will be updated if we use a reduced scale factor+ let actualScaleFactor = scaleFactor;+const metadata = {originalWidth: widthInPoints,originalHeight: heightInPoints,- width: widthInPoints * scaleFactor,- height: heightInPoints * scaleFactor,- scaleFactor: scaleFactor,+ width: widthInPoints * actualScaleFactor,+ height: heightInPoints * actualScaleFactor,+ scaleFactor: actualScaleFactor,};- console.time("toPixmap");- let scaledPixmap = page.toPixmap(- // [3, 0, 0, 3, 0, 0], // scale 3x // to 300 DPI- doc_to_screen,- mupdf.ColorSpace.DeviceRGB,- false,- true,+ // Estimate memory usage before creating pixmap+ const finalWidth = Math.floor(widthInPoints * scaleFactor);+ const finalHeight = Math.floor(heightInPoints * scaleFactor);+ const estimatedMemoryMB = (finalWidth * finalHeight * 3) / (1024 * 1024); // RGB = 3 bytes per pixel++ console.log(+ `Estimated memory usage: ${estimatedMemoryMB.toFixed(1)}MB for ${finalWidth} × ${finalHeight} pixels`,);++ // Warn if memory usage is high+ if (estimatedMemoryMB > 200) {+ console.warn(+ `High memory usage expected: ${estimatedMemoryMB.toFixed(1)}MB. Consider reducing document size.`,+ );+ }++ console.time("toPixmap");+ let scaledPixmap;+ try {+ scaledPixmap = page.toPixmap(+ doc_to_screen,+ mupdf.ColorSpace.DeviceRGB,+ false,+ true,+ );+ } catch (error) {+ // If pixmap creation fails, try with a smaller scale factor+ console.error(+ "Pixmap creation failed, attempting with reduced scale factor:",+ error,+ );+ const reducedScaleFactor = Math.max(1, scaleFactor * 0.5);+ console.log(`Retrying with reduced scale factor: ${reducedScaleFactor}`);++ const reduced_doc_to_screen = mupdf.Matrix.scale(+ reducedScaleFactor,+ reducedScaleFactor,+ );+ scaledPixmap = page.toPixmap(+ reduced_doc_to_screen,+ mupdf.ColorSpace.DeviceRGB,+ false,+ true,+ );++ // Update metadata with actual scale factor used+ actualScaleFactor = reducedScaleFactor;+ metadata.width = widthInPoints * actualScaleFactor;+ metadata.height = heightInPoints * actualScaleFactor;+ metadata.scaleFactor = actualScaleFactor;+ console.log(+ "Successfully created pixmap with reduced scale factor:",+ actualScaleFactor,+ );+ }console.timeEnd("toPixmap");console.time("compare");