Skip to content

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

feat: adjust scalefactor based on size of document

Marc Seitz7058fe637007
diff --git a/jobs/convert-pdf-to-image.ts b/jobs/convert-pdf-to-image.ts
index 0d09b92d..c53d909c 100644
--- a/jobs/convert-pdf-to-image.ts
+++ b/jobs/convert-pdf-to-image.ts
@@ -134,7 +134,7 @@ client.defineJob({
currentPage = i + 1;
await io.runTask(
`upload-page-${currentPage}`,
- async ({ attempts }) => {
+ async () => {
// send page number to api/convert-page endpoint in a task and get back page img url
const response = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/mupdf/convert-page`,
@@ -145,7 +145,6 @@ client.defineJob({
pageNumber: currentPage,
url: signedUrl,
teamId: payload.teamId,
- attempts: attempts - 1,
}),
headers: {
"Content-Type": "application/json",
@@ -173,15 +172,15 @@ client.defineJob({
},
// { retry: retry.standardBackoff },
{
- retry: {
- limit: 3,
- minTimeoutInMs: 1000,
- maxTimeoutInMs: 10000,
- factor: 2,
- randomize: true,
- },
+ // retry: {
+ // limit: 3,
+ // minTimeoutInMs: 1000,
+ // maxTimeoutInMs: 10000,
+ // factor: 2,
+ // randomize: true,
+ // },
},
- (error) => {
+ (error, task) => {
conversionWithoutError = false;
return { error: error as Error };
},
diff --git a/pages/api/mupdf/convert-page.ts b/pages/api/mupdf/convert-page.ts
index f71189c8..e87015d0 100644
--- a/pages/api/mupdf/convert-page.ts
+++ b/pages/api/mupdf/convert-page.ts
@@ -12,8 +12,6 @@ export const config = {
maxDuration: 180,
};
-const SCALE_FACTORS = [3, 2, 1];
-
export default async (req: NextApiRequest, res: NextApiResponse) => {
// check if post method
if (req.method !== "POST") {
@@ -31,18 +29,11 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
return;
}
- const {
- documentVersionId,
- pageNumber,
- url,
- teamId,
- attempt = 0,
- } = req.body as {
+ const { documentVersionId, pageNumber, url, teamId } = req.body as {
documentVersionId: string;
pageNumber: number;
url: string;
teamId: string;
- attempt: number;
};
try {
@@ -65,30 +56,14 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
var doc = new mupdf.PDFDocument(pdfData);
console.log("Original document size:", pdfData.byteLength);
- const scaleFactor = SCALE_FACTORS[attempt] || 1;
- const desiredDPI = 72 * scaleFactor;
- const currentDPI = 72;
-
- // Calculate the scale factor to achieve the desired DPI
- const dpiScaleFactor = desiredDPI / currentDPI;
- const doc_to_screen = mupdf.Matrix.scale(dpiScaleFactor, dpiScaleFactor);
-
- console.log(
- `Converting with scale factor: ${scaleFactor}, DPI: ${desiredDPI}`,
- );
- console.log("attempt", attempt);
-
- // Scale the document to 300 DPI
- // const doc_to_screen = mupdf.Matrix.scale(216 / 72, 216 / 72); // scale 3x // to 216 DPI
- // const doc_to_screen = mupdf.Matrix.scale(2, 2); // scale 2x // to 144 DPI
-
- let page = doc.loadPage(pageNumber - 1); // 0-based page index
+ const page = doc.loadPage(pageNumber - 1); // 0-based page index
+ // get the bounds of the page for orientation and scaling
+ const bounds = page.getBounds();
+ const [ulx, uly, lrx, lry] = bounds;
+ const widthInPoints = Math.abs(lrx - ulx);
if (pageNumber === 1) {
// get the orientation of the document and update document version
- const bounds = page.getBounds();
- const [ulx, uly, lrx, lry] = bounds;
- const widthInPoints = Math.abs(lrx - ulx);
const heightInPoints = Math.abs(lry - uly);
const isVertical = heightInPoints > widthInPoints;
@@ -98,10 +73,17 @@ 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
+ const doc_to_screen = mupdf.Matrix.scale(scaleFactor, scaleFactor);
+
+ console.log("Scale factor:", scaleFactor);
+
// get links
const links = page.getLinks();
const embeddedLinks = links.map((link: any) => link.getURI());
+ console.time("toPixmap");
let scaledPixmap = page.toPixmap(
// [3, 0, 0, 3, 0, 0], // scale 3x // to 300 DPI
doc_to_screen,
@@ -109,9 +91,15 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
false,
true,
);
+ console.timeEnd("toPixmap");
+ console.time("compare");
+ console.time("asPNG");
const pngBuffer = scaledPixmap.asPNG(); // as PNG
+ console.timeEnd("asPNG");
+ console.time("asJPEG");
const jpegBuffer = scaledPixmap.asJPEG(80, false); // as JPEG
+ console.timeEnd("asJPEG");
const pngSize = pngBuffer.byteLength;
const jpegSize = jpegBuffer.byteLength;
@@ -126,6 +114,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
chosenFormat = "jpeg";
}
+ console.log("Chosen format:", chosenFormat);
+
+ console.timeEnd("compare");
+
let buffer = Buffer.from(chosenBuffer);
// get docId from url with starts with "doc_" with regex
@@ -143,6 +135,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
});
buffer = Buffer.alloc(0); // free memory
+ chosenBuffer = Buffer.alloc(0); // free memory
scaledPixmap.destroy(); // free memory
page.destroy(); // free memory