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

[BUG] SetFont(simsun.ttf) doesn't working for chinese font #114

Closed
researchlab opened this issue Jul 9, 2019 · 13 comments
Closed

[BUG] SetFont(simsun.ttf) doesn't working for chinese font #114

researchlab opened this issue Jul 9, 2019 · 13 comments

Comments

@researchlab
Copy link

researchlab commented Jul 9, 2019

I want to display chinese font normally by using SetFont Method to set the font of simsun.ttf or NotoSansSC-Regular.ttf which support display chinese font normally,

but it's doesn't working. so anyone cloud help me how to display chinese font normally ?

Here's the code for my testing,

func RunPdfReport(outputPath string) error {
	//robotoFontRegular, err := model.NewPdfFontFromTTFFile("./NotoSansSC-Regular.ttf")
	robotoFontRegular, err := model.NewPdfFontFromTTFFile("./simsun.ttf")
	if err != nil {
		return err
	}
	robotoFontPro, err := model.NewPdfFontFromTTFFile("./NotoSansSC-Regular.ttf")
	if err != nil {
		return err
	}
	c := creator.New()
	c.SetPageMargins(50, 50, 100, 70)
	
	c.CreateFrontPage(func(args creator.FrontpageFunctionArgs) {
		DoFirstPage(c, robotoFontRegular, robotoFontPro)
	})

	return c.WriteToFile(outputPath)

}
func DoFirstPage(c *creator.Creator, fontRegular *model.PdfFont, fontBold *model.PdfFont) {
	p := c.NewParagraph("中文检测")
	p.SetFont(fontRegular)
	p.SetFontSize(48)
	p.SetMargins(85, 0, 150, 0)
	p.SetColor(creator.ColorRGBFrom8bit(56, 68, 77))
	c.Draw(p)

	p = c.NewParagraph("Example Report")
	p.SetFont(fontRegular)
	p.SetFontSize(30)
	p.SetMargins(85, 0, 0, 0)
	p.SetColor(creator.ColorRGBFrom8bit(45, 148, 215))
	c.Draw(p)
}
@researchlab researchlab changed the title [BUG] SetFont(simsun.ttf) doesn't work for chinese font [BUG] SetFont(simsun.ttf) doesn't working for chinese font Jul 9, 2019
@rogchap
Copy link

rogchap commented Aug 24, 2020

How was this solved?

@researchlab

This comment was marked as off-topic.

@rogchap

This comment has been minimized.

@gunnsth
Copy link
Contributor

gunnsth commented Aug 27, 2020

This is an outdated question. Font support has been completely revamped over the least year and font subsetting added as well.

@gunnsth
Copy link
Contributor

gunnsth commented Aug 27, 2020

Please keep the discussion focused on topic with respect to the library. This is something that we would like the library to support. To discuss other methods please seek other discussion forums and such.

@gunnsth
Copy link
Contributor

gunnsth commented Aug 27, 2020

The issue with the code in the issue is that it uses NewPdfFontFromTTFFile instead of NewCompositePdfFontFromTTFFile
The function NewPdfFontFromTTFFile only handles simple encodings, whereas NewCompositePdfFontFromTTFFile can use symbolic fonts.
It is also worth pointing out oftentimes symbolic fonts can be very big so it can make sense to enable subsetting in the output. If using creator package that can be achieved with c.EnableFontSubsetting(font). Otherwise can use the font's SubsetRegistered.

By using NewCompositePdfFontFromTTFFile with the code posted above (and fonts that can be found by searching).
output.pdf, which is 5.6MB.
Then by enabling font subsetting, we get:
which is
output_subset.pdf which is 15kB. So it makes a pretty big difference.

@carmel
Copy link

carmel commented May 27, 2024

image

Chinese characters will be crowded together, can this be solved?
@gunnsth

@sampila
Copy link
Collaborator

sampila commented May 27, 2024

Hi @carmel, could you provide a runnable code snippet? by proving runnable code snippet we can further investigate the issue.

However, we tried to reproduce the issue by modifying the code in this issue.

Code

package main

import (
	"fmt"
	"os"

	"github.com/unidoc/unipdf/v3/common"
	"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 {
		fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
		fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
		panic(err)
	}
	common.SetLogger(common.NewConsoleLogger(common.LogLevelDebug))
}

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("Usage: go run main.go <output.pdf>\n")
		os.Exit(1)
	}

	outputPath := os.Args[1]
	err := RunPdfReport(outputPath)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("PDF report written to %s\n", outputPath)
}

func RunPdfReport(outputPath string) error {
	fontRegular, err := model.NewCompositePdfFontFromTTFFile("./fonts/simsun.ttf")
	if err != nil {
		return err
	}
	fontBold, err := model.NewCompositePdfFontFromTTFFile("./fonts/NotoSansSC-Regular.ttf")
	if err != nil {
		return err
	}
	c := creator.New()
	c.SetPageMargins(50, 50, 100, 70)

	c.CreateFrontPage(func(args creator.FrontpageFunctionArgs) {
		DoFirstPage(c, fontRegular, fontBold)
	})

	return c.WriteToFile(outputPath)

}
func DoFirstPage(c *creator.Creator, fontRegular *model.PdfFont, fontBold *model.PdfFont) {
	p := c.NewParagraph("中文检测")
	p.SetFont(fontRegular)
	p.SetFontSize(48)
	p.SetMargins(85, 0, 150, 0)
	p.SetColor(creator.ColorRGBFrom8bit(56, 68, 77))
	c.Draw(p)

	p = c.NewParagraph("Example Report")
	p.SetFont(fontRegular)
	p.SetFontSize(30)
	p.SetMargins(85, 0, 0, 0)
	p.SetColor(creator.ColorRGBFrom8bit(45, 148, 215))
	c.Draw(p)
}

Results

Screenshot 2024-05-28 at 01 58 08

output.pdf

Probably you can try to update the UniPDF to the latest version and checks if the issue still appears.

@carmel
Copy link

carmel commented May 28, 2024

I've found that it has to do with fonts, and the above problem occurs with Microsoft YaHei, but not with SimFang.

@sampila
Copy link
Collaborator

sampila commented May 28, 2024

@carmel here the results when I test with Microsoft Yahei font
Screenshot 2024-05-28 at 08 47 45

output.pdf

It's hard to reproduce the issue by guessing, if you could provide runnable code snippet if would be good

@carmel
Copy link

carmel commented May 28, 2024

Thanks for the example, maybe it's because I'm using a low version of it.

@sampila
Copy link
Collaborator

sampila commented May 28, 2024

Thanks for the example, maybe it's because I'm using a low version of it.

We are happy able to help, let us know if you still having the issue by sharing runnable code snippet 🙂

Best regards

@gunnsth
Copy link
Contributor

gunnsth commented May 28, 2024

Please open a new issue if the problem reoccurs.

@unidoc unidoc locked and limited conversation to collaborators May 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants