foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.92k stars 1.16k forks source link

Broken PDF output when mixing English and Simplified Chinese using Noto font #1357

Open lrmunoz opened 2 years ago

lrmunoz commented 2 years ago

Bug Report

Description of the problem

I'm using one of the official Noto fonts for Simplified Chinese, downloaded from: https://github.com/googlefonts/noto-cjk/blob/main/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Regular.otf

When I call PDFDocument.text with a string that mixes English with Simplified Chinese, subsequent calls to PDFDocument.text with English strings produced garbled text:

Screen Shot 2022-04-06 at 10 43 33

Code sample

Link to minimal-kit fork: https://replit.com/@lrmunoz/minimal-pdfkit-issue-1357

Code:

const PDFDocument = require('pdfkit')
const fs = require('fs')

const doc = new PDFDocument()

doc.pipe(fs.createWriteStream('output.pdf'))

const offset = 75
let idx = 2

doc
  .font('NotoSansCJKsc-Regular.otf')
  .fontSize(18)
  .text('English 中国产品', 100, offset * idx++)
  .text('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 100, offset * idx++)

doc.end()

Your environment

lrmunoz commented 2 years ago

@blikblum @devongovett At my company, we're interested in funding the fix for this issue, please ping me if interested: luis at finaleinventory dot com

summer-wu commented 2 years ago

I am facing the same issue, version 0.13.0

hand-dot commented 1 year ago

Hi @lrmunoz

Maybe It's font file problem.

I'm the author of pdfme, and I have the same issue at pdfme (pdfme has fontkit dependency internally)

I don't have deep knowledge about font and fonkit, but I found that If we use a big font file(Chinese, Japanese), we have to use ttf font file.

So, I checked Simplified Chinese Noto font as ttf. You can download it here

Finally, I got the correct PDF.

CleanShot 2023-01-18 at 11 09 15@2x

Link to minimal-kit fork: https://replit.com/@handdot/minimal-pdfkit-issue-1357?v=1

Thanks.