1. Ross Light
  2. gopdf
  3. Issues
Issue #5 new

Text doesn't handle non-ASCII characters

Otto Bretz
created an issue

Thanks for a very nice library!

doc := pdf.New()
canvas := doc.NewPage(pdf.A4Width, pdf.A4Height)

canvas.Translate(pdf.A4Width/2, pdf.A4Height/2)
text := new(pdf.Text)
text.SetFont(pdf.Helvetica, 24)

err := doc.Encode(os.Stdout)
if err != nil {
    fmt.Fprintln(os.Stderr, err)

I have attached a screenshot of the output that I'm getting. I expected something more like ÅÄÖåäö :)

Comments (7)

  1. Ross Light repo owner

    This is a known problem. The PDF spec has a different text encoding for non-ASCII characters and I just haven't gotten around to writing the conversion function.

    I've gotten quite a few requests about this, so I'll try to patch it this week.

  2. Anonymous

    A possible encoding is Windows-1252. An example for a windows-1252 conversion function I am using in a PDF backend is http://play.golang.org/p/na4RZMYO-s

    In this case the font's (exception: Symbol and ZapfDingbats) dictionary would need an entry /Encoding /WinAnsiEncoding

  3. bouticfactory


    I've solved the problem. It might not be the ideal solution, but here it is:

    1) In gopdf

    • I added a "/Encoding /MacRomanEncoding" to each stream to ensure that this particular mapping is used.

    2) new package

    • I created an encoding/macroman package that encodes/decodes streams from/to utf-8 from/to Mac OS roman

    3) in gopdf

    • I call macroman.Encode() in text.Text()

    I tried to generate pdf files that way and I got the accents!

    Here are the paths to the packages on github: https://github.com/bouticfactory/encoding https://github.com/bouticfactory/gopdf

  4. Log in to comment