compression logic : improve compression levels and output sizing#10
Open
koppadipranaykumar wants to merge 1 commit into
Open
Conversation
Contributor
|
@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. |
Contributor
There was a problem hiding this comment.
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; |
Owner
|
@koppadipranaykumar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)compressWithQuality()logicCompression Levels
Screenshots
Before this update:
After this update:
Observed expected behavior:
Checklist
npm run devruns successfully