Skip to content

Commit

Permalink
Fix tiff file conversion (Aspose) (#2364)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisihab committed Oct 21, 2021
1 parent c61d576 commit 802549a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
Expand Up @@ -18,6 +18,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -27,8 +28,9 @@
import org.apache.tika.mime.MediaType;

import com.aspose.imaging.extensions.ImageExtensions;
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.imaging.imageoptions.TiffOptions;
import com.aspose.imaging.fileformats.tiff.TiffFrame;
import com.aspose.imaging.fileformats.tiff.TiffImage;
import com.aspose.imaging.imageoptions.PngOptions;
import com.aspose.pdf.Document;
import com.aspose.pdf.Image;
import com.aspose.pdf.LoadOptions;
Expand Down Expand Up @@ -85,7 +87,6 @@ public void convert(MediaType mediaType, Message message, CisConversionResult re
File tmpImageFile = null;
com.aspose.imaging.Image image = null;
Document doc = new Document();
float scaleFactor = 0;
try {
// Set borders on 0.5cm.
float marginInCm = 0.0f;
Expand All @@ -100,10 +101,15 @@ public void convert(MediaType mediaType, Message message, CisConversionResult re
tmpImageFile = UniqueFileGenerator.getUniqueFile(getPdfOutputlocation(), this.getClass().getSimpleName(), mediaType.getSubtype());
image = com.aspose.imaging.Image.load(message.asInputStream());
if(mediaType.getSubtype().equalsIgnoreCase(TIFF)) {
try(TiffOptions options = new TiffOptions(TiffExpectedFormat.TiffJpegRgb)){
image.save(tmpImageFile.getAbsolutePath(), options);
TiffFrame[] frames = ((TiffImage)image).getFrames();
PngOptions pngOptions = new PngOptions();
for(int i=0; i<frames.length;i++) {
Image pdfImage = new Image();
frames[i].save(tmpImageFile.getAbsolutePath()+i, pngOptions);
pdfImage.setFile(tmpImageFile.getAbsolutePath()+i);
page.getParagraphs().add(pdfImage);
}
}else {
} else {
Files.copy(message.asInputStream(), tmpImageFile.toPath());
BufferedImage bufferedImage = ImageExtensions.toJava(image);
LOGGER.debug("Image info height:" + bufferedImage.getHeight() + " width:" + bufferedImage.getWidth());
Expand All @@ -115,21 +121,20 @@ public void convert(MediaType mediaType, Message message, CisConversionResult re
float scaleHeight = maxImageHeightInPoints / bufferedImage.getHeight();

// Get the smallest scale factor so it will fit on the paper.
scaleFactor = Math.min(scaleWidth, scaleHeight);
float scaleFactor = Math.min(scaleWidth, scaleHeight);
if (scaleFactor > NO_SCALE_FACTOR) {
scaleFactor = NO_SCALE_FACTOR;
}
Image pdfImage = new Image();
pdfImage.setFile(tmpImageFile.getAbsolutePath());

// do not set scale if the image type is tiff
if (!mediaType.getSubtype().equalsIgnoreCase(TIFF)) {
pdfImage.setImageScale(scaleFactor);
}
page.getParagraphs().add(pdfImage);
}

Image pdfImage = new Image();
pdfImage.setFile(tmpImageFile.getAbsolutePath());

// do not set scale if the image type is tiff
if (!mediaType.getSubtype().equalsIgnoreCase(TIFF)) {
pdfImage.setImageScale(scaleFactor);
}

page.getParagraphs().add(pdfImage);
long startTime = new Date().getTime();
doc.save(result.getPdfResultFile().getAbsolutePath(), SaveFormat.Pdf);
long endTime = new Date().getTime();
Expand All @@ -141,14 +146,23 @@ public void convert(MediaType mediaType, Message message, CisConversionResult re
doc.dispose();
doc.close();

// Delete always the temporary file.

if(mediaType.getSubtype().equalsIgnoreCase(TIFF)) {
int length = ((TiffImage)image).getFrames().length;
for(int i=0; i<length; i++) {
Files.delete(Paths.get(tmpImageFile.getAbsolutePath()+i));
}
}

if (image != null) {
image.close();
image = null;
}
// Delete always the temporary file.
if (tmpImageFile != null) {
Files.delete(tmpImageFile.toPath());
}

if (tmpImageFile != null) {
Files.deleteIfExists(tmpImageFile.toPath());
}
}
}

Expand Down
Binary file modified aspose/src/test/resources/PdfPipe/results/tiff.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion aspose/src/test/resources/PdfPipe/xml-results/tiff.xml
@@ -1 +1 @@
<main conversionOption="0" mediaType="image/tiff" documentName="default_filename" numberOfPages="1" convertedDocument="IGNORE"/>
<main conversionOption="0" mediaType="image/tiff" documentName="default_filename" numberOfPages="5" convertedDocument="IGNORE"/>

0 comments on commit 802549a

Please sign in to comment.