Converting PS in bytearray with Java doesn't work

Hi,

in our OS independent Java software we need to convert Postscript into BufferedImage. We’ve ordered a trail licences of Aspose.Page for Java to evaluate, if it fits in our use-case.

To keep it simple I wrote a small Junit test, that should initalize the licence and a PsDocument that could be saved as image (code below).

import static org.assertj.core.api.Assertions.*;

import java.io.FileInputStream;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

import com.aspose.eps.PsDocument;
import com.aspose.eps.device.ImageSaveOptions;
import com.aspose.eps.device.SmoothingMode;
import com.aspose.page.ImageFormat;
import com.aspose.page.License;

class AsposeTest {

	@Test
	void testAsposePage() {

		try (FileInputStream inputSteam = new FileInputStream(Paths.get("src", "test", "resources", "aspose").resolve("Aspose.PageforJava.lic").toString())) {
			new License().setLicense(inputSteam);
		} catch (Exception e) {
			System.out.print(e);
		}

		try (FileInputStream inputSteam = new FileInputStream(Paths.get("src", "test", "resources", "aspose").resolve("input.ps").toString())) {
			PsDocument document = new PsDocument(inputSteam);
			assertThat(document.isLicensed()).isTrue();
			assertThat(document.getNumberOfPages()).isEqualTo(1);

			ImageSaveOptions options = new ImageSaveOptions();
			options.setDebug(true);
			options.setImageFormat(ImageFormat.PNG);
			options.setSupressErrors(false);
			options.setSmoothingMode(SmoothingMode.HIGH_SPEED);
			byte[][] images = document.saveAsImage(options);

			assertThat(images).isNotEmpty();
		} catch (Exception e) {
			System.out.print(e);
		}
	}
}

When we run the code it stops at document.saveAsImage(). The process still unses the CPU and the amount of used RAM increases (I stoped the process at 2,8 GB).
The only feedback we get is the following line in the console:

class com.aspose.page.internal.lI.I7: writeCap() not implemented.

I run the test on the following machine:
OS: Windows 11 x64
RAM: 64 GB
Applicationtype: Junit test in eclipse 4.33.0
Java Version: 21.0.4
API Version: Aspose.Page for Java 25.1

With the following Postscript files:
aspose.zip (1,6 MB)

Where is our mistake?

Thanks in advance

Björn

@bjoernw

It seems that you are encountering an issue when trying to convert a PostScript file to an image using Aspose.Page for Java. The problem occurs specifically at the document.saveAsImage() method, which leads to high CPU usage and memory consumption without producing the expected output.

Here are a few suggestions to troubleshoot and potentially resolve the issue:

  1. Check the PostScript File: Ensure that the PostScript file you are using is valid and not corrupted. Sometimes, certain PostScript files may not be compatible with the conversion process, leading to unexpected behavior.

  2. Update Aspose.Page: Make sure you are using the latest version of Aspose.Page for Java. If you are using version 25.1, consider checking for any updates or patches that may address known issues.

  3. Debugging Options: Since you have set options.setDebug(true);, check the debug output for any additional information that might indicate what is going wrong during the conversion process.

  4. Memory Management: Given that the process consumes a significant amount of RAM, consider testing with a smaller or simpler PostScript file to see if the issue persists. This can help determine if the problem is related to the specific file or the overall memory handling.

  5. Error Handling: Instead of just printing the exception, consider logging the stack trace to get more detailed information about the error. This can provide insights into what might be causing the failure.

  6. Consult Documentation: Review the official documentation for Aspose.Page regarding PostScript to image conversion. There may be specific requirements or limitations that you need to be aware of.

  7. Community Support: If the issue persists, consider reaching out to the Aspose community or support team with details about your problem, including the specific PostScript file you are using. They may have encountered similar issues and can provide guidance.

If you continue to experience difficulties, please provide any additional error messages or logs that you receive, as this information can be helpful in diagnosing the problem further.

For more information on converting PostScript files using Aspose.Page, you can refer to the official documentation here.

Sources:
[1]: Convertir PostScript usando API para Java - Aspose Documentation
[2]: Working with Images in PS file | Java - Aspose Documentation

Hi,
based on your suggestions, we have conducted further experiments with the component. Our current code for the JUnit tests now looks like this:

