开发者

How to draw Thai text to PDF file by using libharu library

开发者 https://www.devze.com 2023-01-10 03:46 出处:网络
i am using free pdf library libh开发者_JAVA百科aru to generate PDF file, but i have a encoding problem, i can not draw Thai lanugage text on PDF file,

i am using free pdf library libh开发者_JAVA百科aru to generate PDF file, but i have a encoding problem, i can not draw Thai lanugage text on PDF file, all the text shows "???.."

Somebody know how to fix it? Thanks


I have succeeded in rendering hieroglyphic texts (not Thai, but Chinese and Japanese) using libharu. First of all, I used Unicode mode, please refer to HPDF_UseUTFEncodings() function documentation.

For C language, here is a sequence of libharu API calls needed to overcome your trouble:

HPDF_UseUTFEncodings(docHandle);
HPDF_SetCurrentEncoder(docHandle, "UTF-8");

Here docHandle is a valid HPDF_Doc object.

Next part is proper work with UTF fonts:

const char * libFontName = HPDF_LoadTTFontFromFile(docHandle, fontFileName.c_str(), font_embed::EmbedFonts);
HPDF_Font font = HPDF_GetFont(docHandle, libFontName, "UTF-8");

After these calls you may render unicode texts containing Thai characters. Also note about embedding flag (3rd param of LoadTTFontFromFile) - your PDF file may be unreadable due to external font references. If you are not crazy with output PDF size, you may just embed fonts.

I've tested couple of Thai .ttf fonts found in Google and they were rendered OK. Also (it may be important, but I'm not sure) I'm using fork of libharu https://github.com/kdeforche/libharu which is now merged into master branch.


When you write text to the PDF, use the correct font and encoding. In the libharu documentation you have all the possibilities: https://github.com/libharu/libharu/wiki/Fonts

In your case, you must use the ISO8859-11 Thai, TIS 620-2569 character set

An example (in spanish):

    HPDF_Font fontEn = HPDF_GetFont(pdf, "Helvetica-Bold", "ISO8859-2");
    HPDF_Page_TextOut(page1, 50.00, 750.00, [@"Código para correcta codificación en libharu" cStringUsingEncoding:NSISOLatin1StringEncoding]);
0

精彩评论

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