jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.33k stars 782 forks source link

the Chinese characters of bookmark outlines are garbled #307

Closed Guoozz closed 4 years ago

Guoozz commented 5 years ago

I want to generate PDF that contains bookmark outlines, but the Chinese characters are corrupted.

Test Code

func ExampleFpdf_Bookmark() {
    pdf := gofpdf.New("P", "mm", "A5", "")
    pdf.AddUTF8Font("SourceHanSerif", "", "SourceHanSerifCN-ExtraLight.ttf")
    pdf.SetFont("SourceHanSerif", "", 16)
    pdf.AddPage()
    pdf.Bookmark("目录1", 0, 0)
    pdf.Bookmark("子目录1", 1, -1)
    pdf.Cell(0, 6, "段落 1")
    pdf.Ln(50)
    pdf.Bookmark("目录2", 1, -1)
    pdf.Cell(0, 6, "段落 2")
    pdf.AddPage()
    pdf.Bookmark("目录3", 0, 0)
    pdf.Bookmark("Paragraph 3", 1, -1)
    pdf.Cell(0, 6, "Paragraph 3")
    fileStr := example.Filename("Fpdf_Bookmark")
    err := pdf.OutputFileAndClose(fileStr)
    example.Summary(err, fileStr)
    // Output:
    // Successfully generated pdf/Fpdf_Bookmark.pdf
}

Generated PDF

image

jung-kurt commented 5 years ago

Thanks for the report, @Guoozz. If I remember correctly, those bookmark names are UTF-16 encoded. I don't think the recent UTF-8 work included any conversion routines to UTF-16. I will look into this and hope others do too.

devplayg commented 5 years ago

The same problem occurs when entering Korean bookmarks.

func (r *Report) writeReport(pdf *gofpdf.Fpdf) error {
    pdf.AddPage()
    pdf.SetFont(HangulFont, "", 16)
    pdf.Bookmark("1. 커버", 0, 0)
    pdf.Cell(0, 6, "1. 커버")
SuddenGunter commented 5 years ago

@jung-kurt can I take it, is this a good first issue? I've not so much experience in Go

jung-kurt commented 5 years ago

can I take it

Absolutely! Thanks or your interest, @SuddenGunter!

is this a good first issue?

Possibly -- I haven't looked into to know how difficult it might be. You will want to study utf8toutf16() and .isCurrentUTF8. My assumption here is that the bookmark problem occurs only with UTF-8 fonts.

If this problem turns out to be too frustrating but you are still interested in tackling something else, let me know and I'll scan the list for another issue.

SuddenGunter commented 5 years ago

I was able to reproduce it locally. It took some time to install Chinese fonts and get it to work. Will debug it and try to fix, possibly this weekend. UPD: https://github.com/jung-kurt/gofpdf/blob/master/fpdf.go#L2389 - here you have checks that do this fix from utf8 to utf16. The fix itself occurs on https://github.com/jung-kurt/gofpdf/blob/master/fpdf.go#L2419. But this formatting happens only for content of pdf, I cannot find if you format bookmark text the same way with checks and conversions

UPD2: see pr https://github.com/jung-kurt/gofpdf/pull/310

jung-kurt commented 5 years ago

@SuddenGunter: Thanks for your work on this and for your updates!

SuddenGunter commented 5 years ago

@Guoozz @devplayg plz check latest version: was issue resolved for you?

Guoozz commented 5 years ago

@SuddenGunter the problem has resolved, thanks very much.

SuddenGunter commented 5 years ago

@jung-kurt can we close this issue now?

jung-kurt commented 5 years ago

can we close this issue now?

Yes, you may have the honors. Thanks again, @SuddenGunter.

SuddenGunter commented 4 years ago

Yes, you may have the honors

but I don't have required rights in repo :) do it