Skip to content

Commit

Permalink
chore: reduce memory and compress doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mfts committed Jul 6, 2024
1 parent 1b29ec9 commit 2fb484c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions pages/api/mupdf/convert-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
// Convert the response to a buffer
const pdfData = await response.arrayBuffer();
// Create a MuPDF instance
var doc = mupdf.Document.openDocument(pdfData, "application/pdf");
var doc = new mupdf.PDFDocument(pdfData);
console.log("Original document size:", pdfData.byteLength);

// compress the pdf
var pdfDoc = doc.saveToBuffer("compress=true,linearize=true,garbage=true");
console.log("Compressed document size:", pdfDoc.getLength());

var newDoc = new mupdf.PDFDocument(pdfDoc);

// Scale the document to 300 DPI
const doc_to_screen = mupdf.Matrix.scale(216 / 72, 216 / 72); // scale 3x // to 216 DPI

let page = doc.loadPage(pageNumber - 1); // 0-based page index
let page = newDoc.loadPage(pageNumber - 1); // 0-based page index

if (pageNumber === 1) {
// get the orientation of the document and update document version
Expand Down
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"functions": {
"pages/api/mupdf/convert-page.ts": {
"memory": 3009
"memory": 2048
}
}
}

0 comments on commit 2fb484c

Please sign in to comment.