Grails / RenderPdf Arabic Chars - pdf

We have a grails app, in which we are using the Render Plugin to render content in .pdf. It all works fine for English, but unfortunately for Arabic (which we must render) all the charactes seem "broken". Some numbers and spaces there...
The render plugin uses IText, and I have tried the approach with:
...
def renderer = new ITextRenderer()
FontResolver resolver = renderer.getFontResolver()
renderer.getFontResolver().addFont("/usr/share/fonts/truetype/ttf-arabeyes/ae_AlArabiya.ttf", BaseFont.EMBEDDED)
...
(the font used here is just an example), but in any case, it doesn't work.
Anybody any experience with this kind of issue?
Thank you in advance!

renderer.getFontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED_SUBSET);
The default encoding for fonts in iText is WinAnsiEncoding, AKA Code Page 1252. You need to specify an encoding that contains the characters you want...
Yep. Google Code produced this bit of code for the addFont you're using:
public void addFont(String path, boolean embedded)
throws DocumentException, IOException {
addFont(path, BaseFont.CP1252, embedded);
}
IDENTITY_H lets you address all the glyphs in a given font. I always recommend it, though there is a small drawback. Using IDENTITY_H forces the font to be an embedded subset in iText, no way around it.

Related

Content in meta tag

Like the first image, the meta tag is displayed correctly in inspect elements mode but incorrectly displayed in view page source mode as in the second image. Thank you for suggesting a solution to this problem.
I understood the answer:
Because, by default, the HTML encoding engine will only safelist the basic latin alphabet (because browsers have bugs. So we're trying to protect against unknown problems). The &XXX values you see still render as correctly as you can see in your screen shots, so there's no real harm, aside from the increased page size.
If the increased page size bothers you then you can customise the encoder to safe list your own character pages (not language, Unicode doesn't think in terms on language)
To widen the characters treated as safe by the encoder you would insert the following line into the ConfigureServices() method in startup.cs;
services.AddSingleton<HtmlEncoder>(
HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin,
UnicodeRanges.Arabic }));
Arabic has quite a few blocks in Unicode, so you may need to add more blocks to get the full range you need.

Gecko Engine in ABCPDF not rendering text color

I'm using ABCPDF + Gecko to render some PDFs within an HTML->PDF templating engine I'm writing. I switched to Gecko specifically to be able to render embedded SVG graphics for things like signatures. However, it seems that now all font colors have changed to black,regardless of the style sheet, class names, or even inline styles being used. Is this a gecko configuration problem, a problem with ABCPDF, or ... ??
Example HTML:
<span style="color:blue;">This should be blue</span>
I'm specifying no options besides browser size, and didn't customize anything in the XULRunner21_0 folder. I'm using AddImageHtml to add the html to a PDF document.
Update: after some more research looks like this may be tied to how Gecko treats text upon printing (not the same as media type). Is there a setting that will prevent it from mucking with the visuals?
This ended up being caused by including bootstrap css in my little project. Removing the print media bits from my copy of bootstrap resolved the issue.

How to use Verdana Font in Stamper (iText PDF)

I want to use Verdana as a font while stamping a PDF file with iText PDF. The original file uses Verdana, which isn't an option in the class Basefont.
Here is the function to create my font right now:
def standardStampFont() {
return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false)
}
I'd like to change that to the Verdana Font, but simply exchanging the Part BaseFont.HELVETICA with "Verdana" doesn't work.
Any idea? Thanks in advance!
As documented, iText supports the Standard Type 1 fonts, because iText ships with AFM file (Adobe Font Metrics files). iText has no idea about the font metrics of other fonts (Verdana isn't a Standard Type 1 font). You need to provide the path to the Verdana font file.
BaseFont.createFont("c:/windows/fonts/verdana.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED)
Note that I change false to BaseFont.EMBEDDED because the same problem you have on your side, will also occur on the side of the person who looks at your file: his PDF viewer can render Standard Type 1 fonts, but may not be able to render other fonts such as Verdana.
Caveat: The hard coded path "c:/windows/fonts/verdana.ttf" works for me on my local machine because the font file can be found using that path on my local machine. This code won't work on the server where I host the iText site, though (which is a Linux server that doesn't even have a c:/windows/fonts directory). I am using this hard coded path by way of example. You should make sure that the font is present and available when you deploy your application.
Adding this line makes sure the FontFactory actually registers the fonts in the operating systems' default fonts directories:
FontFactory.RegisterDirectories();
After that all the installed fonts can be found using something like
var myfont = FontFactory.GetFont("Verdana", 10f, iTextSharp.text.Font.NORMAL);
I get that this post is old, but it's still relevant I guess.

Adobe Flex 3: can I get TTF/OTF system fonts and embed at runtime?

Adobe Flex 3: can I get TTF/OTF system fonts and embed at runtime?
Hi,
I'm a Stack Overflow noob so please go easy on me.
I've searched all day and found dozens of tutorial/examples on how to use [Embed] metadata or
Flash SWF files but they all tell me to either use a path in the source attrib or a text string in the systemFont attrib. What I want to do is; at runtime get all installed fonts on a given machine, determine which are TTF/OTF, embed them all and offer them in a comboBox. Something along these lines;
public function embedFonts():void{
try{
//get all device and embedded fonts
availableFonts = Font.enumerateFonts(true);
availableFonts.sortOn("fontName", Array.CASEINSENSITIVE);
for each(var thisFont:Font in availableFonts)
{
[Embed(systemFont=thisFont.fontName,
fontName=thisFont.fontName,
mimeType='application/x-font')]
//this bit need to create a unique variable name on each loop
var thisfont:Class;
}
}
catch(error:Error){
//if cant embed it's likely not to be TTF or OTF
//so move on to the next font.
}
}
Does anyone know a way?
Many, many thanks
You can't embed fonts at runtime.
And what would be the point?
Your swf runs on my machine, enumerates fonts on my machine, then embeds them
and offer them back to me? They are installed, use them directly, no need to embed.
So not only that is it not possible, but probably nobody will ever implement such a feature.

Special characters in iText

I need help in using these symbols ⎕, ∨, ๐, Ʌ, and so on. But when I create a PDF with iText these symbols do not appear.
What can I do so that these symbols appear?
You have to use a font and encoding that contains those characters. Your best bet is to use IDENTITY_H for your encoding, as this grants you access to every character within a given font... but you still have to use the right font.
There are several font-manipulation examples within "iText in Action's" chapter on fonts:
http://www.itextpdf.com/book/chapter.php?id=11
The examples are down the right side. Buying the book would probably help too.
I had the same problem too and I figured out using IDENTITY_H for encoding is working fine.
For example:
java.awt.Font f =...;
Font font = FontFactory.getFont(f.getName(),BaseFont.IDENTITY_H)
I don't understand why with BaseFont.WINANSI it doesn't work. Winansi is the standard Windows Cp1252 character set, that one used by my JVM. So, if the char is correctly displayed in Java, why it is not the case for PDF?
You can escape them according to the unicode escape sequence defined in the java language specification. See http://java.sun.com/docs/books/jls/first_edition/html/3.doc.html
If you are using IntelliJ IDEA for your code you can download the StringManipulation plugin, that does the escapes for you. In the settings of IDEA you can also set the "Transparent native-to-ascii conversion" checkbox under File encodings, and this should help do the trick.
square in pdf file by iText:
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialbd.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
question.add(new Phrase("\u25A1", new Font(bf, 26)));
You can see a pdf file exemple here