Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Created November 2, 2009 22:07
Show Gist options
  • Save rlivsey/224522 to your computer and use it in GitHub Desktop.
Save rlivsey/224522 to your computer and use it in GitHub Desktop.
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import com.lowagie.text.pdf.BaseFont;
public class FSTest {
public static void main(String[] args)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
renderer.getFontResolver().addFont("CALIBRI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
String url = new File("test.html").toURI().toURL().toString();
OutputStream os = new FileOutputStream("fstest.pdf");
BufferedOutputStream bos = new BufferedOutputStream(os);
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(bos);
os.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Font;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class ITextTest {
public static void main(String args[])
throws IOException, DocumentException {
BaseFont calibri_bf = BaseFont.createFont("CALIBRI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font calibri = new Font(calibri_bf, 18);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("itext.pdf"));
document.open();
document.add(new Paragraph("Some unicode: \u0394 \u2192", calibri));
document.close();
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Test PDF</title>
<style>
p
{
color: red;
font-size: 50px;
font-family: Calibri;
}
</style>
</head>
<body>
<p>
Some unicode [&#x0490;, &#x2192;]
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment