I have below HTML:
String css = "@font-face {"
+ "font-family: myFont;"
+ "src: url(fonts/COMICATE.TTF);"
+ "-fs-pdf-font-embed: embed;"
+ "-fs-pdf-font-encoding: Identity-H;"
+ "}"
+ "font-family: myFont;}";
String html = "<html><head>"+css+"</head><body><p>Hello My Font</p></body></html>";
I have below flyingsaucer
code to convert the above HTML
to PDF
, and it worked perfect. It does render the Hello My Font with my custom font-family COMICATE.TTF
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
String outputFile = "result.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.close();
I have below iText
code to place the above HTML
at absolute position, and it works perfect except @font-face
in css
.
PdfContentByte cb = stamper.getOverContent(1);
ColumnText columnText = new ColumnText(cb);
columnText.setSimpleColumn(800, 800, 100, 100);
ElementList elements = XMLWorkerHelper.parseToElementList(html, ""); // I want to embed flyingsaucer generated html with custom font here
for (Element element : elements) {
columnText.addElement(element);
}
columnText.go();
Is there any way that I can take help of flyingsaucer
for the html conversion and then place it in my existing PDF using iText
? Or any other alternatives to achieve above using iText
?
UPDATE
I can use custom font with below code using iText and it works perfectly but I need to use HTML+CSS.
Font font = FontFactory.getFont("fonts/COMICATE.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();
PdfContentByte cb = stamper.getOverContent(1);
cb.setFontAndSize(baseFont, 12);
cb.beginText();
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "Hello My Font", 150, 760, 0);
cb.endText();