How to display Chinese characters in java web applications? - java

I use Itext 5 to create pdf file. I refer to https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-1 and get a pdf. When I open it, Chinese characters display normally.
But I develop web applications like https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-9 described. Chinese characters is blank when pdf show in browser.
My font code is
String chFontPath = "c:\fonts\xxx.ttf";
BaseFont chBaseFont = BaseFont.CreateFont(chFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(chBaseFont, 12);
Does anybody know?

If you embed the font using an absolute path, probably the path will be broken for any webapp you develop. Use a relative path instead for any embeddable (fonts, images, etc) so you can place them in server without any trouble.
I think Bruno's answer about a relative anchor could help you to set up a relative path for your font: https://stackoverflow.com/a/27064142/4048864

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

Java ,wkhtmltopdf, HTML to PDF not all fonts works correctly

I am using wkhtmltopdf to generate PDF from HTML (string not file).
Before I start creating PDF, I add all fonts to HTML file
htmlTemplate = htmlTemplate.replaceAll("\\$\\{fontsPlaceholder}", ResourcesCache.getInstance().getFontsCSSCache());
and all fonts are inside of html, and look like
#font-face {
font-family: 'Abril_Fatface-Regular';
src: url(data:font/ttf;base64,AAEAA....
But when I tried to use font properties, bold, italic etc, and then make the pdf, this is not working correctly, and field use 'regular' font in pdf, but in html set bold..
So, why not all fonts working good in wkhtmltopdf, does someone fixed issue like this?
I have been solve this issue. The problem was with fonts, not with wkhtmltopdf lib.
If you want to use font-property correct, you need to be sure that 'Preferred Family' is set in your font. To check or to set this you can with FontForge app.
Open FontForge, then import your font, and click 'Element/FontInfo/TTF Names', and change 'Preferred Family' (it's need to be unique for each font).

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"));

how to specify default font in javafx WebView to be the same as default UI font

Some coworkers and I are writing help documentation in HTML to be used with a JavaFX WebView panel. We want it to look consistent with the rest of the UI. Normally I would specify the default font in a CSS file, but the rest of the JavaFX application appears to use the default system UI font.
Is there any way to set this programmatically? I can pick Segoe UI by name in .css, but we want it to look correct regardless of platform.
Use
Font.getDefault().getName()
to get the defaults font name and just add it to the HTML file / String which is about to be added into the WebView.
example:
cssString = cssString + "html { font-family: '" + Font.getDefault().getName() + "';}";
Ofcause you should do it cleaner than this.
If I understood you completely you want a default font for the whole javafx part of your application. I suggest you do this:
Download a font of your own as .ttf and add it to the resources in your application.
Load the font with the static function Font.loadFont:
Font.loadFont(this.class.getClassLoader().getResource("fonts/YOURFONT.ttf").toExternalForm(), 10);
Now set the font to the root of the application. I suggest doing that with css as it is suggested here -> another stackoverflow answer: In your application.css add:
.root{ -fx-font-family: "YOURFONT"; }
After that you need to load the application.css file and set it on your scene:
scene.getStylesheets().add(this.class.getClassLoader().getResource("css/application.css").toExternalForm());
I believe that it is better if you are not dependent of the system on which your app is running, so pack the font in your application.
For the html you can do completely the same thing with normal css by providing a #font-face.

how to create pdf file which contains marathi font using itext?

I have to create report using itext but the language should be hindi or marathi.
Is it possible to make pdf file which contains marathi font like mangal,shruti,shree-dev...etc if yes plz reply me
Thank you!
Adding a marathi font in itext pdf is :
BaseFont kruti_Dev = BaseFont.createFont("c:/WINDOWS/Font/Kruti_Dev_010.ttf"
,BaseFont.CP1252,BaseFont.EMBEDDED);
Font font = new Font(kruti_Dev, 12, Font.NORMAL);
Try to use Aspose PDF Generator. Make request and response to utf-8.

Categories

Resources