FPDF with python : Some Japanese characters are showing as rectangle - python

I try to print a simple japanese word in a pdf. I modified a basic exemple to see if it would work but not what I expected to see.
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
# Add a AR PL New Sung Unicode font (uses UTF-8)
# The Open Source Chinese Font (also supports other east Asian languages)
pdf.add_font('fireflysung', '', 'fireflysung.ttf', uni=True)
pdf.set_font('fireflysung', '', 14)
pdf.write(8, u'Chinese: 你好世界\n')
pdf.write(8, u'Japanese: こんにちは世界\n')
pdf.write(8, u'bicycle: 自転車 (ジテンシャ)\n')
pdf.ln(10)
pdf.output("unicode.pdf")
Result
Expected:
Japanese: 自転車 (ジテンシャ)
How do I display this missing character ?
Edit:
So I found a font that seams to work for now. I converted it to .ttf using otf2tff. Hope it will help.

Related

Bengali words printing out all wrong in manim

I had been trying to animate bengali characters using Manim. I used this method to use pc fonts in Manim. Everything seemed to be working well until i saw the output. For instance, if i write বাংলা লেখা i get the output as (look closely at the output) বাংলা লখো. Most of the times it spits out absolutely meaningless words.
The code used was:
class test_3(Scene):
def construct(self):
text1 = Text('বাংলা লেখা', font='Akaash')
text2 = Text('english text', font='Arial').move_to(DOWN)
self.play(Write(text1), Write(text2))
self.wait()
Bangla texts can be displayed properly just by specifying a Bangla font in Text() or MarkupText().
For example, if I like to display the Bangla text আইনস্টাইনের সমীকরণ in Kalpurush font, it can be done by:
from manim import *
class bangla(Scene):
def construct(self):
text = Text("আইনস্টাইনের সমীকরণ", font="Kalpurush")
self.play(Write(text))
Here, the font is locally installed. Many fonts can be used directly from online via the python package manim-fonts.
If you want to nicely show Bangla texts/sentences that contain inline-maths, you can use the LaTeX package latexbangla.
Here's an example code:
from manim import *
class bangla(Scene):
def construct(self):
myTemplate = TexTemplate(tex_compiler="xelatex", output_format=".pdf", preamble=r"\usepackage[banglamainfont=Kalpurush, banglattfont=Kalpurush]{latexbangla}")
tex = Tex(r"আইনস্টাইনের সমীকরণ, $E^2=(mc^2)^2+(pc)^2$", tex_template=myTemplate)
self.play(Write(tex))
The output:
N.B. The issue was also discussed on the Github repositories: ManimCommunity/manim and 3b1b/maim.
This is because of font type.
You should use bangli font. Try any font from here
Try to use ANSI Bangla fonts like "SutonnyMJ". If you are using Avro keyboard you can use Output as ANSI option like this,
Then if you have chosen font for example "SutonnyMJ", your code should look like this,
class test_3(Scene):
def construct(self):
text1 = Text('evsjv †jKAv', font='SutonnyMJ')
text2 = Text('english text', font='Arial').move_to(DOWN)
self.play(Write(text1), Write(text2))
self.wait()
Here I've replaced বাংলা লেখা with evsjv †jKAv (just ANSI form of the same Unicode text) which will render বাংলা লেখা as the font is now ANSI. I hope that Manim will support unicode fonts soon.
EDIT
I've found Bengali Unicode fonts to be working on Manim now. (24 March, 2021). I did this with Kalpurush font.
The code is
class FirstScene(Scene):
def construct(self):
text = Text("বাংলা অক্ষরে লেখা", font="Kalpurush")
text2 = Text("Another text")
self.play(Write(text), run_time=1)
self.wait(3)
self.remove(text)
self.play(Write(text2))
See the screenshot below,

MoviePy not displaying non-English characters

I'm trying to display non English characters in movie py but it's not displaying the actual character I typed. The language I'm trying is Telugu. What is the problem in displaying the characters?
This is the code I'm using
# -*- coding: utf-8 -*-
from moviepy.editor import *
# create clip from image
clip = ImageClip('img/1.jpg').on_color((1920, 1080))
clip = clip.set_duration(2)
# add annotation to clip
txtclip = TextClip('n+<ý² yûTq¿£yû', fontsize=50, color='red', font="Deepika")
cvc = CompositeVideoClip([ clip, txtclip.set_pos(('center', 'bottom'))])
cvc = cvc.set_duration(2)
# write video to file
cvc.write_videofile("text.mp4", fps=24)
The characters(Language) displayed in the code is weird but when I copy the text from the original file which was different characters displayed as this. And this worked in displaying the text in PySide QLabel.
Its just displaying boxes instead of the characters.
Can anyone help me with this issue?For your reference I'm adding image of text displayed in the code for language
I had a similar problem. I printed Japanese characters, but they were not displayed in video.
The problem was that the font did not support these characters.
Thus this produced empty result:
my_text = mp.TextClip("すみません。お先に失礼します",
font= "Amiri-regular", color= "white", fontsize= 34)
Solution was to download a custom font and import it as in example:
my_text2 = mp.TextClip("すみません。お先に失礼します",
font="wqy-microhei.ttc", color="white", fontsize=34)
I downloaded this font from github and it can be simply placed into directory with the python source code to be imported.

