import java.io.FileOutputStream;
import java.io.StringReader;

import org.junit.Test;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import com.itextpdf.text.pdf.PdfWriter;


public class TestCreatePDF {

	@Test
	public void htmlCreate() throws Exception {
		// TODO Auto-generated method stub
		String fontname = "d:\\GulimChe.ttf";
		String filename = "d:\\filename.pdf";
		
		FontFactory.register(fontname);
		StringBuffer sBuff = new StringBuffer("<html>");
		sBuff.append("<head></head>");
		sBuff.append("<body>");
		sBuff.append("<table border=1>");
		sBuff.append("<tr><td>Test worker <b>한글</b> 테스트</td><td>11<b>1</b>11</td></tr>");
		sBuff.append("</table>");
		sBuff.append("</body>");
		sBuff.append("</html>");
		StringReader stringReader = new StringReader(sBuff.toString());

		Document document = new Document();
		StyleSheet st = new StyleSheet();
		st.loadTagStyle("body", "face", "굴림체"); 
		st.loadTagStyle("body", "encoding", "Identity-H"); 
		st.loadTagStyle("body", "leading", "12,0"); 
		HTMLWorker worker = new HTMLWorker(document);
		PdfWriter.getInstance(document, new FileOutputStream(filename));
		document.open();
		java.util.List<Element> p = HTMLWorker.parseToList(stringReader, st);
		for (int k = 0; k < p.size(); ++k)
		    document.add((Element)p.get(k));
		document.close();
	}

}

GulimChe.ttf

itextpdf-5.4.0.jar


[출처] http://blog.wooriaru.com/131163646


+ Recent posts