ccharacter-encodinglibharu

Encoding in pdf with libharu library


I'm trying to create a pdf document with help of libharu library version 2.3.
Everything goes fine except for encoding. I need to insert Central European language (Czech). Source code saved as utf-8 and compiled under Linux gcc.
In function HPDF_GetFont() I've tried all of single byte encodings and all of buildin fonts and fonts from file with utf-8 support. But that's not the way to success.

What should I change in the code below? Should I save text "abcdÁéěňóšťíď" to wchar_t* and then convert it to singlebyte somehow?
Thanks for advice.

void write_to_pdf(char *file_name)
{
    HPDF_Doc pdf;
    HPDF_Page page;
    HPDF_Font font;

    pdf = HPDF_New (NULL, NULL);
    if (!pdf) 
    {
        printf ("ERROR: cannot create pdf object.\n");
        return false;
    }

    HPDF_UseUTFEncodings(pdf);

    font = HPDF_GetFont(pdf, HPDF_LoadTTFontFromFile(pdf, "arial.ttf", HPDF_TRUE), "StandardEncoding");
    page = HPDF_AddPage(pdf);
    HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
    HPDF_Page_SetTextRenderingMode(page, HPDF_FILL);
    HPDF_Page_SetRGBFill(page, 0.0, 0.0, 0.0);

    HPDF_Page_SetFontAndSize(page, font, 11);
    HPDF_Page_BeginText(page);
    HPDF_Page_TextOut(page, 10, 20, "abcdÁéěňóšťíď");
    HPDF_Page_EndText(page);

    HPDF_SaveToFile (pdf, file_name);
    HPDF_Free (pdf);
}

In pdf file characters "Áéěňóšťíď" are not printed correctly.


Solution

  • I solved problem by adding:
    HPDF_SetCurrentEncoder(pdf, "UTF-8")
    and UTF-8 to fnc:
    HPDF_GetFont(pdf, HPDF_LoadTTFontFromFile(pdf, "arial.ttf", HPDF_TRUE), "UTF-8")

    I did not find this in documentation but here. Maybe doc is not uptodate.