Search code examples
c++pdflibharu

Cannot write texts in a PDF file using libHaru


The following code creates a PDF file containing two words and saves it:

#include "hpdf.h"
#include <iostream>

void error_handler (HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data) {
    std::cerr << "error! " << error_no << " " << detail_no << '\n';
    throw std::exception();
}

int main() {
    HPDF_Doc pdf = HPDF_New (error_handler, NULL);
    if (!pdf) {
        std::cerr << "Error: Cannot create pdf object" << '\n';
        return 1;
    }
    try {
        HPDF_Page page = HPDF_AddPage(pdf);
        HPDF_Page_BeginText (page);
        HPDF_Page_TextOut (page, 0,0,"sample text");
        HPDF_Page_EndText(page);
        HPDF_SaveToFile (pdf, "test.pdf");
        std::cout << "file save: " << '\n';
    } catch (const std::exception& e) {
        e.what();
        HPDF_Free(pdf);
        return 1;
    }
    HPDF_Free(pdf);
}

When this line:

HPDF_Page_TextOut (page, 0,0,"sample text");

is executed, I got:

error! 4174 0

I can't find anything about error code 4174 in the libHaru documentation, can anyone explain what this means?


Solution

  • Your error handler outputs error codes in decimal while the documentation lists them as hexadecimal.

    4174 (decimal) equals to 0x104e (hex).

    This error code is listed in the libharu documentation as HPDF_PAGE_FONT_NOT_FOUND.

    Refer to the documentation on how to fix this.