feat: compression level

This commit is contained in:
Ibrahima G. Coulibaly
2025-04-03 02:02:09 +00:00
parent 82e51b6f91
commit 219cc653dd
4 changed files with 61 additions and 64 deletions

48
.idea/workspace.xml generated
View File

@@ -4,13 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="refactor: compress pdf"> <list default="true" id="b30e2810-c4c1-4aad-b134-794e52cc1c7d" name="Changes" comment="fix: path">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/lib/background-worker.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib/ghostscript/background-worker.js" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/lib/ghostscript/background-worker.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib/ghostscript/background-worker.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/lib/gs-worker.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib/ghostscript/gs-worker.js" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/pages/tools/pdf/compress-pdf/index.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/tools/pdf/compress-pdf/index.tsx" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/lib/worker-init.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib/ghostscript/worker-init.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/pages/tools/pdf/compress-pdf/service.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/tools/pdf/compress-pdf/service.ts" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/pages/tools/pdf/compress-pdf/service.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/pages/tools/pdf/compress-pdf/service.ts" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tsconfig.json" beforeDir="false" afterPath="$PROJECT_DIR$/tsconfig.json" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -411,22 +409,6 @@
<workItem from="1743397561176" duration="25000" /> <workItem from="1743397561176" duration="25000" />
<workItem from="1743458576265" duration="13083000" /> <workItem from="1743458576265" duration="13083000" />
</task> </task>
<task id="LOCAL-00135" summary="feat: jakarta font">
<option name="closed" value="true" />
<created>1740665609483</created>
<option name="number" value="00135" />
<option name="presentableId" value="LOCAL-00135" />
<option name="project" value="LOCAL" />
<updated>1740665609483</updated>
</task>
<task id="LOCAL-00136" summary="chore: img">
<option name="closed" value="true" />
<created>1740680778110</created>
<option name="number" value="00136" />
<option name="presentableId" value="LOCAL-00136" />
<option name="project" value="LOCAL" />
<updated>1740680778110</updated>
</task>
<task id="LOCAL-00137" summary="docs: readme"> <task id="LOCAL-00137" summary="docs: readme">
<option name="closed" value="true" /> <option name="closed" value="true" />
<created>1740788899030</created> <created>1740788899030</created>
@@ -803,7 +785,23 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1743644703042</updated> <updated>1743644703042</updated>
</task> </task>
<option name="localTasksCounter" value="184" /> <task id="LOCAL-00184" summary="refactor: lib">
<option name="closed" value="true" />
<created>1743644942488</created>
<option name="number" value="00184" />
<option name="presentableId" value="LOCAL-00184" />
<option name="project" value="LOCAL" />
<updated>1743644942488</updated>
</task>
<task id="LOCAL-00185" summary="fix: path">
<option name="closed" value="true" />
<created>1743645074051</created>
<option name="number" value="00185" />
<option name="presentableId" value="LOCAL-00185" />
<option name="project" value="LOCAL" />
<updated>1743645074051</updated>
</task>
<option name="localTasksCounter" value="186" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -850,8 +848,6 @@
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" /> <option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<option name="CHECK_NEW_TODO" value="false" /> <option name="CHECK_NEW_TODO" value="false" />
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" /> <option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
<MESSAGE value="fix: prettify json" />
<MESSAGE value="refactor: sum" />
<MESSAGE value="fix: tools by category scroll" /> <MESSAGE value="fix: tools by category scroll" />
<MESSAGE value="fix: missing meta" /> <MESSAGE value="fix: missing meta" />
<MESSAGE value="feat: trim video" /> <MESSAGE value="feat: trim video" />
@@ -875,7 +871,9 @@
<MESSAGE value="fix: stars button width for 1k+ " /> <MESSAGE value="fix: stars button width for 1k+ " />
<MESSAGE value="feat: compress pdf" /> <MESSAGE value="feat: compress pdf" />
<MESSAGE value="refactor: compress pdf" /> <MESSAGE value="refactor: compress pdf" />
<option name="LAST_COMMIT_MESSAGE" value="refactor: compress pdf" /> <MESSAGE value="refactor: lib" />
<MESSAGE value="fix: path" />
<option name="LAST_COMMIT_MESSAGE" value="fix: path" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState"> <component name="XSLT-Support.FileAssociations.UIState">
<expand /> <expand />

View File

