Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[US-395] Add example code for multifont encoder #246

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added multi-font-encoder/fonts/Batang.ttf
Binary file not shown.
Binary file not shown.
Binary file added multi-font-encoder/fonts/OpenSans-Regular.ttf
Binary file not shown.
Binary file added multi-font-encoder/fonts/PCSB Hebrew Regular.ttf
Binary file not shown.
Binary file not shown.
61 changes: 61 additions & 0 deletions multi-font-encoder/render_multi_language_text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* renders a multi language text using a multi font encoder.
*
* Run as: go run render_multi_language_text.go
*/
package main

import (
"os"

"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/creator"
"github.com/unidoc/unipdf/v3/model"
)

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}

func main() {
inputText := "Sample text 示例文本 טקסט לדוגמה ጽሑፍ ኣብነት 샘플 텍스트"
fonts := getFonts([]string{
"./fonts/PCSB Hebrew Regular.ttf",
"./fonts/NotoSerifEthiopic_Condensed-Black.ttf",
"./fonts/OpenSans-Regular.ttf",
"./fonts/Batang.ttf",
})

c := creator.New()
c.SetPageSize(creator.PageSizeA5)
p := c.NewStyledParagraph()
style := &p.Append(inputText).Style
style.MultiFont = model.NewMultipleFontEncoder(fonts)

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.22

style.MultiFont undefined (type *creator.TextStyle has no field or method MultiFont)

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.22

undefined: model.NewMultipleFontEncoder

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.21

style.MultiFont undefined (type *creator.TextStyle has no field or method MultiFont)

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.21

undefined: model.NewMultipleFontEncoder

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.20

style.MultiFont undefined (type *creator.TextStyle has no field or method MultiFont)

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.20

undefined: model.NewMultipleFontEncoder

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.19

style.MultiFont undefined (type *creator.TextStyle has no field or method MultiFont)

Check failure on line 38 in multi-font-encoder/render_multi_language_text.go

View workflow job for this annotation

GitHub Actions / Build Go 1.19

undefined: model.NewMultipleFontEncoder

err := c.Draw(p)
if err != nil {
panic(err)
}
if err := c.WriteToFile("multiple-language-text-multi-font.pdf"); err != nil {
panic(err)
}
}

// getFonts returns list of *model.PdfFont from list of paths to fonts files.
func getFonts(fontPaths []string) []*model.PdfFont {
fonts := []*model.PdfFont{}
for _, path := range fontPaths {
font, err := model.NewCompositePdfFontFromTTFFile(path)
if err != nil {
panic(err)
}
fonts = append(fonts, font)
}

return fonts
}
Loading