iText monospaced fonts in html view with bold, italic from css style - java

I am using html to generate PDF document, since its an EMR record I have to use Monospaced fonts.
PDF is getting generated fine, but css style for bold and italics are getting ignored, as I am using single .otf file for font hence no bold and italics.
I was wondering how to enable the same. Below are the code snippets.
Font Factory:
public static class MyFontFactory implements FontProvider,Serializable {
public Font getFont(String fontname,
String encoding, boolean embedded, float size,
int style, BaseColor color) {
BaseFont bf3 = null;
try {
bf3 = BaseFont.createFont("Inconsolata.otf",BaseFont.CP1252, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
return new Font(bf3, 6);
}
public boolean isRegistered(String fontname) {
return false;
}
}
PDF Generation Code:
public void createPdf(Object object) throws Exception, DocumentException{
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(new File("test.pdf")));
// step 3
document.open();
// create extra properties
HashMap<String,Object> map = new HashMap<String, Object>();
map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory());
// step 4
String snippet;
// create the snippet
snippet = createHtmlSnippet(object);
Map<Object,Object> model = new HashMap<Object,Object>();
model.put("object", object);
StyleSheet css = new StyleSheet();
Map<String, String> stylemap = new HashMap<String, String>();
stylemap.put("font-style", "italic");
stylemap.put("font-size", "small");
stylemap.put("font-weight", "bold");
css.loadStyle("header",(HashMap<String, String>) stylemap);
css.loadStyle("strongClass", "text-decoration", "underline");
List<Element> objects = HTMLWorker.parseToList(new StringReader(snippet), css, map);
for (Element element : objects)
document.add(element);
// step 5
document.close();
}
In the above code css supplied does not produce any effect on output
as I mentioned due to single font defined, if I want to have bold and
italics how can that be achieved?
Really appreciate if anyone provides pointers or help regarding same.
Thanks.
Note: If I remove Monospaced fonts css gets applied.

You are confusing a font family with a font.
Inconsolata is a font family consisting of different fonts:
Inconsolata regular as defined in inconsolata.ttf
Inconsolata bold as defined in inconsolata-Bold.ttf
See http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/
I didn't know of any bold, italic or bold-italic version because I assumed "there is no bold or italic for Inconsolata." And if there is no font program for other styles, you shouldn't expect iText to support those styles (*).
Then I found a repository with a TTF for the bold font: http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/
Searching StackOverflow I read the question about Inconsolata Italic in MacVim on StackOverflow; unfortunately these fonts can't be used in iText.
(*) When a font doesn't support bold or italic, iText can mimic these styles by changing the render mode and/or the skew. However, you'll have better results by choosing another monospaced font.

Related

iText/Java When converting HTML to PDF, the text positioning does not match

I use iText to convert HTML to PDF. In general, everything works as it should, but there is a problem with some font families. After the conversion, the height positioning of fonts in a PDF document does not match what it was in HTML. An example in the picture below (on the left is the generated PDF, on the right is a preview of the PDF document):
In the image:
(1) - the font Arial - with it all right.
(2) - the font Times New Roman - in PDF it rose to 2px.
(3) - the Dancing Script font - in PDF it dropped to 5px.
(4) - the Tangerine font - it’s all right.
(5) - the Charmonman font - in PDF it dropped to 5px.
For HTML template, which is generated in PDF specified CSS styles exactly the same as used to preview PDF document.
PDF is generated as follows:
private byte[] getPdf(StringBuilder fileText) throws IOException {
FontProvider fontProvider = new FontProvider(addCertificateFonts(), "");
fontProvider.addStandardPdfFonts();
ConverterProperties properties = new ConverterProperties().setFontProvider(fontProvider);
properties.setBaseUri(getBaseProjectUri());
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
HtmlConverter.convertToPdf(fileText.toString(), out, properties);
return out.toByteArray();
}
}
private FontSet addCertificateFonts() {
FontSet set = new FontSet();
List<String> fontPaths = //здесь список путей к файлам шрифтов
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
for (String path : fontPaths) {
set.addFont(context.getRealPath(path));
}
return set;
}
Is there any way to fix the font positioning?
Is it possible that the fonts themselves have some configurations that may affect the position of the text with this font?

