I’m trying to setup Aspose.PSD to run in nodejs using the java-node wrapper and through various trial and error I’ve been able to get most things to process up until I try to save a file.
Sometimes I get the following error and sometimes I get the error after it:
Error: Error running instance method
class com.aspose.psd.coreexceptions.ImageSaveException: Image saving failed. —> class com.aspose.psd.internal.Exceptions.ArgumentNullException: Path cannot be null.
Parameter name: path
Other error:
class com.aspose.psd.coreexceptions.ImageLoadException: Image saving failed. —> java.lang.IllegalAccessError: class com.aspose.psd.internal.jw.q (in unnamed module @0x2e647e59) cannot access class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl (in module java.base) because module java.base does not export sun.reflect.generics.reflectiveObjects to unnamed module @0x2e647e59
Both seem to happen when saving to file or passing a file stream to one of the save functions.
Running Aspose.PSD in nodejs would save me a lot of time and errors but I’m stuck on these issues.
Here’s my test code:
async test(psdPath: string, outputPath: string, layerIndex: integer, applyEffects: boolean = true): Promise<RenditionResponse> {
var java = require("java");
var loadOptions = null;
psdPath = "path/to/test.psd";
try {
// library
java.classpath.push("aspose-psd-23.11-jdk16.jar");
// path constants
const psdImagePath = "com.aspose.psd.fileformats.psd.PsdImage";
// class paths
// i don't know if we need class paths if we have imports
java.classpath.push("com.aspose.psd.License");
java.classpath.push("com.aspose.psd.Image");
java.classpath.push("com.aspose.psd.fileformats.psd.PsdImage");
java.classpath.push("com.aspose.psd.imageloadoptions.PsdLoadOptions");
java.classpath.push("com.aspose.psd.imageoptions.PngOptions");
// imports
const License = java.import("com.aspose.psd.License");
const PsdLoadOptions = java.import("com.aspose.psd.imageloadoptions.PsdLoadOptions");
const Image = java.import("com.aspose.psd.Image");
//const PsdImage = java.import("com.aspose.psd.fileformats.psd.PsdImage");
const PngOptions = java.import("com.aspose.psd.imageoptions.PngOptions");
const PngColorType = java.import("com.aspose.psd.fileformats.png.PngColorType");
var licensePath = "Aspose.PSD.Java.lic";
var licensePath2 = path.join(__dirname, licensePath);
// get license
try {
var licenseStream = fs.createReadStream(licensePath);
if (licenseStream == null) {
console.log("license not found");
}
}
catch (error) {
console.log("Missing license resource");
}
// apply license
try {
var license = new License();
var licenseResult = license.setLicenseSync(licensePath);
}
catch (error) {
console.log(error);
console.log("Invalid license");
}
// create new load options
var loadOptions = new PsdLoadOptions();
if (applyEffects) {
loadOptions.setLoadEffectsResource(true);
}
// load psd
var result = Image.loadSync(psdPath, loadOptions);
var psdImage = this.loadPSD(psdPath, true);
// not sure if or why we need this
var newPsdImage = java.newInstanceSync(psdImagePath, psdImage);
// remove non layers like groups and section dividers
var allLayers: [] = psdImage.getLayersSync();
var normalizedLayers = normalizeLayers(allLayers);
var layerIndexOffset = 1;
var selectedIndex = layerIndex - layerIndexOffset;
var layer = normalizedLayers[selectedIndex];
if (layer) {
console.log("exporting " + layer.getNameSync())
}
else {
console.log("Layer not found")
}
var pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
var filename = "zoutput.png";
var filePath = path.join(__dirname, "public", filename);
var outputFileStream = fs.createWriteStream(filePath);
// if we output without effects
// we can use layer.save()
if (applyEffects == false) {
try {
layer.saveSync(outputFileStream, pngOptions);
}
catch (error) {
console.log(error);
}
}
else {
// to output with effects
// we need to hide the visible layers
// then save the psd
//hideAllLayers(allLayers, selectedIndex);
try {
result.saveSync(filename, pngOptions);
} catch (error) {
console.log("test 1 failed")
}
try {
psdImage.saveSync(filename, pngOptions);
} catch (error) {
console.log("test 2 failed")
}
try {
result.saveSync(outputFileStream, pngOptions);
} catch (error) {
console.log("test 3 failed")
}
try {
psdImage.saveSync(outputFileStream, pngOptions);
} catch (error) {
console.log("test 4 failed")
}
var outputFileStream = fs.createWriteStream(filePath);
try {
result.saveSync(outputFileStream, pngOptions);
} catch (error) {
console.log("test 5 failed")
}
try {
psdImage.saveSync(outputFileStream, pngOptions);
} catch (error) {
console.log("test 6 failed")
}
try {
newPsdImage.saveSync(filename, pngOptions);
} catch (error) {
console.log("test 7 failed")
}
try {
newPsdImage.saveSync(filename, pngOptions);
} catch (error) {
console.log("test 8 failed")
}
}
}
catch(error) {
console.log(error);
}
}
await test(null, null, 1);
Sometimes a file is created but it is 0 bytes.