PdfException during Xml to Pdf conversion

Hello,

I am trying to convert Xml to Pdf using below aspose code but encountering an exception.
Code I am using:

try (final com.aspose.pdf.Document document = new com.aspose.pdf.Document();) {
            final com.aspose.pdf.PdfSaveOptions pdfSaveOptions = new com.aspose.pdf.PdfSaveOptions();
            final String xmlContent = Files.readString(Path.of(sourceFilePath), StandardCharsets.UTF_8);
            // -- Add a page to the PDF
            try (final Page page = document.getPages().add();) {
                // -- Create a TextFragment with the whole XML content
                final TextFragment textFragment = new TextFragment(xmlContent);
                // -- Set font to a monospaced font to mimic browser XML view

                textFragment.getTextState().setFontSize(10);
                // -- Add the text fragment to the page's paragraphs collection
                page.getParagraphs().add(textFragment);

                // -- Set small margins to fit content better
                final MarginInfo marginInfo = new MarginInfo();
                marginInfo.setTop(50);
                marginInfo.setBottom(50);
                marginInfo.setLeft(50);
                marginInfo.setRight(50);
                page.setPageInfo(new PageInfo());
                page.getPageInfo().setMargin(marginInfo);

                if (targetType == TargetDocFormats.PDF) {
                    if (-1 != pdfStandardLvl) {
                        ((IPdfOptions) pdfSaveOptions).setCompliance(pdfStandardLvl);
                    }
                    // -- Save the PDF file
                    document.save(outputFilePath, pdfSaveOptions);
                    conStatus = ConversionStatus.OK;
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("XML Conversion - preview save done");
                    }
                } else if (targetType == TargetDocFormats.PNG) {
                    document.save(outputFilePath, pdfSaveOptions);
                    pdfToPngConversion(outputFilePath, targetType, outputFilePath, thumbnailType);
                    conStatus = ConversionStatus.OK;
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("XPS Conversion - thumbnail save done");
                    }
                }
            }
        } catch (final Exception e) {
            LOGGER.error("XML  Conversion Failed. Exception -- ", e);
        }

