try to use less memory while image merging

This commit is contained in:
Jörg Prante 2021-03-02 17:40:35 +01:00
parent 4ad07247b2
commit a091bc52cf
8 changed files with 50 additions and 18 deletions

View file

@ -14,8 +14,8 @@ test {
failFast = false
environment 'TMPDIR', '/var/tmp/gs'
systemProperty 'java.awt.headless', 'true'
systemProperty 'jna.debug', 'true'
//systemProperty 'jna.library.path', file('src/test/resources/').toString()
systemProperty 'java.io.tmpdir', '/var/tmp/'
systemProperty 'jna.debug', 'false'
systemProperty 'java.util.logging.config.file', 'src/test/resources/logging.properties'
testLogging {
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'

View file

@ -24,7 +24,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;
@DisabledOnOs(OS.MAC)
@DisabledOnOs({OS.MAC, OS.LINUX})
public class PDFRendererTest {
private Locale originalDefaultLocale;
@ -98,6 +98,7 @@ public class PDFRendererTest {
int height = (int) (symbol.getHeight() * magnification);
Rectangle rectangle = new Rectangle(0, 0, width, height);
PDFGraphics2D pdfGraphics2D = new PDFGraphics2D(rectangle);
// we need a trick here to generate HELVETICA font statements
pdfGraphics2D.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
BarcodeGraphicsRenderer barcodeGraphicsRenderer = new BarcodeGraphicsRenderer(pdfGraphics2D,
rectangle, magnification, paper, ink, false, false);

View file

@ -83,7 +83,8 @@ public class PDFConverter {
int processColorModel,
int pdfsettings,
String compatibilityLevel,
boolean pdfx, PaperSize paperSize) {
boolean pdfx,
PaperSize paperSize) {
this.autoRotatePages = autoRotatePages;
this.processColorModel = processColorModel;
this.pdfsettings = pdfsettings;

View file

@ -4,6 +4,7 @@ import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
@ -203,7 +204,7 @@ public class PDFRasterizer implements Closeable {
int pagecount = 0;
List<PDDocument> coverPageDocs = new ArrayList<>();
try (Stream<Path> files = Files.list(sourceDir);
PDDocument pdDocument = new PDDocument();
PDDocument pdDocument = new PDDocument(MemoryUsageSetting.setupTempFileOnly());
OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(targetFile))) {
List<Path> entries = files.sorted()
.filter(PDFRasterizer::checkForRealFile)
@ -371,27 +372,23 @@ public class PDFRasterizer implements Closeable {
Map<String, ImageReader> map = new LinkedHashMap<>();
ImageReader pngReader = getImageReader("png");
if (pngReader != null) {
ImageReadParam param = pngReader.getDefaultReadParam();
logger.log(Level.FINE, "PNG reader: " + pngReader.getClass().getName() + " param: " + param);
logger.log(Level.FINE, "PNG reader: " + pngReader.getClass().getName());
map.put("png", pngReader);
}
ImageReader pnmReader = getImageReader("pnm");
if (pnmReader != null) {
ImageReadParam param = pnmReader.getDefaultReadParam();
logger.log(Level.FINE, "PNM reader: " + pnmReader.getClass().getName() + " param: " + param);
logger.log(Level.FINE, "PNM reader: " + pnmReader.getClass().getName());
map.put("pnm", pnmReader);
}
ImageReader jpegReader = getImageReader("jpeg");
if (jpegReader != null) {
ImageReadParam param = jpegReader.getDefaultReadParam();
logger.log(Level.FINE, "JPEG reader: " + jpegReader.getClass().getName() + " param: " + param);
logger.log(Level.FINE, "JPEG reader: " + jpegReader.getClass().getName());
map.put("jpg", jpegReader);
map.put("jpeg", jpegReader);
}
ImageReader tiffReader = getImageReader("tiff");
if (tiffReader != null) {
ImageReadParam param = tiffReader.getDefaultReadParam();
logger.log(Level.FINE, "TIFF reader: " + tiffReader.getClass().getName() + " param: " + param);
logger.log(Level.FINE, "TIFF reader: " + tiffReader.getClass().getName());
map.put("tif", tiffReader);
map.put("tiff", tiffReader);
}
@ -425,7 +422,11 @@ public class PDFRasterizer implements Closeable {
ImageReader imageReader = imageReaders.get(suffix);
if (imageReader != null) {
imageReader.setInput(imageInputStream);
BufferedImage bufferedImage = imageReader.read(0);
ImageReadParam param = imageReader.getDefaultReadParam();
BufferedImage bufferedImage = imageReader.read(0, param);
logger.log(Level.FINE, "path = " + path + "loaded: width = " + bufferedImage.getWidth() +
" height = " + bufferedImage.getHeight() +
" color model = " + bufferedImage.getColorModel());
imageInputStream.close();
return bufferedImage;
} else {

View file

@ -33,14 +33,43 @@ public class PDFRasterizerTest {
pdfRasterizer.close();
}
@Test
public void testPDFColorImage() throws IOException {
String creator = "Xbib PDF";
String author = "Jörg Prante";
String subject = "Test";
Path sourceDir = Paths.get("src/test/resources/org/xbib/graphics/ghostscript/test/images");
Path targetFile = Paths.get("build/color.pdf");
PDFRasterizer pdfRasterizer = new PDFRasterizer(creator, author, subject);
int pagecount = pdfRasterizer.mergeImagesToPDF(sourceDir, targetFile);
logger.info("pagecount = " + pagecount);
pdfRasterizer.close();
}
@Test
public void testPDFUnpackRasterAndScale() throws IOException {
Path source = Paths.get("src/test/resources/org/xbib/graphics/ghostscript/test/20200024360.pdf");
Path target = Paths.get("build/20200024360-new.pdf");
Path tmp = Files.createTempDirectory("graphics-test");
try {
PDFRasterizer pdfRasterizer = new PDFRasterizer("xbib", "Jörg Prante", "Test");
pdfRasterizer.pdfToImage(source, tmp, null, null);
Path tmpTarget = tmp.resolve(target.getFileName());
int pagecount = pdfRasterizer.mergeImagesToPDF(tmp, tmpTarget);
logger.info("pagecount = " + pagecount);
pdfRasterizer.scalePDF(tmpTarget, target);
pdfRasterizer.close();
} finally {
delete(tmp);
}
}
@Test
public void testPDFRasterizerToImage() throws Exception {
Path path = Paths.get("build/resources/test");
try (Stream<Path> stream = Files.list(path)) {
stream.forEach(p -> {
if (p.toString().endsWith(".pdf")) {
logger.info("found " + p.toString());
if (p.toString().endsWith("cjk.pdf")) {
Path target = Paths.get("build/image-" + p.getFileName());
try {
delete(target);

View file

@ -1,7 +1,7 @@
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
.level=INFO
.level=ALL
java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] %5$s %6$s%n
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=build/test.log

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB