Hi,
i’m using a spring boot/maven project. This is a part of my pom.xml file
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-total</artifactId>
<version>21.10</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai_imageio</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.media</groupId>
<artifactId>jai-codec</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<repository>
<id>Jboss</id>
<name>Jboss repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
</repository>
<repository>
<id>Geotoolkit</id>
<name>Geotoolkit repository</name>
<url>http://maven.geotoolkit.org/</url>
</repository>
</repositories>
As you can see this is not the 22.5 version. I changed the version and for some reason i could not download some microsoft dependencies. So i changed the pom.xml file to this:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-total</artifactId>
<version>22.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.microsoft.onnxruntime</groupId>
<artifactId>onnxruntime</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.onnxruntime</groupId>
<artifactId>onnxruntime_gpu</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai_imageio</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.media</groupId>
<artifactId>jai-codec</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<repository>
<id>Jboss</id>
<name>Jboss repository</name>
<url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
</repository>
<repository>
<id>Geotoolkit</id>
<name>Geotoolkit repository</name>
<url>http://maven.geotoolkit.org/</url>
</repository>
</repositories>
Now the problem is even worse. The test that i run locally is still producing the tiff as i want it but when i create the jar file (mvn package) and deploy it in our VM using docker, the output tiff file is a corrupted zero byte file.
This is the Dockerfile:
FROM openjdk:11
RUN apt-get upgrade -y --fix-missing && \
apt-get update -y
EXPOSE 8080
ARG JAR_FILE=target/asposeconverter-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} asposeconverter-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-Xms6G", "-Xmx6G", "-jar", "/asposeconverter-0.0.1-SNAPSHOT.jar"]
Using spring, maven or docker as developing tools is a common practice nowadays. Why Aspose doesn’t provide a demo project of these three that is tested and should work as expected?
Here is the test that i’m running locally:
@Test
void convertPdfToTiffCODITest() throws Exception {
String inputFolderPath = Path.of(packagePath, "samplefiles", "pdf").toString();
String outputFolderPath = Path.of(packagePath, "outputfiles", "pdf").toString();
// create output folder if it doesn't exist
Files.createDirectories(Paths.get(outputFolderPath));
// get pdf name/absolute-paths
Map<String, String> filenamePathMap = Stream.of(new File(inputFolderPath).listFiles())
.filter(file -> !file.isDirectory() && file.getName().endsWith("codi.pdf"))
.collect(Collectors.toMap(File::getName, File::getAbsolutePath));
TiffSettings tiffSettings = new TiffSettings();
tiffSettings.setCompression(CompressionType.LZW);
tiffSettings.setDepth(ColorDepth.Format4bpp);
Resolution resolution = new Resolution(200);
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
for (String filename: filenamePathMap.keySet()) {
log.info("test: Converting " + filename + " to tiff");
long startTime = System.currentTimeMillis();
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(filenamePathMap.get(filename));
String convertedFilename = filename.substring(0, filename.lastIndexOf('.')) + ".tiff";
String outputFilePath = outputFolderPath + File.separator + convertedFilename;
java.io.OutputStream imageStream = new java.io.FileOutputStream(outputFilePath);
tiffDevice.process(pdfDocument, imageStream);
imageStream.close();
log.info("test: Conversion finished! Time: " + ((System.currentTimeMillis() - startTime)/1000) + " secords");
log.info("test: " + outputFilePath);
}
}
Here is the function that is running in the docker app:
public void runConverter() throws Exception {
String outputAbsolutePath = getOutputAbsolutePath();
String inputAbsolutePath = getInputAbsolutePath();
Integer[] pagesToKeep = getPagesToKeep();
String filename = FilenameUtils.getName(inputAbsolutePath);
// tiff settings
TiffSettings tiffSettings = new TiffSettings();
Resolution resolution;
if (isHighQuality()) {
tiffSettings.setCompression(CompressionType.LZW);
resolution = new Resolution(200);
} else {
tiffSettings.setCompression(CompressionType.CCITT4);
resolution = new Resolution(300);
}
tiffSettings.setDepth(ColorDepth.Format4bpp);
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
log.info("Converting " + filename + " to tiff");
long startTime = System.currentTimeMillis();
Document pdfDocument = new Document(inputAbsolutePath);
Document tempPdfDocument = null;
// check if a page number exceeds available pages
if (pagesToKeep != null && pagesToKeep.length > 0 && Arrays.stream(pagesToKeep).max(Integer::compare).get() > pdfDocument.getPages().size())
throw new InvalidRequestException("Invalid page number was given");
// Since a Java heap memory error is possible, we use a temporary filename and if the conversion
// is finished without the error, we then rename the file to the original requested name
String tempFilename = "corrupted-" + UUID.randomUUID() + ".tiff";
Path tempOutputAbsolutePath = Path.of(FilenameUtils.getPath(outputAbsolutePath), tempFilename);
try (OutputStream imageStream = new FileOutputStream(tempOutputAbsolutePath.toString())) {
if (pagesToKeep == null || pagesToKeep.length == 0) {
tiffDevice.process(pdfDocument, imageStream);
} else {
tempPdfDocument = new Document();
for (int i: pagesToKeep)
tempPdfDocument.getPages().add(pdfDocument.getPages().get_Item(i));
tiffDevice.process(tempPdfDocument, imageStream);
}
} finally {
pdfDocument.close();
if (tempPdfDocument != null)
tempPdfDocument.close();
}
Files.move(tempOutputAbsolutePath, tempOutputAbsolutePath.resolveSibling(FilenameUtils.getName(outputAbsolutePath)), StandardCopyOption.REPLACE_EXISTING);
log.info("Conversion of " + filename + " to tiff finished in " +
((System.currentTimeMillis() - startTime) / 1000) + " seconds");
}
Of course i expect the same result if isHighQuality() returns true.