PDFBox: No glyph for U+0050 in extracted font

I'm trying to create new page in document and write some text to it, while using the font contained in the file.
The font is extracted from the resources:
PDPage page = document.getPage(0);
PDResources res = page.getResources();
List<PDFont> fonts = new ArrayList<>();
for (COSName fontName : res.getFontNames()) {
PDFont font = res.getFont(fontName);
System.out.println(font);
fonts.add(font);
}
And later used to write some text:
stream.beginText();
stream.setFont(fonts.get(0), 12);
stream.setTextMatrix(Matrix.getTranslateInstance(20, 50));
stream.showText("Protokol");
stream.endText();
The showText method always fails with error
No glyph for U+0050 (P) in font QZHBRL+ArialMT
But the glyph is there, as verified by FontForge:
Also the method hasGlyph returns true.
The complete project including the PDF is available at github repository showing the issue
Here you actually ran into an open PDFBox TODO, your stream.showText eventually calls encode of the underlying CID font for each character and here we have:
public class PDCIDFontType2 extends PDCIDFont
{
...
public byte[] encode(int unicode)
{
int cid = -1;
if (isEmbedded)
{
...
// otherwise we require an explicit ToUnicode CMap
if (cid == -1)
{
//TODO: invert the ToUnicode CMap?
// see also PDFBOX-4233
cid = 0;
}
}
...
if (cid == 0)
{
throw new IllegalArgumentException(
String.format("No glyph for U+%04X (%c) in font %s", unicode, (char) unicode, getName()));
}
return encodeGlyphId(cid);
}
...
}
(org.apache.pdfbox.pdmodel.font.PDCIDFontType2)
Where PDFBox could not otherwise determine a mapping from Unicode to glyph code (if (cid == -1)), the JavaDoc comments indicate another way to determine a glyph code, an inverse lookup of the ToUnicode map. If this was implemented, PDFBox could have determined a glyph ID and written your text.
Unfortunately it is not implemented yet.
This has been fixed in issue PDFBOX-5103. This will be available in PDFBox 2.0.23 and until then, in a snapshot build.

Render Type3 font character as image using PDFBox

