I try to convert PDFont into a font file in ttf format, but the font file obtained is not a valid font file. Below is my code.
public byte[] writeFont(PDFont font) {
byte[] fontBytes = null;
try {
InputStream is = null;
if (font instanceof PDTrueTypeFont) {
PDTrueTypeFont f = (PDTrueTypeFont) font;
is = f.getTrueTypeFont().getOriginalData();
} else if (font instanceof PDType0Font) {
PDType0Font type0Font = (PDType0Font) font;
if (type0Font.getDescendantFont() instanceof PDCIDFontType2) {
PDCIDFontType2 ff = (PDCIDFontType2) type0Font
.getDescendantFont();
is = ff.getTrueTypeFont().getOriginalData();
} else if (type0Font
.getDescendantFont() instanceof PDCIDFontType0) {
// a Type0 CIDFont contains CFF font
PDCIDFontType0 cidType0Font = (PDCIDFontType0) type0Font
.getDescendantFont();
PDFontDescriptor s = cidType0Font.getFontDescriptor();
PDStream s1 = s.getFontFile();
if (s1 != null) {
is = s1.createInputStream();
}
PDStream s2 = s.getFontFile2();
if (s2 != null) {
is = s2.createInputStream();
}
PDStream s3 = s.getFontFile3();
if (s3 != null) {
is = s3.createInputStream();
}
} else {
PDType0Font f = (PDType0Font) font;
PDFontDescriptor s = f.getFontDescriptor();
PDStream s1 = s.getFontFile();
if (s1 != null) {
is = s1.createInputStream();
}
PDStream s2 = s.getFontFile2();
if (s2 != null) {
is = s2.createInputStream();
}
PDStream s3 = s.getFontFile3();
if (s3 != null) {
is = s3.createInputStream();
}
}
} else if (font instanceof PDType1Font) {
PDType1Font f = (PDType1Font) font;
PDFontDescriptor s = f.getFontDescriptor();
PDStream s1 = s.getFontFile();
if (s1 != null) {
is = s1.createInputStream();
}
PDStream s2 = s.getFontFile2();
if (s2 != null) {
is = s2.createInputStream();
}
PDStream s3 = s.getFontFile3();
if (s3 != null) {
is = s3.createInputStream();
}
} else if (font instanceof PDType1CFont) {
PDType1CFont f = (PDType1CFont) font;
PDFontDescriptor s = f.getFontDescriptor();
PDStream s1 = s.getFontFile();
if (s1 != null) {
is = s1.createInputStream();
}
PDStream s2 = s.getFontFile2();
if (s2 != null) {
is = s2.createInputStream();
}
PDStream s3 = s.getFontFile3();
if (s3 != null) {
is = s3.createInputStream();
}
} else if (font instanceof PDType3Font) {
PDType3Font f = (PDType3Font) font;
PDFontDescriptor s = f.getFontDescriptor();
PDStream s1 = s.getFontFile();
if (s1 != null) {
is = s1.createInputStream();
}
PDStream s2 = s.getFontFile2();
if (s2 != null) {
is = s2.createInputStream();
}
PDStream s3 = s.getFontFile3();
if (s3 != null) {
is = s3.createInputStream();
}
}
if (is != null) {
fontBytes = IOUtils.toByteArray(is);
} else {
//logger.error("error:" + font.getClass());
}
} catch (Exception e) {
//logger.error("error:" + font.getName(), e);
}
return fontBytes;
}
Next step convert the byte data to font files.
I can open it with FontForge . But I have a question. Why can't I directly click on this font subset file with the mouse, there will be an invalid font error? But the font subset file I got using sfnttool.jar
can be opened directly by clicking