Set Windows font on Linux in SVG with Apache Batik - java

I'm developing a simple web application that provides an URL for rendering an SVG image. I use Apache Batik library for SVG rendering and I have a problem with setting font-family. I would like the resulting SVG to look identical (as it can) when viewed from any browser (mainly Chrome, FF and Edge) and any OS (mainly Linux and Windows). The problem is, that as I see, Batik uses some internal logic that loads fonts from the local system, and this prevents cross-platform behaviour.
To be more precise: my webapp runs on Ubuntu Linux, and I set font-family:'DejaVu Sans' to my graphics. This works perfectly when the client uses Linux, but on Windows, obviously this font does not exist. I tried to set font-family according to the User-Agent header in the request, but when I set font-family:'Verdana' for Windows clients, Batik tries to load the 'Verdana' font, and it obviously fails.
I tried to set the attribute like this: font-family:'DejaVu Sans',Verdana but Batik can not handle comma-separated list of valid font families. Is there any way to resolve this besides installing the Verdana font on the server?

Finally I achieved it by including the font in my app and register it with GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);. This way Batik properly keeps the 'Verdana' string in the font-family attribute. Thanks for mike-pomax-kamermans for the helpful advice!

Related

Aspose Android via java

I am using aspose java jar for web application and aspose Android via java for mobile application. By default aspose increase font size when i create pdf from mobile . Can someone tell me what exactly aspose doing for mobile?
Thanks for your inquiry. I suspect that the difference in font size is actually the difference in fonts. Android has a limited set of fonts and when Aspose.Words cannot find some fonts upon rendering document to PDF it tries to substitute them. You can configure font substitution rules using FontSubstitutionSetings class.
If you need the exact rendering, then the same set of fonts must be used on different platforms. You can put the required fonts into a folder and use it as font source in FontSettings.

Render docx file in a browser

I'm using docx4j to convert a microsoft word document into a pdf then displaying it in a browser http://www.docx4java.org/trac/docx4j and it works well for a preview. The problem I'm facing is that this conversion loses most of the microsoft word document formatting. Page breaks and fonts don't transfer into the PDF format properly and even though I'm using standard font types docx4j doesn't come with them. In a Linux Tomcat hosted scenario fonts are not found and throw exceptions as it falls back to sans serif or other generic types.
I have found this Microsoft tool to make documents render online, but I'm behind a firewall so I cannot include this tool as an option: https://products.office.com/en-us/office-online/view-office-documents-online
I'm open to suggestion on displaying a docx file as a preview and print option from within a browser. Pdf conversion appears to be the most promising, but I run into formatting issues.
Any ideas are welcome!
Have a play with http://converter-eval.plutext.com/viewer.html
Consider it an alpha level preview. We haven't quite released it yet, but you will be able to host it behind a firewall.
It isn't open source, I'm afraid, and we're still working out pricing (and whether/how there could be a free edition).
If you only need to render a docx document in a browser, u can use Google Documents Viewer for this as :
<iframe src="http://docs.google.com/gview?url=pathOfDocx&embedded=true" />

How to get ttf font data from system fonts in java

I have some ttf fonts installed on system.
I get that list using
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()
This is not only ttf fonts but all fonts I guess.
Eventually if I use:
Font.decode(fontName)
I can get awt.Font instance.
As far as I know Font is not connected to actual PhysicalFont, so
how can I retrieve either ttf font file, or byte data from that ttf file for a font from that list or from awt.Font ? I am trying to retrieve a physical font data or anything similar. That data should be somewhere right?
The reason I need it is to eventually use with libGDX
FreeTypeFontGenerator in order to generate BitmapFont
This has to work on windows, osx and linux.
It isn't possible. The best you can do is using reflection, but it will only work with an Oracle JRE and accesses a private API so may break with any new Oracle release.
You could probably write a native lib to enumerate the fonts and their files.
As #NateS pointed out, it appears what I want to achieve is not exactly possible.
So I will just share the solution I used in my case for now:
Which is this class:
FontManager.java
This allows to pre-cache existing ttf files in known system locations based on your system, and then make a Map for fontName->fontFile type of connection. this then goes to preferences, and is loaded on next run.
Known issues are
awt.Font has a known bug that is not able to read some ttf font family names on osx systems (mainly some arabic and chinese fonts)
Did not test on Linux, will probably fail.
First run might be slow if you have many fonts.
It would be ideal to write a native lib, but time is of an essence...

Displaying embedded fonts with PDFBox and Swing

I am using PDFBox to display PDF files inside a JInternalFrame. When opening PDF I get lots of warnings like this:
Changing font on <m> from <Tahoma Negrita> to the default font
I am aware that the fonts being reported are not part of the standard set of 14 fonts. So I decided to check if those fonts are embedded on the PDF file (thinking that there shouldn't be a problem loading embedded fonts, right?).
So I open the file on different readers and check properties/fonts. I am in doubt whether this section reports fonts required by the document or fonts actually embedded in the document.
The information that I get is as follows:
BAAAA+Tahoma-Bold (embedded Subset), type:TrueType, Encoding:
CAAAA+Tahoma (Embedded Subset), type:TrueType, Encoding:
Confused about this, I researched on how to embed fonts from OpenOffice and found that the PDF/A-1a option should be checked. So I made another PDF using this option (in case this was not used when making the original PDF file), yet I got the same results.
I would like your guidance understanding how this works. I would like to be able to open PDF files just as PDF readers do. I also read about the PDFBox_External_Fonts.properties but I am guessing this file shouldn't be modified since I am dealing with embedded fonts.
Thanks.
pdfbox is not able to parse embedded subsets of TrueType fonts.
As far as I understand it, embedded TrueType subsets are missing some metadata for the font file that pdfbox needs.
The bug is known but not easy to solve. Right now I can only advise to use embedded Type 1 Fonts if possible, pdfbox can deal with them.
You can also try to set the path to your complete font files in your pdfbox.jar under org/apache/pdfbox/resources/PDFBox_External_Fonts.properties, so if pdfbox cannot parse the subset, at least it can find a full path to the original font file. Maybe that works, but I have not tested this.
Good Luck!

How to render Asian characters in a PDF using xhtmlrenderer

I was wondering what steps were needed to render Asian characters using the java based xhtmlrenderer (flying saucer) library?
I am wanting to render the following:
<html>
<body>同名の映画のモデ</body>
</html>
Without any font settings being added to the HTML this renders fine in normal browsers, but I can't find anyway to render this to PDF using the iTextRenderer portion of xhtmlrenderer.
After following various threads on the mailing list, I see lots of posts talking about adding .TTF files from the c:\windows\fonts directory, and I have modified the examples to run on linux ( https://gist.github.com/643173745182c9becc57 ), which shows me various fonts being displayed, but I don't see any Asian glyffs.
Does anyone have any decent pointers, or clean solutions to this problem? Or am I looking at the wrong problem with a really simple solution elsewhere?
You can also add the font style information in css.
#font-face {
font-family: 'your_font_face_name';
src: url('your_font.ttf');
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
To support the big character set you need to specify a font file that has all those characters in it. Once you've picked a font file you'll need your application to point to that file. I've found that just putting the font files in your font's directory doesn't work.
Try embedding the font too, eg.
renderer.getFontResolver().addFont("your_font_file.ttf", BaseFont.EMBEDDED);
This link has quite a few font files.

Categories

Resources