try to use less memory while image merging
This commit is contained in:
parent
4ad07247b2
commit
a091bc52cf
8 changed files with 50 additions and 18 deletions
|
@ -14,8 +14,8 @@ test {
|
||||||
failFast = false
|
failFast = false
|
||||||
environment 'TMPDIR', '/var/tmp/gs'
|
environment 'TMPDIR', '/var/tmp/gs'
|
||||||
systemProperty 'java.awt.headless', 'true'
|
systemProperty 'java.awt.headless', 'true'
|
||||||
systemProperty 'jna.debug', 'true'
|
systemProperty 'java.io.tmpdir', '/var/tmp/'
|
||||||
//systemProperty 'jna.library.path', file('src/test/resources/').toString()
|
systemProperty 'jna.debug', 'false'
|
||||||
systemProperty 'java.util.logging.config.file', 'src/test/resources/logging.properties'
|
systemProperty 'java.util.logging.config.file', 'src/test/resources/logging.properties'
|
||||||
testLogging {
|
testLogging {
|
||||||
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
|
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
|
||||||
|
|
|
@ -24,7 +24,7 @@ import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@DisabledOnOs(OS.MAC)
|
@DisabledOnOs({OS.MAC, OS.LINUX})
|
||||||
public class PDFRendererTest {
|
public class PDFRendererTest {
|
||||||
|
|
||||||
private Locale originalDefaultLocale;
|
private Locale originalDefaultLocale;
|
||||||
|
@ -98,6 +98,7 @@ public class PDFRendererTest {
|
||||||
int height = (int) (symbol.getHeight() * magnification);
|
int height = (int) (symbol.getHeight() * magnification);
|
||||||
Rectangle rectangle = new Rectangle(0, 0, width, height);
|
Rectangle rectangle = new Rectangle(0, 0, width, height);
|
||||||
PDFGraphics2D pdfGraphics2D = new PDFGraphics2D(rectangle);
|
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));
|
pdfGraphics2D.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
|
||||||
BarcodeGraphicsRenderer barcodeGraphicsRenderer = new BarcodeGraphicsRenderer(pdfGraphics2D,
|
BarcodeGraphicsRenderer barcodeGraphicsRenderer = new BarcodeGraphicsRenderer(pdfGraphics2D,
|
||||||
rectangle, magnification, paper, ink, false, false);
|
rectangle, magnification, paper, ink, false, false);
|
||||||
|
|
|
@ -83,7 +83,8 @@ public class PDFConverter {
|
||||||
int processColorModel,
|
int processColorModel,
|
||||||
int pdfsettings,
|
int pdfsettings,
|
||||||
String compatibilityLevel,
|
String compatibilityLevel,
|
||||||
boolean pdfx, PaperSize paperSize) {
|
boolean pdfx,
|
||||||
|
PaperSize paperSize) {
|
||||||
this.autoRotatePages = autoRotatePages;
|
this.autoRotatePages = autoRotatePages;
|
||||||
this.processColorModel = processColorModel;
|
this.processColorModel = processColorModel;
|
||||||
this.pdfsettings = pdfsettings;
|
this.pdfsettings = pdfsettings;
|
||||||
|
|
|
@ -4,6 +4,7 @@ import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReadParam;
|
import javax.imageio.ImageReadParam;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
import javax.imageio.stream.ImageInputStream;
|
import javax.imageio.stream.ImageInputStream;
|
||||||
|
import org.apache.pdfbox.io.MemoryUsageSetting;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
@ -203,7 +204,7 @@ public class PDFRasterizer implements Closeable {
|
||||||
int pagecount = 0;
|
int pagecount = 0;
|
||||||
List<PDDocument> coverPageDocs = new ArrayList<>();
|
List<PDDocument> coverPageDocs = new ArrayList<>();
|
||||||
try (Stream<Path> files = Files.list(sourceDir);
|
try (Stream<Path> files = Files.list(sourceDir);
|
||||||
PDDocument pdDocument = new PDDocument();
|
PDDocument pdDocument = new PDDocument(MemoryUsageSetting.setupTempFileOnly());
|
||||||
OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(targetFile))) {
|
OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(targetFile))) {
|
||||||
List<Path> entries = files.sorted()
|
List<Path> entries = files.sorted()
|
||||||
.filter(PDFRasterizer::checkForRealFile)
|
.filter(PDFRasterizer::checkForRealFile)
|
||||||
|
@ -371,27 +372,23 @@ public class PDFRasterizer implements Closeable {
|
||||||
Map<String, ImageReader> map = new LinkedHashMap<>();
|
Map<String, ImageReader> map = new LinkedHashMap<>();
|
||||||
ImageReader pngReader = getImageReader("png");
|
ImageReader pngReader = getImageReader("png");
|
||||||
if (pngReader != null) {
|
if (pngReader != null) {
|
||||||
ImageReadParam param = pngReader.getDefaultReadParam();
|
logger.log(Level.FINE, "PNG reader: " + pngReader.getClass().getName());
|
||||||
logger.log(Level.FINE, "PNG reader: " + pngReader.getClass().getName() + " param: " + param);
|
|
||||||
map.put("png", pngReader);
|
map.put("png", pngReader);
|
||||||
}
|
}
|
||||||
ImageReader pnmReader = getImageReader("pnm");
|
ImageReader pnmReader = getImageReader("pnm");
|
||||||
if (pnmReader != null) {
|
if (pnmReader != null) {
|
||||||
ImageReadParam param = pnmReader.getDefaultReadParam();
|
logger.log(Level.FINE, "PNM reader: " + pnmReader.getClass().getName());
|
||||||
logger.log(Level.FINE, "PNM reader: " + pnmReader.getClass().getName() + " param: " + param);
|
|
||||||
map.put("pnm", pnmReader);
|
map.put("pnm", pnmReader);
|
||||||
}
|
}
|
||||||
ImageReader jpegReader = getImageReader("jpeg");
|
ImageReader jpegReader = getImageReader("jpeg");
|
||||||
if (jpegReader != null) {
|
if (jpegReader != null) {
|
||||||
ImageReadParam param = jpegReader.getDefaultReadParam();
|
logger.log(Level.FINE, "JPEG reader: " + jpegReader.getClass().getName());
|
||||||
logger.log(Level.FINE, "JPEG reader: " + jpegReader.getClass().getName() + " param: " + param);
|
|
||||||
map.put("jpg", jpegReader);
|
map.put("jpg", jpegReader);
|
||||||
map.put("jpeg", jpegReader);
|
map.put("jpeg", jpegReader);
|
||||||
}
|
}
|
||||||
ImageReader tiffReader = getImageReader("tiff");
|
ImageReader tiffReader = getImageReader("tiff");
|
||||||
if (tiffReader != null) {
|
if (tiffReader != null) {
|
||||||
ImageReadParam param = tiffReader.getDefaultReadParam();
|
logger.log(Level.FINE, "TIFF reader: " + tiffReader.getClass().getName());
|
||||||
logger.log(Level.FINE, "TIFF reader: " + tiffReader.getClass().getName() + " param: " + param);
|
|
||||||
map.put("tif", tiffReader);
|
map.put("tif", tiffReader);
|
||||||
map.put("tiff", tiffReader);
|
map.put("tiff", tiffReader);
|
||||||
}
|
}
|
||||||
|
@ -425,7 +422,11 @@ public class PDFRasterizer implements Closeable {
|
||||||
ImageReader imageReader = imageReaders.get(suffix);
|
ImageReader imageReader = imageReaders.get(suffix);
|
||||||
if (imageReader != null) {
|
if (imageReader != null) {
|
||||||
imageReader.setInput(imageInputStream);
|
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();
|
imageInputStream.close();
|
||||||
return bufferedImage;
|
return bufferedImage;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -33,14 +33,43 @@ public class PDFRasterizerTest {
|
||||||
pdfRasterizer.close();
|
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
|
@Test
|
||||||
public void testPDFRasterizerToImage() throws Exception {
|
public void testPDFRasterizerToImage() throws Exception {
|
||||||
Path path = Paths.get("build/resources/test");
|
Path path = Paths.get("build/resources/test");
|
||||||
try (Stream<Path> stream = Files.list(path)) {
|
try (Stream<Path> stream = Files.list(path)) {
|
||||||
stream.forEach(p -> {
|
stream.forEach(p -> {
|
||||||
if (p.toString().endsWith(".pdf")) {
|
if (p.toString().endsWith("cjk.pdf")) {
|
||||||
logger.info("found " + p.toString());
|
|
||||||
Path target = Paths.get("build/image-" + p.getFileName());
|
Path target = Paths.get("build/image-" + p.getFileName());
|
||||||
try {
|
try {
|
||||||
delete(target);
|
delete(target);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
|
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.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.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
|
||||||
java.util.logging.FileHandler.level=INFO
|
java.util.logging.FileHandler.level=INFO
|
||||||
java.util.logging.FileHandler.pattern=build/test.log
|
java.util.logging.FileHandler.pattern=build/test.log
|
||||||
|
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 66 KiB |
Loading…
Reference in a new issue