Exception:
com.aspose.pdf.exceptions.PdfException: Can’t find any suitable font for operate with text: 是
at com.aspose.pdf.internal.l5f.lv.lI(Unknown Source)
at com.aspose.pdf.TextSegment.setText(Unknown Source)
at com.aspose.pdf.TextSegment.lI(Unknown Source)
at com.aspose.pdf.TextBuilder.lI(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.l5l.lI(Unknown Source)
at com.aspose.pdf.l5l.lf(Unknown Source)
at com.aspose.pdf.l5l.lh(Unknown Source)
at com.aspose.pdf.Page.lf(Unknown Source)
at com.aspose.pdf.Page.lf(Unknown Source)
at com.aspose.pdf.Page.l0v(Unknown Source)
at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
at com.aspose.pdf.Document.processParagraphs(Unknown Source)
at com.aspose.pdf.ADocument.lI(Unknown Source)
at com.aspose.pdf.ADocument.save(Unknown Source)
at com.aspose.pdf.Document.save(Unknown Source)

The conversion is successful without any exception for when docx with same content is converted to Pdf.

Please suggest a way to overcome this exception.

Thanks

@aakanksha76

The exception you’re encountering during the XML to PDF conversion indicates that the Aspose library cannot find a suitable font for the text being processed. This often occurs when the text includes characters that require specific fonts that are not available in the environment.

To resolve this issue, you can try the following approaches:

  1. Set a Default Font: Ensure that you set a default font that supports the characters in your XML content. You can do this by modifying the TextState of your TextFragment to use a font that includes the necessary glyphs.

    Example:

    textFragment.getTextState().setFont(FontRepository.findFont("Arial Unicode MS")); // Use a font that supports a wide range of characters
    
  2. Use HTML Conversion: If the XML content can be transformed into HTML, consider converting the XML to HTML first and then loading that HTML into the PDF document. This can help bypass font issues since HTML can specify fonts more flexibly.

    Example:

    HtmlLoadOptions options = new HtmlLoadOptions();
    Document pdfDocument = new Document("path/to/your/html/file.html", options);
    pdfDocument.save("output.pdf");
    
  3. Check for Missing Fonts: Ensure that the font you are trying to use is installed on the system where the conversion is taking place. If it’s a custom font, you may need to embed it or install it.

  4. Use XSLT Transformation: If your XML structure allows, consider using XSLT to transform the XML into a format that can be more easily converted to PDF, potentially avoiding font issues.

If these suggestions do not resolve the issue, please provide more details about the XML content and the specific characters causing the problem, and I can assist you further.

But, the conversion is successful without any exception for when docx with same content is converted to Pdf.

@aakanksha76 Could you please provide us with a sample data file so we can investigate the issue further?

Hello,
here is the sample file:
samplexml.zip (393 Bytes)

For docx conversion we have set the font settings as follows:

private static com.aspose.words.FontSettings wordsFontSettings;
wordsFontSettings = new com.aspose.words.FontSettings();

wordsFontSettings.getSubstitutionSettings().getTableSubstitution().setSubstitutes( unavailableFont, substituteFont);
doc.setFontSettings(wordsFontSettings);

How can we achieve same for XML file?

Hi, @aakanksha76! I’m sorry for delay.
If I understood you correctly, you want to substitute an unavailable font with another one. In this case, please take a look at the following example. The commented part of the code is an additional example of how to check if a font is present or not.

package com.aspose.pdf.examples;

import com.aspose.pdf.*;
import com.aspose.pdf.exceptions.FontNotFoundException;
import com.aspose.pdf.text.SimpleFontSubstitution;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        String sourceFilePath = "E:\\TestData\\SampleXml.xml";
        String outputFilePath = "E:\\TestData\\output.pdf";

        try (Document document = new Document()) {
            final String xmlContent;
            try {
                xmlContent = Files.readString(Path.of(sourceFilePath), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            Page page = document.getPages().add();
            PageInfo pageInfo = new PageInfo();
            pageInfo.setWidth(595);  // A4 width in points
            pageInfo.setHeight(842); // A4 height in points
            pageInfo.setMargin(new MarginInfo(50, 50, 50, 50)); // Set margins
            page.setPageInfo(new PageInfo());

//            try
//            {
//                // Check whether a font, used in the source document, is installed in the system
//                FontRepository.findFont("AgencyFB");
//            }
//            catch (FontNotFoundException ex)
//            {
//                SimpleFontSubstitution fontSubstitution = new SimpleFontSubstitution("AgencyFB", "Arial");
//                FontRepository.getSubstitutions().add(fontSubstitution);
//            }

            SimpleFontSubstitution fontSubstitution = new SimpleFontSubstitution("SimSun", "NSimSun");
            FontRepository.getSubstitutions().add(fontSubstitution);

            // -- Create a TextFragment with the whole XML content
            TextFragment textFragment = new TextFragment(xmlContent);
            // -- Set font to a monospaced font to mimic browser XML view
            textFragment.getTextState().setFontSize(10);

            // -- Add the text fragment to the page's paragraphs collection
            page.getParagraphs().add(textFragment);

            document.convert("Conversion_log.xml", PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);

            document.save(outputFilePath);
        }
    }
}

@andriy.andrukhovski Thank you for your response,
It is working for pdf!

I also want to save it as PNG. How can document be saved as PNG here?

Please, try this code:

    private static void Example02(TargetPdfFormat targetPdfFormat) {
        final String sourceFilePath = "E:\\TestData\\SampleXml.xml";
        final String outputFilePath = "E:\\TestData\\output.pdf";
        final String output_PNG = "E:\\TestData\\output.png";

        try (Document document = new Document()) {
            final String xmlContent;
            try {
                xmlContent = Files.readString(Path.of(sourceFilePath), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            Page page = document.getPages().add();
            PageInfo pageInfo = new PageInfo();
            pageInfo.setWidth(595);  // A4 width in points
            pageInfo.setHeight(842); // A4 height in points
            pageInfo.setMargin(new MarginInfo(50, 50, 50, 50)); // Set margins
            page.setPageInfo(new PageInfo());

            SimpleFontSubstitution fontSubstitution =
                    new SimpleFontSubstitution("SimSun", "NSimSun");
            FontRepository.getSubstitutions().add(fontSubstitution);

            // -- Create a TextFragment with the whole XML content
            TextFragment textFragment = new TextFragment(xmlContent);
            // -- Set font to a monospaced font to mimic browser XML view
            textFragment.getTextState().setFontSize(10);

            // -- Add the text fragment to the page's paragraphs collection
            page.getParagraphs().add(textFragment);
            if (targetPdfFormat == TargetPdfFormat.PNG) {
                // Save the document as PNG
                document.processParagraphs();
                Resolution resolution = new Resolution(300);
                PngDevice pngDevice = new PngDevice(resolution);
                pngDevice.process(page, output_PNG);
            } else {
                document.save(outputFilePath);
            }

        }
    }

@andriy.andrukhovski
Thank you for your respose.

There was a update on pdf generation solution, after testing the solution windows it is working but on linux I get the same exception:

com.aspose.pdf.exceptions.PdfException: Can’t find any suitable font for operate with text: 汉
at com.aspose.pdf.internal.l5f.lv.lI(Unknown Source)
at com.aspose.pdf.TextSegment.setText(Unknown Source)
at com.aspose.pdf.TextSegment.lI(Unknown Source)
at com.aspose.pdf.TextBuilder.lI(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
at com.aspose.pdf.l5l.lI(Unknown Source)
at com.aspose.pdf.l5l.lf(Unknown Source)
at com.aspose.pdf.l5l.lh(Unknown Source)
at com.aspose.pdf.Page.lf(Unknown Source)
at com.aspose.pdf.Page.lf(Unknown Source)
at com.aspose.pdf.Page.l0v(Unknown Source)
at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
at com.aspose.pdf.Document.processParagraphs(Unknown Source)
at com.aspose.pdf.ADocument.lI(Unknown Source)
at com.aspose.pdf.ADocument.save(Unknown Source)
at com.aspose.pdf.Document.save(Unknown Source)

Caused by: com.aspose.pdf.internal.ms.System.l7k: Format of font “Helvetica” is not supported for new composite fonts
at com.aspose.pdf.internal.l5h.lj.lf(Unknown Source)
at com.aspose.pdf.internal.l5h.lj.(Unknown Source)
at com.aspose.pdf.internal.l7v.lf.lI(Unknown Source)
at com.aspose.pdf.internal.l5h.l1j.lI(Unknown Source)
at com.aspose.pdf.internal.l5y.le.lI(Unknown Source)
at com.aspose.pdf.internal.l5n.lI.lI(Unknown Source)

Thank you for providing the details. I’ll investigate the issue on Linux as well. Since this may require some time, I’ll follow up with an update by tomorrow.

@aakanksha76 Thank you for your patience!
This exception occurs because there are no fonts with Chinese character support installed on the Linux system.
You need to add the appropriate fonts manually. For testing, I used the downloaded font Noto Serif CJK JP ExtraLight.
I made slight changes to the code:

FontRepository.getSources().add(new FolderFontSource("/home/aspose/samples/fonts")); 
SimpleFontSubstitution fontSubstitution = 
    new SimpleFontSubstitution("Helvetica", "Noto Serif CJK JP ExtraLight");
FontRepository.getSubstitutions().add(fontSubstitution);

These changes mean that the downloaded font will be loaded from a custom folder.