0

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

  • Your code works but remember _"Most PDFs which are in the wild out there do not embed the full font anyway, but only subsets. Extracting a subset of a font is only useful in a very limited scope, if at all."_ https://stackoverflow.com/questions/3488042/how-can-i-extract-embedded-fonts-from-a-pdf-as-valid-font-files in this answer command to extract without coding – josejuan Sep 09 '21 at 11:27
  • Not all formats are ttf. – Tilman Hausherr Sep 09 '21 at 13:03
  • @TilmanHausherr you are right and `PDFontDescriptor::getFontFile`, `getFontFile2` (TTF) and `getFontFile3` return currently the available font files. – josejuan Sep 09 '21 at 14:25

0 Answers0