void testAsposePage() {

	Path baseDir = Paths.get("src", "test", "resources", "aspose");

	try (FileInputStream inputSteam = new FileInputStream(baseDir.resolve("Aspose.PageforJava.lic").toString())) {
		new License().setLicense(inputSteam);
	} catch (Exception e) {
		System.out.print(e);
	}

	FileFilter filter = new RegexFileFilter("PostScript_.+\\.ps");
	File[] files = baseDir.toFile().listFiles(filter);

	if (files != null) {
		for (File file : files) {
			String fileName = FilenameUtils.removeExtension(file.getName());
			LocalTime begin = LocalTime.now();
			try (FileInputStream inputSteam = new FileInputStream(baseDir.resolve(fileName.concat(".ps")).toString());
					PrintStream out = new PrintStream(new FileOutputStream(baseDir.resolve(fileName.concat(".txt")).toString(), true), true)) {

				System.setOut(out);

				PsDocument document = new PsDocument(inputSteam);
				assertThat(document.isLicensed()).isTrue();
				assertThat(document.getNumberOfPages()).isEqualTo(1);

				ImageSaveOptions options = new ImageSaveOptions();
				options.setDebug(true);
				options.setImageFormat(ImageFormat.PNG);
				options.setSupressErrors(false);
				byte[][] images = document.saveAsImage(options);

				assertThat(images).isNotEmpty();

				BufferedImage image = ImageIO.read(new ByteArrayInputStream(images[0]));
				File result = baseDir.resolve(fileName.concat(".png")).toFile();
				if (image.getColorModel().hasAlpha()) {
					BufferedImage bgImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
					Graphics2D graphics = bgImage.createGraphics();
					graphics.setColor(Color.WHITE);
					graphics.fillRect(0, 0, bgImage.getWidth(), bgImage.getHeight());
					graphics.drawImage(image, 0, 0, null);
					graphics.dispose();
					ImageIO.write(bgImage, "png", result);
				} else {
					ImageIO.write(image, "png", result);
				}
				;
				System.out.println("Ends after ".concat(String.valueOf(ChronoUnit.MILLIS.between(begin, LocalTime.now())).concat(" milliseconds")));
			} catch (Throwable e) {
				System.out.print(e);
				try (PrintStream err = new PrintStream(new FileOutputStream(baseDir.resolve(fileName.concat(".txt")).toString(), true), true)) {
					e.printStackTrace(err);
				} catch (FileNotFoundException fnfe) {
					// do nothing
				}
			}
		}
	}
}

The memory available to the JVM was limited to 200 MB. The results actually vary a lot depending on the source file. I ran the test with 11 different files. With 8 files we get a valid result, but still have the following lines as output from the debug:

class com.aspose.page.internal.lI.I7: writeCap() not implemented.
%%[ ProductName: Aspose.EPSclass com.aspose.page.internal.lI.I7: writeCap() not implemented.
class com.aspose.page.internal.lI.I7: writeJoin() not implemented.
class com.aspose.page.internal.lI.I7: writeCap() not implemented.
%%[ ProductName: Aspose.EPSclass com.aspose.page.internal.lI.I7: writeCap() not implemented.
class com.aspose.page.internal.lI.I7: writeJoin() not implemented.

Two of the failing files are giving the same error. I have attached the log and a sample PostScript.
With the third failed file, the process runs into an OutOfMemoryError after about 20 minutes. Unfortunately, I only have files for this scenario, which I am not allowed to share for data protection reasons. I have attached the debug information and the stack trace anyway.

Result.zip (101,5 KB)

@bjoernw

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PAGEJAVA-303

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@bjoernw
These lines are just debugging info, but not errors. The error in PostScript_Staber_10.ps related to processing of @PJL instructions and it has been been fixed. The correction will be released in Aspose.Page for Java 25.3. As for other file (PostScript_Medatixx_Graphics.ps), I cannot fix it because it is impossible without viewing the input PS file.

Thanks for the quick reply. I’ll do my best to get a sample PostScript from our customer that prudeces the same error as PostScript_Medatixx_Graphics.ps but contains test data.

Will version 25.3 be released at the end of the month?

@bjoernw

We are working on finalizing the new release and yes it will be published during this month i.e. March 2025. Once we have more updates, we will inform you.