klippa-app / go-pdfium

Easy to use PDF library using Go and PDFium
MIT License
195 stars 16 forks source link

Something wrong with FPDFText_SetText(), Chinese not support? #186

Closed Jiaget closed 3 months ago

Jiaget commented 3 months ago

I want to add some text in my pdf, and here is my code

data, err := os.ReadFile("./font/DroidSansFallbackFull.ttf")
    if err != nil {
        return err
    }

    font, err := instance.FPDFText_LoadFont(&requests.FPDFText_LoadFont{
        Document: doc.Document,
        Data:     data,
        FontType: enums.FPDF_FONT_TRUETYPE,
    })
    if err != nil {
        return err
    }

    obj, err := instance.FPDFPageObj_CreateTextObj(&requests.FPDFPageObj_CreateTextObj{
        Document: doc.Document,
        Font:     font.Font,
        FontSize: 12,
    })
    if err != nil {
        return err
    }

    if err != nil {
        return err
    }

    _, err = instance.FPDFPageObj_SetFillColor(&requests.FPDFPageObj_SetFillColor{
        PageObject: obj.PageObject,
        FillColor: structs.FPDF_COLOR{
            A: 255,
        },
    })
    if err != nil {
        return err
    }
    text := "abc~!@测试文字"

    _, err = instance.FPDFText_SetText(&requests.FPDFText_SetText{
        PageObject: obj.PageObject,
        Text:       text,
    })
    if err != nil {
        return err
    }
    _, err = instance.FPDFPageObj_Transform(&requests.FPDFPageObj_Transform{
        PageObject: obj.PageObject,
        Transform: structs.FPDF_FS_MATRIX{
            A: 1,
            B: 0,
            C: 0,
            D: 1,
            E: 100,
            F: 700,
        },
    })
    if err != nil {
        return err
    }

    _, err = instance.FPDFPage_InsertObject(&requests.FPDFPage_InsertObject{
        PageObject: obj.PageObject,
        Page: requests.Page{
            ByIndex: &requests.PageByIndex{
                Document: doc.Document,
                Index:    0,
            },
        },
    })
    if err != nil {
        return err
    }

    _, err = instance.FPDFPage_GenerateContent(&requests.FPDFPage_GenerateContent{
        Page: requests.Page{
            ByIndex: &requests.PageByIndex{
                Document: doc.Document,
                Index:    0,
            },
        },
    })
    if err != nil {
        return err
    }

    _, err = instance.FPDF_SaveAsCopy(&requests.FPDF_SaveAsCopy{
        Flags:    requests.SaveFlagRemoveSecurity,
        Document: doc.Document,
        FilePath: &outPath,
    })
    if err != nil {
        return err
    }

And I got image

jerbob92 commented 3 months ago

Hey! I think the trick is adding CID: true, to FPDFText_LoadFont. With DroidSansFallbackFull then only the Chinese characters show up, but when I use NotoSansCJK all characters show up.

Jiaget commented 3 months ago

Hey! I think the trick is adding CID: true, to FPDFText_LoadFont. With DroidSansFallbackFull then only the Chinese characters show up, but when I use NotoSansCJK all characters show up.

Thanks for your work, that really helps