We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Blank .PNG after converting from .PDF

Attached snipped of code recreates the issue - after converting pages of a .PDF document (attached in the code), resulting .PNG appears blank. This is the only .PDF document for which we have encountered this issue, the same code works well for many other docs.

In order to run the example, please build Docker image, and then run it exposing port 4567, .PNG of the first page will be available under: http://localhost:4567/page.png

Let me know if you have any issues recreating this.

AsposeFontIssueRecreate 3.zip (8.0 MB)

@MaciejRocket

We have logged an investigation ticket as PDFJAVA-40831 in our issue tracking system for further analysis. We will look into details of it and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFJAVA-40831) have been fixed in Aspose.PDF for Java 21.9.

@asad.ali
Thanks for the info. Unfortunately, when I tried to update the version, another issue started to occur:

Caused by: class com.aspose.pdf.exceptions.PdfException: Fatal: font is not defined. The font object can’t be created from the font name: TimesNewRoman,Bold

This can be seen when running the ‘test’ script I have attached in the first post, after updating the Aspose.PDF version to 21.9

I’d appreciate any help, thanks.

@MaciejRocket

You are noticing this exception because we implemented it in 21.9 version of the API. The reason of empty output was absent font used in the current document. From the version 21.9, you will be able to see suitable exception for this case and fix the issue on your side using either proper substitution or using default font:

Exception:

class com.aspose.pdf.exceptions.PdfException: Fatal: font is not defined. The font object can't be created from the font name: TimesNewRoman,Bold

This will enable using internal system font instead of not found :

FontRepository.setReplaceNotFoundFonts(true);

Or using custom predefined font for such case:

TextDefaults.setDefaultFontStrategy(TextDefaults.DefaultFontStrategy.PredefinedFont);
TextDefaults.setPredefinedFont(FontRepository.openFont(PdfToDocxConvert.class.getClassLoader()
                .getResourceAsStream("aspose-fonts/LiberationSerif-Regular.ttf")));

But the best way will be adding SimpleFontSubstitution for the each absent font, depends on document’s design.

new SimpleFontSubstitution("Arial", "LiberationSerif"),
new SimpleFontSubstitution("TimesNewRoman", "LiberationSerif"),
new SimpleFontSubstitution("TimesNewRoman,Bold", "LiberationSerif-Bold"),

also you should remove the following line (we’ve commented), because original font could not exist so we should not make it embedded to avoid NPE:

FONT_SUBSTITUTIONS.forEach(fontSubstitution -> {
            FontRepository.getSubstitutions().add(fontSubstitution);
            FontRepository.findFont(fontSubstitution.getSubstitutionFontName()).setEmbedded(true);
//            FontRepository.findFont(fontSubstitution.getOriginalFontName()).setEmbedded(true);
        }); 

Thanks @asad.ali

I still need a bit more explanation, because what I want to achieve is:

  • I want to define some most common fonts substitutions,
  • but since I don’t have control over the document users are going to use, I don’t want it to throw ‘font is not defined’ exception, but when I set setReplaceNotFoundFonts(true), it seems like my substitutions are not being used anymore, and the whole document is being rendered using some default font.

Is there a way to render it using font substitutions, and use default font only when the substitution is not found?
Thanks in advance

@MaciejRocket

Thanks for the feedback. We have recorded your concerns under the ticket and will surely re-check the scenario from this perspective. We will let you know as soon as we have some feedback to share. Please spare us some time.