Skip to content

Instantly share code, notes, and snippets.

@talios
Created November 8, 2009 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save talios/643173745182c9becc57 to your computer and use it in GitHub Desktop.
Save talios/643173745182c9becc57 to your computer and use it in GitHub Desktop.
package smx3.pdf;
import java.awt.Font;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Set;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.FontFactory;
import org.xhtmlrenderer.resource.FSEntityResolver;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class FontTest {
public static void main(String[] args) throws Exception {
boolean printITextFonts = false;
boolean printXHtmlFonts = false;
boolean printXHtmlDocument = true;
FileOutputStream os = new FileOutputStream("/tmp/test.pdf");
ITextRenderer renderer = new ITextRenderer();
ArrayList<Font> awtFonts = new ArrayList<Font>();
ITextFontResolver resolver = renderer.getFontResolver();
addFonts(printXHtmlFonts, awtFonts, resolver, "/usr/share/fonts/truetype");
addFonts(printXHtmlFonts, awtFonts, resolver, "/usr/share/fonts/truetype/thai");
addFonts(printXHtmlFonts, awtFonts, resolver, "/usr/share/fonts/truetype/freefont");
addFonts(printXHtmlFonts, awtFonts, resolver, "/usr/share/fonts/truetype/sazanami");
addFonts(printXHtmlFonts, awtFonts, resolver, "/usr/share/fonts/truetype/kochi");
ArrayList<String> fontFamilies = new ArrayList<String>();
for (Font awtFont : awtFonts) {
if (!fontFamilies.contains(awtFont.getFamily())) {
fontFamilies.add(awtFont.getFamily());
}
}
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append(newLine);
sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">");
sb.append(newLine);
sb.append("<head>");
sb.append(newLine);
sb.append("<style type=\"text/css\">");
sb.append(newLine);
for (int i = 0; i < fontFamilies.size(); i++) {
sb.append("*.font_" + i + "{font-family:\"" + fontFamilies.get(i) + "\"}");
sb.append(newLine);
}
sb.append("</style>");
sb.append(newLine);
sb.append("</head>");
sb.append(newLine);
sb.append("<body>");
sb.append(newLine);
for (int i = 0; i < fontFamilies.size(); i++) {
sb.append("<p>");
sb.append(newLine);
sb.append("<span>");
sb.append(fontFamilies.get(i));
sb.append(" - </span>");
sb.append("<span class=\"font_" + i + "\">同名の映画のモデ ");
sb.append(fontFamilies.get(i));
sb.append("</span>");
sb.append(newLine);
sb.append("</p>");
sb.append(newLine);
}
sb.append("</body>");
sb.append(newLine);
sb.append("</html>");
sb.append(newLine);
String doc = sb.toString();
if (printXHtmlDocument) {
System.out.println(doc);
}
final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
builder.setEntityResolver(FSEntityResolver.instance());
org.w3c.dom.Document document = builder.parse(new ByteArrayInputStream(doc.toString().getBytes()));
renderer.setDocument(document, null);
renderer.layout();
renderer.createPDF(os);
if (printITextFonts) {
Set<?> rf = FontFactory.getRegisteredFonts();
for (Object f : rf) {
System.out.println(f);
}
}
}
private static void addFonts(boolean printXHtmlFonts, ArrayList<Font> awtFonts, ITextFontResolver resolver, String fontPath) {
File[] fontFiles = new File(fontPath).listFiles();
for (File fontFile : fontFiles) {
if (fontFile.isFile() && fontFile.getName().toLowerCase().endsWith(".ttf")) {
try {
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
awtFonts.add(awtFont);
resolver.addFont(fontFile.toString(), true);
if (printXHtmlFonts) {
System.out.println("OK: " + fontFile.getName() + " - " + awtFont.getFamily() + ":" + awtFont.getName());
}
} catch (Exception ex) {
System.out.println("ERROR: " + fontFile.getName() + " - " + ex.getMessage());
}
}
}
}
}
@uleming
Copy link

uleming commented May 17, 2012

as expected this will work only with latin symbols but not cyrilic.
its useless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment