Skip to content

Commit

Permalink
use scratch file to convert outputstream to inputstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Warren committed Sep 13, 2018
1 parent 1b62f01 commit ac83638
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package org.springframework.content.renditions.renderers;

import internal.org.springframework.renditions.pdf.PDFServiceImpl;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.io.ScratchFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import org.springframework.content.commons.io.FileRemover;
import org.springframework.content.commons.io.ObservableInputStream;
import org.springframework.content.commons.renditions.RenditionProvider;
import org.springframework.content.renditions.RenditionException;
import org.springframework.renditions.poi.PDFService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
Expand Down Expand Up @@ -56,20 +62,19 @@ public InputStream convert(InputStream fromInputSource, String toMimeType) {
document = pdfService.load(fromInputSource);
PDFRenderer pdfRenderer = pdfService.renderer(document);
if (document.getNumberOfPages() > 0) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
new Thread(
() -> {
try {
pdfService.writeImage(bim, "jpeg", out);
} catch (IOException e) {
logger.error("Error writing buffered image to output stream");
throw new RenditionException("Error writing buffered image to output stream", e);
}
}
).start();
return in;
BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
File tmp = File.createTempFile("pdftojpegrenderer", ".tmp");
tmp.deleteOnExit();
FileOutputStream out = new FileOutputStream(tmp);
try {
pdfService.writeImage(bim, "jpeg", out);
} catch (IOException e) {
logger.error("Error writing buffered image to output stream");
throw new RenditionException("Error writing buffered image to output stream", e);
} finally {
IOUtils.closeQuietly(out);
}
return new ObservableInputStream(new FileInputStream(tmp), new FileRemover(tmp));
}
} catch (IOException e) {
logger.error("Error rendering application/pdf to image/jpeg");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.BeforeEach;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
Expand All @@ -30,6 +31,7 @@
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -93,9 +95,9 @@ public class PdfToJpegRendererTest {
It("should get the embedded thumbnail from the XWPFDocument's properties", () -> {
verify(pdfRenderer).renderImageWithDPI(0, 300, ImageType.RGB);
});
// It("should output the rendered image", () -> {
// verify(pdf).writeImage(anyObject(), eq("jpeg"), anyObject());
// });
It("should output the rendered image", () -> {
verify(pdf).writeImage(anyObject(), eq("jpeg"), isA(OutputStream.class));
});
Context("when the pdf document fails to return a thumbnail", () -> {
BeforeEach(() -> {
doThrow(IOException.class).when(pdfRenderer).renderImageWithDPI(0, 300, ImageType.RGB);
Expand Down

0 comments on commit ac83638

Please sign in to comment.