Skip to content

compression logic : improve compression levels and output sizing#10

Open
koppadipranaykumar wants to merge 1 commit into
JhaSourav07:mainfrom
koppadipranaykumar:feature/improve-pdf-compression-logic
Open

compression logic : improve compression levels and output sizing#10
koppadipranaykumar wants to merge 1 commit into
JhaSourav07:mainfrom
koppadipranaykumar:feature/improve-pdf-compression-logic

Conversation

@koppadipranaykumar
Copy link
Copy Markdown

Summary

Closes #1

This PR improves the PDF compression tool by making all three compression levels more meaningful, consistent, and user-friendly.

Previously, some compression modes could generate outputs close to or larger than the original file size depending on the PDF. This update improves the compression engine so each tier now behaves as expected.


What Changed

Compression Logic (pdf.service.js)

  • Reworked compressWithQuality() logic
  • Improved differentiation between all 3 modes
  • Retained strong Extreme compression

Compression Levels

  • High Quality → light compression with preserved clarity
  • Balanced → moderate compression with noticeable reduction
  • Extreme → aggressive compression for maximum size savings

Screenshots

Before this update:

image image image

After this update:

image image image

Observed expected behavior:

  • High Quality = minor reduction
  • Balanced = medium reduction
  • Extreme = maximum reduction

Checklist

  • npm run dev runs successfully
  • Compression tested locally
  • No backend/server uploads introduced
  • Focused PR for assigned issue

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 15, 2026

@koppadipranaykumar is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the client-side PDF compression implementation to make the three “quality” tiers produce more distinct output sizes by re-encoding pages as rasterized JPEGs before rebuilding the PDF.

Changes:

  • Reworked compressWithQuality() to render each PDF page via PDF.js, encode as JPEG, and embed into a new PDF via pdf-lib.
  • Introduced tier-based parameters (scale, jpegQuality) to differentiate High Quality / Balanced / Extreme output sizing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +135 to +137
const sourcePdf = await pdfjsLib.getDocument({
data: arrayBuffer,
}).promise;
Comment on lines +135 to +137
const sourcePdf = await pdfjsLib.getDocument({
data: arrayBuffer,
}).promise;
Comment on lines +164 to +180
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");

canvas.width = Math.floor(viewport.width);
canvas.height = Math.floor(viewport.height);

await page.render({
canvasContext: context,
viewport,
}).promise;

const imageBlob = await new Promise((resolve) => {
canvas.toBlob(resolve, "image/jpeg", jpegQuality);
});

const imageBytes = await imageBlob.arrayBuffer();

Comment on lines +167 to +193
canvas.width = Math.floor(viewport.width);
canvas.height = Math.floor(viewport.height);

await page.render({
canvasContext: context,
viewport,
}).promise;

const imageBlob = await new Promise((resolve) => {
canvas.toBlob(resolve, "image/jpeg", jpegQuality);
});

const imageBytes = await imageBlob.arrayBuffer();

const jpgImage = await outputPdf.embedJpg(imageBytes);

const pdfPage = outputPdf.addPage([
viewport.width,
viewport.height,
]);

pdfPage.drawImage(jpgImage, {
x: 0,
y: 0,
width: viewport.width,
height: viewport.height,
});
Comment on lines +159 to +193
for (let pageNumber = 1; pageNumber <= sourcePdf.numPages; pageNumber++) {
const page = await sourcePdf.getPage(pageNumber);

const viewport = page.getViewport({ scale });

const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");

canvas.width = Math.floor(viewport.width);
canvas.height = Math.floor(viewport.height);

await page.render({
canvasContext: context,
viewport,
}).promise;

const imageBlob = await new Promise((resolve) => {
canvas.toBlob(resolve, "image/jpeg", jpegQuality);
});

const imageBytes = await imageBlob.arrayBuffer();

const jpgImage = await outputPdf.embedJpg(imageBytes);

const pdfPage = outputPdf.addPage([
viewport.width,
viewport.height,
]);

pdfPage.drawImage(jpgImage, {
x: 0,
y: 0,
width: viewport.width,
height: viewport.height,
});
Comment on lines +147 to +151
scale = 0.92;
jpegQuality = 0.72;
} else if (quality >= 0.55) {
// BALANCED
scale = 0.95;
@JhaSourav07
Copy link
Copy Markdown
Owner

@koppadipranaykumar
Thanks for opening the PR.
Please look into the comments left by Copilot , after implementing it I will review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better Compression Logic

3 participants