In my project, I'm stuck with necessity to parse PDF file, that contains some characters rendered by Type3 fonts. So, what I need to do is to render such characters into BufferedImage for further processing.
I'm not sure if I'm looking in correct way, but I'm trying to get PDType3CharProc for such characters:
PDType3Font font = (PDType3Font)textPosition.getFont();
PDType3CharProc charProc = font.getCharProc(textPosition.getCharacterCodes()[0]);
and the input stream of this procedure contains following data:
54 0 1 -1 50 43 d1
q
49 0 0 44 1.1 -1.1 cm
BI
/W 49
/H 44
/BPC 1
/IM true
ID
<some binary data here>
EI
Q
but unfortunately I don't have any idea how can I use this data to render character into an image using PDFBox (or any other Java libraries).
Am I looking in correct direction, and what can I do with this data?
If not, are there some other tools that can solve such problem?
Unfortunately PDFBox out-of-the-box does not provide a class to render contents of arbitrary XObjects (like the type 3 font char procs), at least as far as I can see.
But it does provide a class for rendering complete PDF pages; thus, to render a given type 3 font glyph, one can simply create a page containing only that glyph and render this temporary page!
Assuming, for example, the type 3 font is defined on the first page of a PDDocument document and has name F1, all its char procs can be rendered like this:
PDPage page = document.getPage(0);
PDResources pageResources = page.getResources();
COSName f1Name = COSName.getPDFName("F1");
PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name);
Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap();
COSDictionary charProcsDictionary = fontF1.getCharProcs();
for (COSName key : charProcsDictionary.keySet())
{
COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key);
PDType3CharProc charProc = new PDType3CharProc(fontF1, stream);
PDRectangle bbox = charProc.getGlyphBBox();
if (bbox == null)
bbox = charProc.getBBox();
Integer code = f1NameToCode.get(key.getName());
if (code != null)
{
PDDocument charDocument = new PDDocument();
PDPage charPage = new PDPage(bbox);
charDocument.addPage(charPage);
charPage.setResources(pageResources);
PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage);
charContentStream.beginText();
charContentStream.setFont(fontF1, bbox.getHeight());
charContentStream.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
charContentStream.endText();
charContentStream.close();
File result = new File(RESULT_FOLDER, String.format("4700198773-%s-%s.png", key.getName(), code));
PDFRenderer renderer = new PDFRenderer(charDocument);
BufferedImage image = renderer.renderImageWithDPI(0, 96);
ImageIO.write(image, "PNG", result);
charDocument.close();
}
}
(RenderType3Character.java test method testRender4700198773)
Considering the textPosition variable in the OP's code, he quite likely attempts this from a text extraction use case. Thus, he'll have to either pre-generate the bitmaps as above and simply look them up by name or adapt the code to match the available information in his use case (e.g. he might not have the original page at hand, only the font object; in that case he cannot copy the resources of the original page but instead may create a new resources object and add the font object to it).
Unfortunately the OP did not provide a sample PDF. Thus I used one from another stack overflow question, 4700198773.pdf from extract text with custom font result non readble for my test. There obviously might remain issues with the OP's own files.
I stumbled upon the same issue and I was able to render Type3 font by modifying PDFRenderer and the underlying PageDrawer:
class Type3PDFRenderer extends PDFRenderer
{
private PDFont font;
public Type3PDFRenderer(PDDocument document, PDFont font)
{
super(document);
this.font = font;
}
#Override
protected PageDrawer createPageDrawer(PageDrawerParameters parameters) throws IOException
{
FontType3PageDrawer pd = new FontType3PageDrawer(parameters, this.font);
pd.setAnnotationFilter(super.getAnnotationsFilter());//as done in the super class
return pd;
}
}
class FontType3PageDrawer extends PageDrawer
{
private PDFont font;
public FontType3PageDrawer(PageDrawerParameters parameters, PDFont font) throws IOException
{
super(parameters);
this.font = font;
}
#Override
public PDGraphicsState getGraphicsState()
{
PDGraphicsState gs = super.getGraphicsState();
gs.getTextState().setFont(this.font);
return gs;
}
}
Simply use Type3PDFRenderer instead of PDFRendered. Of course if you have multiple fonts this needs some more modification to handle them.
Edit: tested with pdfbox 2.0.9

Encoding for FontFactor.getFont() [duplicate]

