mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
line art (#5052)
## Summary - introduce a shared line art conversion interface and proprietary ImageMagick-backed implementation - have the compress controller optionally autowire the enterprise service before running per-image line art processing - remove ImageMagick command details from core by delegating conversions through the proprietary service ## Testing - not run (not requested) ------ [Codex Task](https://chatgpt.com/codex/tasks/task_b_6928aecceaf083289a9269b1ca99307e) --------- Co-authored-by: James Brunton <[email protected]>
This commit is contained in:
co-authored by
James Brunton
parent
33188815da
commit
5f72c05623
@@ -491,6 +491,9 @@ public class EndpointConfiguration {
|
||||
addEndpointToGroup("Ghostscript", "repair");
|
||||
addEndpointToGroup("Ghostscript", "compress-pdf");
|
||||
|
||||
/* ImageMagick */
|
||||
addEndpointToGroup("ImageMagick", "compress-pdf");
|
||||
|
||||
/* tesseract */
|
||||
addEndpointToGroup("tesseract", "ocr-pdf");
|
||||
|
||||
@@ -574,6 +577,7 @@ public class EndpointConfiguration {
|
||||
|| "Javascript".equals(group)
|
||||
|| "Weasyprint".equals(group)
|
||||
|| "Pdftohtml".equals(group)
|
||||
|| "ImageMagick".equals(group)
|
||||
|| "rar".equals(group);
|
||||
}
|
||||
|
||||
|
||||
@@ -653,6 +653,7 @@ public class ApplicationProperties {
|
||||
private int weasyPrintSessionLimit;
|
||||
private int installAppSessionLimit;
|
||||
private int calibreSessionLimit;
|
||||
private int imageMagickSessionLimit;
|
||||
private int qpdfSessionLimit;
|
||||
private int tesseractSessionLimit;
|
||||
private int ghostscriptSessionLimit;
|
||||
@@ -690,6 +691,10 @@ public class ApplicationProperties {
|
||||
return calibreSessionLimit > 0 ? calibreSessionLimit : 1;
|
||||
}
|
||||
|
||||
public int getImageMagickSessionLimit() {
|
||||
return imageMagickSessionLimit > 0 ? imageMagickSessionLimit : 4;
|
||||
}
|
||||
|
||||
public int getGhostscriptSessionLimit() {
|
||||
return ghostscriptSessionLimit > 0 ? ghostscriptSessionLimit : 8;
|
||||
}
|
||||
@@ -719,6 +724,8 @@ public class ApplicationProperties {
|
||||
@JsonProperty("calibretimeoutMinutes")
|
||||
private long calibreTimeoutMinutes;
|
||||
|
||||
private long imageMagickTimeoutMinutes;
|
||||
|
||||
private long tesseractTimeoutMinutes;
|
||||
private long qpdfTimeoutMinutes;
|
||||
private long ghostscriptTimeoutMinutes;
|
||||
@@ -756,6 +763,10 @@ public class ApplicationProperties {
|
||||
return calibreTimeoutMinutes > 0 ? calibreTimeoutMinutes : 30;
|
||||
}
|
||||
|
||||
public long getImageMagickTimeoutMinutes() {
|
||||
return imageMagickTimeoutMinutes > 0 ? imageMagickTimeoutMinutes : 30;
|
||||
}
|
||||
|
||||
public long getGhostscriptTimeoutMinutes() {
|
||||
return ghostscriptTimeoutMinutes > 0 ? ghostscriptTimeoutMinutes : 30;
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package stirling.software.common.service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||
|
||||
public interface LineArtConversionService {
|
||||
PDImageXObject convertImageToLineArt(
|
||||
PDDocument doc, PDImageXObject originalImage, double threshold, int edgeLevel)
|
||||
throws IOException;
|
||||
}
|
||||
@@ -86,6 +86,11 @@ public class ProcessExecutor {
|
||||
.getProcessExecutor()
|
||||
.getSessionLimit()
|
||||
.getCalibreSessionLimit();
|
||||
case IMAGEMAGICK ->
|
||||
applicationProperties
|
||||
.getProcessExecutor()
|
||||
.getSessionLimit()
|
||||
.getImageMagickSessionLimit();
|
||||
case GHOSTSCRIPT ->
|
||||
applicationProperties
|
||||
.getProcessExecutor()
|
||||
@@ -141,6 +146,11 @@ public class ProcessExecutor {
|
||||
.getProcessExecutor()
|
||||
.getTimeoutMinutes()
|
||||
.getCalibreTimeoutMinutes();
|
||||
case IMAGEMAGICK ->
|
||||
applicationProperties
|
||||
.getProcessExecutor()
|
||||
.getTimeoutMinutes()
|
||||
.getImageMagickTimeoutMinutes();
|
||||
case GHOSTSCRIPT ->
|
||||
applicationProperties
|
||||
.getProcessExecutor()
|
||||
@@ -301,6 +311,7 @@ public class ProcessExecutor {
|
||||
WEASYPRINT,
|
||||
INSTALL_APP,
|
||||
CALIBRE,
|
||||
IMAGEMAGICK,
|
||||
TESSERACT,
|
||||
QPDF,
|
||||
GHOSTSCRIPT,
|
||||
|
||||
@@ -46,6 +46,7 @@ public class ExternalAppDepConfig {
|
||||
put("qpdf", List.of("qpdf"));
|
||||
put("tesseract", List.of("tesseract"));
|
||||
put("rar", List.of("rar")); // Required for real CBR output
|
||||
put("magick", List.of("ImageMagick"));
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -128,6 +129,7 @@ public class ExternalAppDepConfig {
|
||||
checkDependencyAndDisableGroup("pdftohtml");
|
||||
checkDependencyAndDisableGroup(unoconvPath);
|
||||
checkDependencyAndDisableGroup("rar");
|
||||
checkDependencyAndDisableGroup("magick");
|
||||
// Special handling for Python/OpenCV dependencies
|
||||
boolean pythonAvailable = isCommandAvailable("python3") || isCommandAvailable("python");
|
||||
if (!pythonAvailable) {
|
||||
|
||||
+103
@@ -28,10 +28,13 @@ import org.apache.pdfbox.pdmodel.PDResources;
|
||||
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
||||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
|
||||
@@ -44,6 +47,7 @@ import stirling.software.SPDF.model.api.misc.OptimizePdfRequest;
|
||||
import stirling.software.common.annotations.AutoJobPostMapping;
|
||||
import stirling.software.common.annotations.api.MiscApi;
|
||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||
import stirling.software.common.service.LineArtConversionService;
|
||||
import stirling.software.common.util.ExceptionUtils;
|
||||
import stirling.software.common.util.GeneralUtils;
|
||||
import stirling.software.common.util.ProcessExecutor;
|
||||
@@ -58,6 +62,9 @@ public class CompressController {
|
||||
private final CustomPDFDocumentFactory pdfDocumentFactory;
|
||||
private final EndpointConfiguration endpointConfiguration;
|
||||
|
||||
@Autowired(required = false)
|
||||
private LineArtConversionService lineArtConversionService;
|
||||
|
||||
private boolean isQpdfEnabled() {
|
||||
return endpointConfiguration.isGroupEnabled("qpdf");
|
||||
}
|
||||
@@ -66,6 +73,10 @@ public class CompressController {
|
||||
return endpointConfiguration.isGroupEnabled("Ghostscript");
|
||||
}
|
||||
|
||||
private boolean isImageMagickEnabled() {
|
||||
return endpointConfiguration.isGroupEnabled("ImageMagick");
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@@ -660,6 +671,9 @@ public class CompressController {
|
||||
Integer optimizeLevel = request.getOptimizeLevel();
|
||||
String expectedOutputSizeString = request.getExpectedOutputSize();
|
||||
Boolean convertToGrayscale = request.getGrayscale();
|
||||
Boolean convertToLineArt = request.getLineArt();
|
||||
Double lineArtThreshold = request.getLineArtThreshold();
|
||||
Integer lineArtEdgeLevel = request.getLineArtEdgeLevel();
|
||||
if (expectedOutputSizeString == null && optimizeLevel == null) {
|
||||
throw new Exception("Both expected output size and optimize level are not specified");
|
||||
}
|
||||
@@ -689,6 +703,26 @@ public class CompressController {
|
||||
optimizeLevel = determineOptimizeLevel(sizeReductionRatio);
|
||||
}
|
||||
|
||||
if (Boolean.TRUE.equals(convertToLineArt)) {
|
||||
if (lineArtConversionService == null) {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.FORBIDDEN,
|
||||
"Line art conversion is unavailable - ImageMagick service not found");
|
||||
}
|
||||
if (!isImageMagickEnabled()) {
|
||||
throw new IOException(
|
||||
"ImageMagick is not enabled but line art conversion was requested");
|
||||
}
|
||||
double thresholdValue =
|
||||
lineArtThreshold == null
|
||||
? 55d
|
||||
: Math.min(100d, Math.max(0d, lineArtThreshold));
|
||||
int edgeLevel =
|
||||
lineArtEdgeLevel == null ? 1 : Math.min(3, Math.max(1, lineArtEdgeLevel));
|
||||
currentFile =
|
||||
applyLineArtConversion(currentFile, tempFiles, thresholdValue, edgeLevel);
|
||||
}
|
||||
|
||||
boolean sizeMet = false;
|
||||
boolean imageCompressionApplied = false;
|
||||
boolean externalCompressionApplied = false;
|
||||
@@ -810,6 +844,75 @@ public class CompressController {
|
||||
}
|
||||
}
|
||||
|
||||
private Path applyLineArtConversion(
|
||||
Path currentFile, List<Path> tempFiles, double threshold, int edgeLevel)
|
||||
throws IOException {
|
||||
|
||||
Path lineArtFile = Files.createTempFile("lineart_output_", ".pdf");
|
||||
tempFiles.add(lineArtFile);
|
||||
|
||||
try (PDDocument doc = pdfDocumentFactory.load(currentFile.toFile())) {
|
||||
Map<String, List<ImageReference>> uniqueImages = findImages(doc);
|
||||
CompressionStats stats = new CompressionStats();
|
||||
stats.uniqueImagesCount = uniqueImages.size();
|
||||
calculateImageStats(uniqueImages, stats);
|
||||
|
||||
Map<String, PDImageXObject> convertedImages =
|
||||
createLineArtImages(doc, uniqueImages, stats, threshold, edgeLevel);
|
||||
|
||||
replaceImages(doc, uniqueImages, convertedImages, stats);
|
||||
|
||||
log.info(
|
||||
"Applied line art conversion to {} unique images ({} total references)",
|
||||
stats.uniqueImagesCount,
|
||||
stats.totalImages);
|
||||
|
||||
doc.save(lineArtFile.toString());
|
||||
return lineArtFile;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, PDImageXObject> createLineArtImages(
|
||||
PDDocument doc,
|
||||
Map<String, List<ImageReference>> uniqueImages,
|
||||
CompressionStats stats,
|
||||
double threshold,
|
||||
int edgeLevel)
|
||||
throws IOException {
|
||||
|
||||
Map<String, PDImageXObject> convertedImages = new HashMap<>();
|
||||
|
||||
for (Entry<String, List<ImageReference>> entry : uniqueImages.entrySet()) {
|
||||
String imageHash = entry.getKey();
|
||||
List<ImageReference> references = entry.getValue();
|
||||
if (references.isEmpty()) continue;
|
||||
|
||||
PDImageXObject originalImage = getOriginalImage(doc, references.get(0));
|
||||
|
||||
int originalSize = (int) originalImage.getCOSObject().getLength();
|
||||
stats.totalOriginalBytes += originalSize;
|
||||
|
||||
PDImageXObject converted =
|
||||
lineArtConversionService.convertImageToLineArt(
|
||||
doc, originalImage, threshold, edgeLevel);
|
||||
convertedImages.put(imageHash, converted);
|
||||
stats.compressedImages++;
|
||||
|
||||
int convertedSize = (int) converted.getCOSObject().getLength();
|
||||
stats.totalCompressedBytes += convertedSize * references.size();
|
||||
|
||||
double reductionPercentage = 100.0 - ((convertedSize * 100.0) / originalSize);
|
||||
log.info(
|
||||
"Image hash {}: Line art conversion {} → {} (reduced by {}%)",
|
||||
imageHash,
|
||||
GeneralUtils.formatBytes(originalSize),
|
||||
GeneralUtils.formatBytes(convertedSize),
|
||||
String.format("%.1f", reductionPercentage));
|
||||
}
|
||||
|
||||
return convertedImages;
|
||||
}
|
||||
|
||||
// Run Ghostscript compression
|
||||
private void applyGhostscriptCompression(
|
||||
OptimizePdfRequest request, int optimizeLevel, Path currentFile, List<Path> tempFiles)
|
||||
|
||||
+6
@@ -230,4 +230,10 @@ public class ConfigController {
|
||||
}
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@GetMapping("/group-enabled")
|
||||
public ResponseEntity<Boolean> isGroupEnabled(@RequestParam(name = "group") String group) {
|
||||
boolean enabled = endpointConfiguration.isGroupEnabled(group);
|
||||
return ResponseEntity.ok(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -74,13 +74,13 @@ public class ReactRoutingController {
|
||||
}
|
||||
|
||||
@GetMapping(
|
||||
"/{path:^(?!api|static|robots\\.txt|favicon\\.ico|manifest.*\\.json|pipeline|pdfjs|pdfjs-legacy|fonts|images|files|css|js|assets|locales|modern-logo|classic-logo|Login|og_images|samples)[^\\.]*$}")
|
||||
"/{path:^(?!api|static|robots\\.txt|favicon\\.ico|manifest.*\\.json|pipeline|pdfjs|pdfjs-legacy|pdfium|fonts|images|files|css|js|assets|locales|modern-logo|classic-logo|Login|og_images|samples)[^\\.]*$}")
|
||||
public ResponseEntity<String> forwardRootPaths(HttpServletRequest request) throws IOException {
|
||||
return serveIndexHtml(request);
|
||||
}
|
||||
|
||||
@GetMapping(
|
||||
"/{path:^(?!api|static|pipeline|pdfjs|pdfjs-legacy|fonts|images|files|css|js|assets|locales|modern-logo|classic-logo|Login|og_images|samples)[^\\.]*}/{subpath:^(?!.*\\.).*$}")
|
||||
"/{path:^(?!api|static|pipeline|pdfjs|pdfjs-legacy|pdfium|fonts|images|files|css|js|assets|locales|modern-logo|classic-logo|Login|og_images|samples)[^\\.]*}/{subpath:^(?!.*\\.).*$}")
|
||||
public ResponseEntity<String> forwardNestedPaths(HttpServletRequest request)
|
||||
throws IOException {
|
||||
return serveIndexHtml(request);
|
||||
|
||||
@@ -45,4 +45,26 @@ public class OptimizePdfRequest extends PDFFile {
|
||||
requiredMode = Schema.RequiredMode.REQUIRED,
|
||||
defaultValue = "false")
|
||||
private Boolean grayscale = false;
|
||||
|
||||
@Schema(
|
||||
description =
|
||||
"Whether to convert images to high-contrast line art using ImageMagick. Default is false.",
|
||||
requiredMode = Schema.RequiredMode.NOT_REQUIRED,
|
||||
defaultValue = "false")
|
||||
private Boolean lineArt = false;
|
||||
|
||||
@Schema(
|
||||
description = "Threshold to use for line art conversion (0-100).",
|
||||
requiredMode = Schema.RequiredMode.NOT_REQUIRED,
|
||||
defaultValue = "55")
|
||||
private Double lineArtThreshold = 55d;
|
||||
|
||||
@Schema(
|
||||
description =
|
||||
"Edge detection strength to use for line art conversion (1-3). This maps to"
|
||||
+ " ImageMagick's -edge radius.",
|
||||
requiredMode = Schema.RequiredMode.NOT_REQUIRED,
|
||||
defaultValue = "1",
|
||||
allowableValues = {"1", "2", "3"})
|
||||
private Integer lineArtEdgeLevel = 1;
|
||||
}
|
||||
|
||||
@@ -224,6 +224,7 @@ processExecutor:
|
||||
weasyPrintSessionLimit: 16
|
||||
installAppSessionLimit: 1
|
||||
calibreSessionLimit: 1
|
||||
imageMagickSessionLimit: 4
|
||||
ghostscriptSessionLimit: 8
|
||||
ocrMyPdfSessionLimit: 2
|
||||
timeoutMinutes: # Process executor timeout in minutes
|
||||
@@ -233,6 +234,7 @@ processExecutor:
|
||||
weasyPrinttimeoutMinutes: 30
|
||||
installApptimeoutMinutes: 60
|
||||
calibretimeoutMinutes: 30
|
||||
imageMagickTimeoutMinutes: 30
|
||||
tesseractTimeoutMinutes: 30
|
||||
qpdfTimeoutMinutes: 30
|
||||
ghostscriptTimeoutMinutes: 30
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package stirling.software.proprietary.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import stirling.software.common.service.LineArtConversionService;
|
||||
import stirling.software.common.util.ProcessExecutor;
|
||||
import stirling.software.common.util.ProcessExecutor.ProcessExecutorResult;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ImageMagickLineArtConversionService implements LineArtConversionService {
|
||||
|
||||
@Override
|
||||
public PDImageXObject convertImageToLineArt(
|
||||
PDDocument doc, PDImageXObject originalImage, double threshold, int edgeLevel)
|
||||
throws IOException {
|
||||
|
||||
Path inputImage = Files.createTempFile("lineart_image_input_", ".png");
|
||||
Path outputImage = Files.createTempFile("lineart_image_output_", ".tiff");
|
||||
|
||||
try {
|
||||
ImageIO.write(originalImage.getImage(), "png", inputImage.toFile());
|
||||
|
||||
List<String> command = new ArrayList<>();
|
||||
command.add("magick");
|
||||
command.add(inputImage.toString());
|
||||
command.add("-colorspace");
|
||||
command.add("Gray");
|
||||
|
||||
// Edge-aware line art conversion using ImageMagick's built-in operators.
|
||||
// -edge/-negate/-normalize are standard convert options (IM v6+/v7) that
|
||||
// accentuate outlines before thresholding to a bilevel image.
|
||||
command.add("-edge");
|
||||
command.add(String.valueOf(edgeLevel));
|
||||
command.add("-negate");
|
||||
command.add("-normalize");
|
||||
|
||||
command.add("-type");
|
||||
command.add("Bilevel");
|
||||
command.add("-threshold");
|
||||
command.add(String.format(Locale.ROOT, "%.1f%%", threshold));
|
||||
command.add("-compress");
|
||||
command.add("Group4");
|
||||
command.add(outputImage.toString());
|
||||
|
||||
ProcessExecutorResult result =
|
||||
ProcessExecutor.getInstance(ProcessExecutor.Processes.IMAGEMAGICK)
|
||||
.runCommandWithOutputHandling(command);
|
||||
|
||||
if (result.getRc() != 0) {
|
||||
log.warn(
|
||||
"ImageMagick line art conversion failed with return code: {}",
|
||||
result.getRc());
|
||||
throw new IOException("ImageMagick line art conversion failed");
|
||||
}
|
||||
|
||||
byte[] convertedBytes = Files.readAllBytes(outputImage);
|
||||
return PDImageXObject.createFromByteArray(
|
||||
doc, convertedBytes, originalImage.getCOSObject().toString());
|
||||
} catch (Exception e) {
|
||||
log.warn("ImageMagick line art conversion failed", e);
|
||||
throw new IOException("ImageMagick line art conversion failed", e);
|
||||
} finally {
|
||||
Files.deleteIfExists(inputImage);
|
||||
Files.deleteIfExists(outputImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user