public static ByteArrayOutputStream convertToPDF(InputStream inputStream, int pdfMaxDimension) {
LoadOptions lop = new LoadOptions();
lop.setDataRecoveryMode(DataRecoveryMode.ConsistentRecover);
lop.setBufferSizeHint(50);
PdfOptions exportOptions = new PdfOptions();
PdfCoreOptions pdfCoreOptions = new PdfCoreOptions();
pdfCoreOptions.setCompression(CompressionLevel.SUPER_FAST);
pdfCoreOptions.setJpegQuality(100);
exportOptions.setPdfCoreOptions(pdfCoreOptions);
exportOptions.setBufferSizeHint(50);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.save(baos, exportOptions);
baos.flush();
return baos;
} catch (Exception e) {
//log error
}
return null;
}
Using this method, when setting the buffer size hint, temp files are created in
c:\users{user}\AppData\Local\Temp\3
These files have guid names, and are never cleared up leading to an out of disk space situation on primary drive.
Changing java tmp directory does not resolve. Is there a way to control what directory these files are created in, so that we can manage them more easily?
Thanks!
Welcome to our forum, @TealWren!
Thank you for using Aspose.Imaging.
Please, let me clarify some points
- Using setBufferSizeHint configures the library to use less memory and more temporary files.
- It is highly required to close unnecessary images using
try(Image img = Image.load(...))
construction or by calling close()
directly. Then the temporary files will be deleted!
- You can change directory for the temporary files by changing
System.setProperty("java.io.tmpdir", "some path")
or by setting it in a command line java -Djava.io.tmpdir="some path" application_launcher_class
Here is the correct version of you code:
public static ByteArrayOutputStream convertToPDF(InputStream inputStream, int pdfMaxDimension) {
LoadOptions lop = new LoadOptions();
lop.setDataRecoveryMode(DataRecoveryMode.ConsistentRecover);
lop.setBufferSizeHint(50);
try (Image image = Image.load(inputStream, lop)) {
PdfOptions exportOptions = new PdfOptions();
PdfCoreOptions pdfCoreOptions = new PdfCoreOptions();
pdfCoreOptions.setCompression(CompressionLevel.SUPER_FAST);
pdfCoreOptions.setJpegQuality(100);
exportOptions.setPdfCoreOptions(pdfCoreOptions);
exportOptions.setBufferSizeHint(50);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.save(baos, exportOptions);
return baos;
}
catch (Exception e) {
//log error
}
return null;
}