Telugu (Unicode) font not rendering correctly in pyfpdf

I am trying to render Telugu text into pdf using pyfpdf.
The problem is with font rendition in fpdf.
What might be the problem?
The code I used is :
#!/usr/bin/python
# -*- coding: utf8 -*-
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.add_font('lakkireddy', '', 'LakkiReddy.ttf', uni=True)
pdf.set_font('lakkireddy','', 16)
pdf.cell(40,10,u'హలో ప్రపంచమా!')
pdf.output('testfpdf.pdf','F')`
The expected output is :
But the actual output is broken text :
What could be the issue, Is it the font, Is it encoding or Is it font rendering engine?
Can I define which font rendering engine to use?
I am not familiar with fpdf but it seems that fpdf does not support proper text shaping for complex scripts. Glyph shapes change based on glyph position in the string and based on its neighbor glyphs but fpdf does not seem to do this kind of processing by default.
You have to check if there are options in fpdf for specifying this kind of processing for complex scripts.

ReportLab Django Not Rendering Chinese Characters

I'm having difficulty making ReportLab render Chinese Characters. From everything I've looked up people are saying that it is probably a font problem but I've used a lot of different fonts and it doesn't even seem to be using them at all. The Chinese characters always just come out as black squares. Below is some sample code of what I have.
# -*- coding: utf8 -*-
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase.ttfonts import TTFont
from io import BytesIO
pdfmetrics.registerFont(TTFont('Arial', 'arial.ttf', 'UTF-8'))
buffer = BytesIO()
doc = SimpleDocTemplate(buffer,
rightMargin=inch*0.5, # 1/2 Inch
leftMargin=inch*0.5, # 1/2 Inch
bottomMargin=0,
topMargin=inch*0.375, # 3/8 Inch
pagesize=letter)
# Get Styles
styles = getSampleStyleSheet()
# Custom Style
styles.add(ParagraphStyle(name='Address', font='Arial', fontSize=8))
elements = []
elements.append(Paragraph(u'6905\u897f\u963f\u79d1\u8857\uff0c\u5927\u53a6\uff03\u5927', styles['Address']))
doc.build(elements)
# Get the value of the BytesIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
return pdf
I'm using an arial.ttf font found on my Ubuntu 12.04 installation in the fonts folder. I have also tried other fonts installed on this machine and all have exactly the same look even on the numbers and none of the Chinese characters are anything other than black squares.
Am I registering fonts wrong if even the numbers at the beginning aren't printing correctly? What could be causing the black squares?
Solved it. Turns out in your ParagraphStyle it needs to be fontName="Arial" not font="Arial" but I did learn some other tricks of getting it to work in other ways below.
styles.add(ParagraphStyle(name='Address', fontName='Arial')
After doing some digging I've learned a few things that I hope helps someone else in this situation. When you add the tags inside of your Paragraph around the Unicode text and set it explicitly to a font it will work.
elements.append(Paragraph(u'<font name="Arial">6905\u897f\u963f\u79d1\u8857\uff0c\u5927\u53a6\uff03\u5927</font>', styles['Address']))
This fixes the problem at least for Paragraphs with various fonts.
Again this code will work.
Choose the fonts that supports Chinese characters.
In Ubuntu, I choose "AR PL UMing CN" for example.
My code snippets:
# -*- coding: utf-8 -*-
...
pdfmetrics.registerFont(TTFont('AR PL UMing CN', 'uming.ttc'))
styles = getSampleStyleSheet()
...
styles.add(ParagraphStyle(name='Chinese', fontName='AR PL UMing CN', fontSize=20))
elements=[]
elements.append(Paragraph("成”, styles['Chinese']))
doc.build(elements)
...
I can even change to Chinese editor and type in the character straight off. Hope this helps.

Reportlab: Asian fonts with encryption enabled does not work

I have been using Asian fonts (Chinese simplified/tradional, Japanese, and Korean) in my reportlab generated pdfs without issuse for a while now. However recently we have decided to enable the encryption options like this:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
pdfmetrics.registerFont(UnicodeCIDFont("STSong-Light"))
enc = pdfencrypt.StandardEncryption(
"", canPrint=1, canModify=0, canCopy=0, canAnnotate=0
)
self._Report = SimpleDocTemplate(
save_file,
topmargin=0.75*inch, bottommargin=0.75*inch,
rightmargin=0.70*inch, leftmargin=0.70*inch,
showBoundary=0,
author="xxx",
title="xxx",
subject=xxx",
encrypt=enc
)
For non Asian languages the encryption works as expected. When we use Asian fonts with encryption the pdf cannot be read by adobe reader. For example with simplified Chinese we get a "Cannot find the font "STSong-Light" error from Adobe Reader.
Anyone have any ideas on what encryption is breaking to not make this work?
I had the same problem. This might not solve your problem.
But, If I specify it into Japanese,
you can solve it by installing TTFont(in this example it is IPA gothic),
and set the font.
So, by installing another languages fonts, you can solve the problem though
this is not cool.
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter,A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
fontname = 'IPA Gothic'
pdfmetrics.registerFont(TTFont(fontname,'{directory that you put the font}/ipag.ttf'))
p = canvas.Canvas(response,pagesize=A4)
p.setFont(fonatname,13)
p.drawString(100,100,u'日本語,中国語,韓国語')
p.showPage()
p.save()

Categories