This question already has an answer here:
iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠
(1 answer)
Closed 6 years ago.
Hiyas
I'm trying to display this string:
λλλλλλλλλλλλλλλλλλλλλλλλ
which is read from a RTF file, parsed and put into this variable. It is NOT used as constant in the code.
Font pdfFont = FontFactory.getFont(font.getFont().getName(), BaseFont.IDENTITY_H, embed, font.getFont().getSize2D(), style);
Phrase phrase = new Phrase("λλλλλλλλλλλλλλλλλλλλλλλλ", pdfFont);
ColumnText.showTextAligned(content[i], alignment, phrase, x, y, rotation);
I also tried CP1252 (and basically all the other encodings I found) together with a simple ArialMT.ttf font, but that damn string is never displayed. I can see that the conversion to the byte array inside iText (we use 5.5.0) always returns a null length byte array which explains why the text is not used, but I don't understand why. What encoding would I need to use to make this visible in a PDF?
Thanks a lot
I suppose that you want to get a result that looks like this:
That's easy. I first tried the SunCharacter example from the official documentation. That example was written in answer to the question: iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠
I then changed the TEXT to:
public static final String TEXT = "Always use the Unicode notation for special characters: \u03bb";
As you can see, I don't use λ in my source code (that's bad practice). Instead I use \u03bb which is the Unicode notation of λ.
The result looked like this:
That's not what you want; you want ArialMT. So I changed the FONT to:
public static final String FONT = "c:/windows/fonts/arial.ttf";
This gave me the desired PDF.
This is the full code sample:
public class LambdaCharacter {
public static final String DEST = "results/fonts/lambda_character.pdf";
public static final String FONT = "c:/windows/fonts/arial.ttf";
public static final String TEXT = "Always use the Unicode notation for special characters: \u03bb";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new LambdaCharacter().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
Paragraph p = new Paragraph(TEXT, f);
document.add(p);
document.close();
}
}
I works just fine.
Maybe you aren't really using Arial. Maybe font.getFont().getName() doesn't give you the correct name of the font. Or maybe it gives you the correct name of the font, but you forgot to register the font. In that case, you will see that Helvetica is used. Helvetica can't render a lambda. You need Arial or Cardo-Regular or Arial Unicode or another font, as long as that font knows how to render a lambda.
If you don't know how to register a font, read:
How to load custom font in FontFactory.register in iText or
Creating fonts from *.ttf files using iText or
Using Fonts in System with iTextSharp or
Get list of supported fonts in ITextSharp or
Why is my font not applied when I create a PDF document? or... (there are just too many hits when I search for an answer to that question)

Why is the Gujarati-Indian text not rendered correctly using Arial Unicode MS?

This is a follow-up on this question How to export fonts in Gujarati-Indian Language to pdf?, #amedee-van-gasse, QA Engineer at iText asked me to post a question specific to itext with relevant mcve.
Why is this sequence of unicode \u0ab9\u0abf\u0aaa\u0acd\u0ab8 not rendered correctly?
It should be rendered like this:
હિપ્સ , also tested with unicode-converter
However this code (example adapted form iText: Chapter 11: Choosing the right font)
public class FontTest {
/** The resulting PDF file. */
public static final String RESULT = "fontTest.pdf";
/** the text to render. */
public static final String TEST = "\u0ab9\u0abf\u0aaa\u0acd\u0ab8";
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
BaseFont bf = BaseFont.createFont(
"ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 20);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36, 730, 569, 36);
column.addElement(new Paragraph(TEST, font));
column.go();
document.close();
System.out.println("DONE");
}
public static void main(String[] args) throws IOException, DocumentException {
new FontTest().createPdf(RESULT);
}
}
Generates this result:
That looks different from
હિપ્સ
I have test with itextpdf-5.5.4.jar,itextpdf-5.5.9.jar and also itext-2.1.7.js3.jar (distributed with jasper-reports)
The font used it the one distributes with MS Office ARIALUNI.TTF and it can be download from here Arial Unicode MS *Maybe there are some legal issues downloading see Mike 'Pomax' Kamermans comment
Neither iText5 nor iText2 (which is a very outdated version by the way) support rendering of Indic scripts, no matter which font you select.
Rendering Indic scripts is not similar to any Latin scripts, because a long series of additional actions should be taken to get the correct result, e.g. some characters need to be reordered first according to the language rules.
This is a known issue to iText company.
There is a stub implementation for Gujaranti in iText5 called GujaratiLigaturizer, but the implementation is really poor and you cannot expect to get correct results with it.
You can try to process your string with this ligaturizer and then output the resultant string in the following way:
IndicLigaturizer g = new GujaratiLigaturizer();
String processed = g.process(inputString);
// proceed with the processed string
Build your application using latest typography jar file that
Will solve your problem of Gujarati font rendering in pdf
In itext.

Categories

Resources