@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)