mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
Merge 27c704951d into 5c9e590856
This commit is contained in:
commit
f7734523ae
@ -791,6 +791,16 @@ public class CompressController {
|
|||||||
currentFile = originalFile;
|
currentFile = originalFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply linearization as final step if requested
|
||||||
|
if (Boolean.TRUE.equals(request.getLinearize()) && isQpdfEnabled()) {
|
||||||
|
try {
|
||||||
|
applyLinearization(currentFile, tempFiles);
|
||||||
|
log.info("PDF linearization applied successfully");
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.warn("PDF linearization failed, continuing without linearization", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String outputFilename =
|
String outputFilename =
|
||||||
GeneralUtils.generateFilename(
|
GeneralUtils.generateFilename(
|
||||||
inputFile.getOriginalFilename(), "_Optimized.pdf");
|
inputFile.getOriginalFilename(), "_Optimized.pdf");
|
||||||
@ -810,6 +820,36 @@ public class CompressController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply PDF linearization using QPDF
|
||||||
|
private void applyLinearization(Path currentFile, List<Path> tempFiles) throws IOException {
|
||||||
|
log.info("Applying PDF linearization to: {}", currentFile);
|
||||||
|
|
||||||
|
// Create output file for linearization
|
||||||
|
Path linearizedOutputFile = Files.createTempFile("linearized_output_", ".pdf");
|
||||||
|
tempFiles.add(linearizedOutputFile);
|
||||||
|
|
||||||
|
// Build QPDF command for linearization only
|
||||||
|
List<String> command = new ArrayList<>();
|
||||||
|
command.add("qpdf");
|
||||||
|
command.add("--linearize");
|
||||||
|
command.add(currentFile.toString());
|
||||||
|
command.add(linearizedOutputFile.toString());
|
||||||
|
|
||||||
|
try {
|
||||||
|
ProcessExecutorResult returnCode =
|
||||||
|
ProcessExecutor.getInstance(ProcessExecutor.Processes.QPDF)
|
||||||
|
.runCommandWithOutputHandling(command);
|
||||||
|
|
||||||
|
// Update current file to the linearized output
|
||||||
|
Files.copy(linearizedOutputFile, currentFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
|
log.info("PDF linearization completed successfully");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("PDF linearization failed", e);
|
||||||
|
throw new IOException("PDF linearization failed", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Run Ghostscript compression
|
// Run Ghostscript compression
|
||||||
private void applyGhostscriptCompression(
|
private void applyGhostscriptCompression(
|
||||||
OptimizePdfRequest request, int optimizeLevel, Path currentFile, List<Path> tempFiles)
|
OptimizePdfRequest request, int optimizeLevel, Path currentFile, List<Path> tempFiles)
|
||||||
|
|||||||
@ -3324,6 +3324,9 @@
|
|||||||
"grayscale": {
|
"grayscale": {
|
||||||
"label": "Apply Grayscale for Compression"
|
"label": "Apply Grayscale for Compression"
|
||||||
},
|
},
|
||||||
|
"linearize": {
|
||||||
|
"label": "Optimize PDF for faster web viewing"
|
||||||
|
},
|
||||||
"tooltip": {
|
"tooltip": {
|
||||||
"header": {
|
"header": {
|
||||||
"title": "Compress Settings Overview"
|
"title": "Compress Settings Overview"
|
||||||
@ -3341,6 +3344,10 @@
|
|||||||
"grayscale": {
|
"grayscale": {
|
||||||
"title": "Grayscale",
|
"title": "Grayscale",
|
||||||
"text": "Select this option to convert all images to black and white, which can significantly reduce file size especially for scanned PDFs or image-heavy documents."
|
"text": "Select this option to convert all images to black and white, which can significantly reduce file size especially for scanned PDFs or image-heavy documents."
|
||||||
|
},
|
||||||
|
"linearize": {
|
||||||
|
"title": "Linearize",
|
||||||
|
"text": "Optimize PDF for faster web viewing by rearranging the PDF structure so the first page can be displayed quickly in web browsers."
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
|
|||||||
@ -129,6 +129,12 @@ const CompressSettings = ({ parameters, onParameterChange, disabled = false }: C
|
|||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
label={t("compress.grayscale.label", "Apply Grayscale for compression")}
|
label={t("compress.grayscale.label", "Apply Grayscale for compression")}
|
||||||
/>
|
/>
|
||||||
|
<Checkbox
|
||||||
|
checked={parameters.linearize}
|
||||||
|
onChange={(event) => onParameterChange('linearize', event.currentTarget.checked)}
|
||||||
|
disabled={disabled}
|
||||||
|
label={t("compress.linearize.label", "Linearize PDF for faster web viewing")}
|
||||||
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -19,6 +19,7 @@ export const buildCompressFormData = (parameters: CompressParameters, file: File
|
|||||||
}
|
}
|
||||||
|
|
||||||
formData.append("grayscale", parameters.grayscale.toString());
|
formData.append("grayscale", parameters.grayscale.toString());
|
||||||
|
formData.append("linearize", parameters.linearize.toString());
|
||||||
return formData;
|
return formData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ export interface CompressParameters extends BaseParameters {
|
|||||||
compressionMethod: 'quality' | 'filesize';
|
compressionMethod: 'quality' | 'filesize';
|
||||||
fileSizeValue: string;
|
fileSizeValue: string;
|
||||||
fileSizeUnit: 'KB' | 'MB';
|
fileSizeUnit: 'KB' | 'MB';
|
||||||
|
linearize: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultParameters: CompressParameters = {
|
export const defaultParameters: CompressParameters = {
|
||||||
@ -17,6 +18,7 @@ export const defaultParameters: CompressParameters = {
|
|||||||
compressionMethod: 'quality',
|
compressionMethod: 'quality',
|
||||||
fileSizeValue: '',
|
fileSizeValue: '',
|
||||||
fileSizeUnit: 'MB',
|
fileSizeUnit: 'MB',
|
||||||
|
linearize: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CompressParametersHook = BaseParametersHook<CompressParameters>;
|
export type CompressParametersHook = BaseParametersHook<CompressParameters>;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user