FlyingSaucer LTR/RTL/BiDi issue with arabic text - java

I'm using flying saucer xhtmlrenderer for building pdf documents. Everything worked fine until now - now we should generate arabic text inside pdf.
Xhtmlrenderer is rendering Arabic text in reverse order.
I've read somewhere on internet (maybe on their own site) that xhtmlrenderer does not support bidi/rtl.
But IText itself contains examples to work with arabic and hebrew via ColumnText and PdfPTable (sources can be found here: http://sourceforge.net/projects/itextpdf/files/Examples/examples-155/examples-155.zip/download - arabic_hebrew.java), and those work fine.
I tried to use itext api in xhtmlrenderer's ReplacedElementFactory/ITextReplacedElement, but could not find good examples for positioning elements.
Does anyone tried to do this? Or maybe there is a simplier (or at least working) solution?

Finally I'm able to print arabic text in rtl/ltr using flying saucer.
In my code I'm giving width and alignment for every arabic text block, but in general it works fine.
(Edited) Code is large to print it down here, please find the code on Google groups, the links are in the comments.

Same issue I was facing, only solution i can find out was using arial fonts
import/add arial.ttf and arialbold.ttf files in resources folder of your project.
OutputStream outputStream = response.getOutputStream();
ITextRenderer renderer = new ITextRenderer();
// renderer.getFontResolver().addFont("/fonts/arialbold.ttf",
// BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("/fonts/arialbold.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// SharedContext sharedContext = renderer.getSharedContext();
// sharedContext.setPrint(true);
// sharedContext.setInteractive(false);
// sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory());
// sharedContext.getTextRenderer().setSmoothingThreshold(0);
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(outputStream);
renderer.finishPDF();
outputStream.close();
in your css use
html, body {
margin: 0;
padding: 0;
font-family: Arial, Arial Bold;
font-size: 10px;
line-height: 14px;
}

Related

UTF-8 emoji problem in PDF for Spring Boot

I am using Spring Boot to create and return PDF. There is an issue when my string content contains emoji and Unicode characters like "This is d£escript😭ion section😢😤😠😡🤬", then in downloaded PDF they are skipped. Can someone please help me to resolve this issue.
My code is like below
ITextRenderer renderer = new ITextRenderer();
ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
callback.setSharedContext(renderer.getSharedContext());
renderer.getSharedContext().setUserAgentCallback(callback);
renderer.setDocumentFromString(pdfContent(templateId, pdfData));
renderer.layout();
renderer.createPDF(outputStream);
}
pdfContent(TemplateId templateId, Map<String, Object> pdfData) throws TemplateException,
IOException {
return FreeMarkerTemplateUtils
.processTemplateIntoString(freemarkerMailConfiguration.getTemplate(templateId.getValue()), pdfData);
}
The problem is that the font you use doesn't contain emojis, so they can't be rendered in the PDF. Unfortunately, I could not find a font that would cover all emojis. The best I could find is DejaVu, which cover some of the emojis in your example.
To use it,
you have to download the DejaVu font (you will find it easily on the internet).
include it in the rendering process (make sure you match the exact path of the file):
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("font/dejavu-sans/DejaVuSans.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
set the font in the HTML:
<html>
<head>
<meta charset="utf-8" />
<style>
body{font-family:"DejaVu Sans", sans-serif;}
</style>
</head>
<body>
<p>This is descript😭ion section😢😤😠😡🤬.</p>
</body>
</html>
Here is the result in the PDF:
Emoji symbols are problematic as symbols we can see that if we use one font with two styles (upper left) even in one font the symbols are not matched well so in upper style there is one missing and in lower style two look identical.
Converted to PDF (upper middle) they look reasonable on the surface graphic image however we see that when extracted text (upper right) the font styling was lost and there is only one glyph possible for each valid font character.
So the lower row is on left also as shown in modern notepad however the same system font is now applying the other style and if we extract those we get
😭😢😤😠😡🤬 as
Thus the way a font and its style of emoji symbols is generally not well supported by a font system but if we traverse via html it is much more consistent however the text is not text.
The best we might get is a poor hybrid of images of undefined CID characters which can be confusing as the characters are all the same.
������
������
So if you export the pdf as symbols with an image overlay there is no visual equivalence

How to display multilingual or any UNICODE special character in itextpdf PDF which is accepted from UI?

How i can diplay multiligual and mathamatical symbols in java itextpdf PDF?
I have tried arialuni.ttf in BaseFont,
But still Some Mathematical symbols are not shows in itextpdf.
Please help for this, I want exact text content accepted from web application wanted to show in itextpdf.
BaseFont bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph("Şinasi ıssız ile ağaç", new Font(bf, 22));
document.add(p);

How to embed an standard font into generated PDF with PDFBox

I need to add some text to PDF/A files using the Apache PDFBox library for Java. The problem is that, because it needs to be a valid PDF/A file, all the used fonts must be embedded in it. I know that I can embed a TTF font using PDFBox, but I'd like to avoid having to provide a font file with the application, so I was wondering if there's a way to embed one of the standard fonts available in PDFBox as if it was external.
For example, when I write something using one of the standard fonts, the PDF validator complains about this:
I've used the following code to write the text:
PDFont standardFont = PDType1Font.HELVETICA_BOLD;
PDPage pag = new PDPage();
pag.setResources(new PDResources());
PDPageContentStream contentStream = new PDPageContentStream(pdfFile, pag);
//Begin the Content stream
contentStream.beginText();
//Setting the font to the Content stream
contentStream.setFont(standardFont, 12);
//Setting the position for the line
contentStream.newLineAtOffset(25, 500);
//Adding text in the form of string
contentStream.showText("JUST A SAMPLE STRING");
//Ending the content stream
contentStream.endText();
//Closing the content stream
contentStream.close();
pdfFile.addPage(pag);
pdfFile.save(file);
pdfFile.close();
Is there any option to force the embed of the font when setting it?
Thanks in advance,
There is only one font embedded in PDFBox. You can use it this way:
PDFont font = PDType0Font.load(doc, SomePdfboxClass.class.getResourceAsStream(
"/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));

java itextpdf 5.5.6 custom font not displaying properly

I am using ITextPdf (5.5.6) with my java + android web application to generate pdf.I wanted to view the contents in Gotham Light font. Therefore I have downloaded a Gotham Light .ttf file and created a font as follows.
In my generated pdf it looks like it is the good font but it is bolder.
The version that i am using of iText does support Gotham Ligh ?
The code that i am using:
Template t = ve.getTemplate(contratXHTML, "UTF-8");
StringWriter writer = new StringWriter();
t.merge(context, writer);
String body = writer.toString();
writer.close();
OutputStream os = new FileOutputStream(absolutePathContratPDF);
renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\Gotham Light_0.ttf",BaseFont.EMBEDDED);
renderer.setDocumentFromString(body);
renderer.layout();
renderer.createPDF(os);
os.close();
FileUtils.deleteQuietly(new File(pathTemporalDirectory));
return body;
Please can you help me ?
I got stuck with a similar problem,
The problem was not with the font you add, but the CSS/style used to body (HTML body) you want to print in PDF.
Please specify the font-family correctly. In my case, I used Arial font along with
font-family: Arial; in
and my font did not work with tag.
So, changing it to
font-family: sans-serif;
resolved the issue.
In few cases, you might need to define
font-family: Arial, Helvetica, sans-serif;
Reference : https://developer.salesforce.com/forums/?id=906F000000095wDIAQ

Defining Courier font in Java Documentation comments

In my Enunciate API documentation report, I need to change the font of some comment text to "Courier New" font type.
Is is possible?
Found a way for this feature -
Use Font html tag in the comment, Java doc code reads it as a html tag and displays in courier font.
/* Sample URL - <font face="courier">https://{Hostname}/programs/FireIn</font>, Replace the host name with the respective environment name</br>
*/
Starting with 1.2, Javadoc supports HTML style sheets. You can make these changes to stylesheet.css (located at the root directory of the Javadoc HTML files).
To choose a smaller, sans-serif fonts for the left-hand frames, change this:
#FrameItemFont { font-size: normal; font-family: normal }
To:
#FrameItemFont { font-size: 10pt; font-family: helvetica arial sans-serif }
Customize this yourself for whatever font you want.

Categories

Resources