@@ -5,6 +5,23 @@ function loadScript() {
var Module; var Module;
function _GSPS2PDF(dataStruct, responseCallback) { function _GSPS2PDF(dataStruct, responseCallback) {
const compressionLevel = dataStruct.compressionLevel || 'medium';
// Set PDF settings based on compression level
let pdfSettings;
switch (compressionLevel) {
case 'low':
pdfSettings = '/printer'; // Higher quality, less compression
break;
case 'medium':
pdfSettings = '/ebook'; // Medium quality and compression
break;
case 'high':
pdfSettings = '/screen'; // Lower quality, higher compression
break;
default:
pdfSettings = '/ebook'; // Default to medium
}
// first download the ps data // first download the ps data
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', dataStruct.psDataURL); xhr.open('GET', dataStruct.psDataURL);
@@ -33,7 +50,7 @@ function _GSPS2PDF(dataStruct, responseCallback) {
arguments: [ arguments: [
'-sDEVICE=pdfwrite', '-sDEVICE=pdfwrite',
'-dCompatibilityLevel=1.4', '-dCompatibilityLevel=1.4',
'-dPDFSETTINGS=/ebook', `-dPDFSETTINGS=${pdfSettings}`,
'-DNOPAUSE', '-DNOPAUSE',
'-dQUIET', '-dQUIET',
'-dBATCH', '-dBATCH',

View File

@@ -152,7 +152,6 @@ export default function CompressPdf({
setInput={setInput} setInput={setInput}
initialValues={initialValues} initialValues={initialValues}
compute={compute} compute={compute}
exampleCards={exampleCards}
inputComponent={ inputComponent={
<ToolPdfInput <ToolPdfInput
value={input} value={input}
@@ -175,22 +174,23 @@ export default function CompressPdf({
title: 'Compression Settings', title: 'Compression Settings',
component: ( component: (
<Box> <Box>
<Typography variant="subtitle2" sx={{ mb: 1 }}> <Box>
Compression Level <Typography variant="subtitle2" sx={{ mb: 1 }}>
</Typography> Compression Level
</Typography>
{compressionOptions.map((option) => (
<SimpleRadio
key={option.value}
title={option.label}
description={option.description}
checked={values.compressionLevel === option.value}
onClick={() => {
updateField('compressionLevel', option.value);
}}
/>
))}
{compressionOptions.map((option) => (
<SimpleRadio
key={option.value}
title={option.label}
description={option.description}
checked={values.compressionLevel === option.value}
onClick={() => {
updateField('compressionLevel', option.value);
}}
/>
))}
</Box>
{fileInfo && ( {fileInfo && (
<Box <Box
sx={{ sx={{
@@ -217,27 +217,6 @@ export default function CompressPdf({
) )
} }
]} ]}
toolInfo={{
title: 'How to Use the Compress PDF Tool',
description: `This tool allows you to compress PDF files securely in your browser using Ghostscript, a powerful PDF processing engine. Your files never leave your device, ensuring complete privacy and security.
Choose a compression level:
- Low Compression: Slightly reduces file size with minimal quality loss (72 dpi images)
- Medium Compression: Balances between file size and quality (150 dpi images) - Recommended for most cases
- High Compression: Maximum file size reduction with some quality loss (300 dpi images)
How it works:
1. Upload your PDF file
2. Select your desired compression level
3. Click "Compress" and wait for processing
4. Download your compressed PDF
The tool uses WebAssembly to run Ghostscript directly in your browser, which is why the first compression might take a moment to load the necessary components (about 18MB).
Note: The compression results may vary depending on the content of your PDF. Documents with many images will typically see greater size reduction than text-only documents.
${longDescription}`
}}
/> />
); );
} }

View File

@@ -18,7 +18,10 @@ export async function compressPdf(
throw new Error('The provided file is not a PDF'); throw new Error('The provided file is not a PDF');
} }
const dataObject = { psDataURL: URL.createObjectURL(pdfFile) }; const dataObject = {
psDataURL: URL.createObjectURL(pdfFile),
compressionLevel: options.compressionLevel
};
const compressedFileUrl: string = await compressWithGhostScript(dataObject); const compressedFileUrl: string = await compressWithGhostScript(dataObject);
return await loadPDFData(compressedFileUrl, pdfFile.name); return await loadPDFData(compressedFileUrl, pdfFile.name);
} }