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

Free Support Forum - aspose.com

Convert html to pdf using ASPOSE.PDF but emoji can’t display

hi,
I try to convert html to pdf using aspose.pdf, but emoji not display in pdf. Does it support emoji? Or what can I do to display emoji successfulyy after convert?

here is my code:

HtmlLoadOptions options = new HtmlLoadOptions();
Document document = new Document("C:\\Users\\xx\\Desktop\\1.html", options);
document.save("C:\\Users\\xx\\Desktop\\1.pdf");

this is my html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <body>
    <p>😅(✿◡‿◡)(✿◡‿◡)test</p>
  </body>
</html>

pdf result:
image.png (18.5 KB)

Looking forward to receiving your reply. Many thanks.

@lucy.hq

I have been able to reproduce the issue on our end. A ticket with ID PDFJAVA-40745 has been created in our issue tracking system to further investigate the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

Thank you for replying so quickly. Looking forward to your solution

Do we have any update on this ticket?

@lucy.hq

Please note that it was logged in free support model and will be investigated and resolved on a first come first serve basis. We will let you know once we make some definite progress. Please spare us some time.

Is there any other way to display emoji successfully after converting html to pdf? Do you have any suggestion?

@lucy.hq

We request for your patience and will share feedback with you as soon as the issue will be investigated.

@lucy.hq

We would like to share with you that we have investigated the PDFJAVA-40745 completely. The basic answer is that the emojis are done using OpenType SVG fonts. Such fonts provide font definitions both in SVG format (basically SVG vector graphics) as well as in standard CFF or TrueType format for those applications that don’t support SVG format fonts.

At this point, the PDF specification, including the relatively new ISO 32000 PDF 2.0 specification does not support the SVG format in fonts.

We can suggest a workaround that will change emoji to some visible graphic symbol in UTF-8 in HTML. Then, after conversion to PDF, we could find this symbol and draw an SVG image above it. Please see the code snippet below.

    HtmlLoadOptions options = new HtmlLoadOptions();
    String HTML = FileUtils.readFileToString(new File("1.html"), "UTF-8");
    HTML = HTML.replace("😅", "☻");
    FileUtils.writeStringToFile(new File("2.html"), HTML, "UTF-8");

    Document document = new Document("2.html", options);

    TextFragmentAbsorber absorber = new TextFragmentAbsorber("☻");
    document.getPages().accept(absorber);
    Page page = document.getPages().get_Item(1);
    for (TextFragment textFragment : absorber.getTextFragments()) {
        java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File("smile.png"));

        // Add an image to the Images collection of the page resources
        page.getResources().getImages().add(imageStream);

        // Using the GSave operator: this operator saves current graphics state
        page.getContents().add(new GSave());

        // Create Rectangle and Matrix objects
        Rectangle rectangle = new Rectangle(textFragment.getRectangle().getLLX(),
        textFragment.getRectangle().getLLY(),
        textFragment.getRectangle().getURX(),
        textFragment.getRectangle().getURY());
        Matrix matrix = new Matrix(new double[]{rectangle.getURX() - rectangle.getLLX(), 0, 0,
        rectangle.getURY() - rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY()});

        // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must
        // be placed
        page.getContents().add(new ConcatenateMatrix(matrix));
        XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());

        // Using Do operator: this operator draws image
        page.getContents().add(new Do(ximage.getName()));

        // Using GRestore operator: this operator restores graphics state
        page.getContents().add(new GRestore());
    }

    document.save("out.pdf"); 

1.zip (265 Bytes)
smile.jpg (74.1 KB)