开发者

Reportlab: Asian fonts with encryption enabled does not work

开发者 https://www.devze.com 2023-02-18 02:11 出处:网络
I have been using Asian fonts (Chinese simplified/tradional, Japanese, and Korean) in my reportlab generate开发者_高级运维d pdfs without issuse for a while now. However recently we have decided to ena

I have been using Asian fonts (Chinese simplified/tradional, Japanese, and Korean) in my reportlab generate开发者_高级运维d 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() 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号