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

custom font font subset with Chinese font does not work properly #494

Closed
rawbin- opened this issue Jun 17, 2020 · 5 comments
Closed

custom font font subset with Chinese font does not work properly #494

rawbin- opened this issue Jun 17, 2020 · 5 comments

Comments

@rawbin-
Copy link

rawbin- commented Jun 17, 2020

ref for #207
and example for #430

using the subset option to reduce the generated pdf size, when use subset, the pdf content is not expected

use https://github.com/rawbin-/node-pdf-test to reproduce this issue
with-pdflib.js and with-pdflib-issue-430.js

expected
image

but got
image

or even blank with issue-430 example

@rawbin-
Copy link
Author

rawbin- commented Jun 17, 2020

this example will show https://github.com/rawbin-/node-pdf-test/blob/master/pdflib-test.js
when custom font use subset ,the original english font also affected

just change the following to see the difference

    // this line works well
//     const customFont = await pdfDoc.embedFont(fontBytes)

    // this does not work properly
    const customFont = await pdfDoc.embedFont(fontBytes,{subset:true})
const fs = require('fs')
const { PDFDocument, rgb } = require('pdf-lib')
const fontkit = require('@pdf-lib/fontkit')

async function generatePDF(){
    // This should be a Uint8Array or ArrayBuffer
// This data can be obtained in a number of different ways
// If you're running in a Node environment, you could use fs.readFile()
// In the browser, you could make a fetch() call and use res.arrayBuffer()
    const fontBytes = fs.readFileSync('./font/pingfang.ttf')

// Create a new PDFDocument
    const pdfDoc = await PDFDocument.create()

// Register the `fontkit` instance
    pdfDoc.registerFontkit(fontkit)

// Embed our custom font in the document
    // this line works well
//     const customFont = await pdfDoc.embedFont(fontBytes)

    // this does not work properly
    const customFont = await pdfDoc.embedFont(fontBytes,{subset:true})


// Add a blank page to the document
    const page = pdfDoc.addPage()

// Create a string of text and measure its width and height in our custom font
    const text = '中文字体你行不行This is text in an embedded font!'
    const textSize = 35
    const textWidth = customFont.widthOfTextAtSize(text, textSize)
    const textHeight = customFont.heightAtSize(textSize)

// Draw the string of text on the page
    page.drawText(text, {
        x: 40,
        y: 450,
        size: textSize,
        font: customFont,
        color: rgb(0, 0.53, 0.71),
    })

// Draw a box around the string of text
    page.drawRectangle({
        x: 40,
        y: 450,
        width: textWidth,
        height: textHeight,
        borderColor: rgb(1, 0, 0),
        borderWidth: 1.5,
    })

// Serialize the PDFDocument to bytes (a Uint8Array)
    const pdfBytes = await pdfDoc.save()
    fs.writeFileSync('./dist/pdflib-test.pdf',pdfBytes)

// For example, `pdfBytes` can be:
//   • Written to a file in Node
//   • Downloaded from the browser
//   • Rendered in an <iframe>
}

generatePDF()

@Hopding
Copy link
Owner

Hopding commented Sep 20, 2020

Hello @rawbin-!

As you have noticed, font subsetting unfortunately does not work with all types of fonts. The library pdf-lib uses for custom embedding and subsetting is @pdf-lib/fontkit, which is a fork of fontkit. I've noticed issues myself when attempting to subset certain fonts. I haven't had time to track it down, which is why subsetting is not the default behavior for pdf-lib.

As best I can tell, the issue exists upstream in fontkit. If so, there's nothing that can be done to pdf-lib directly to fix it. So it would be best to open an issue on that repo. If/when it gets fixed, it can be pulled into @pdf-lib/fontkit (and, therefore, pdf-lib).

One way you could attempt to verify that the issue exists in fontkit is by trying to embed and subset the font with pdfkit. pdfkit also uses fontkit internally, so if you can reproduce the issue that way, it's highly likely to be a bug in fontkit.

@Hopding
Copy link
Owner

Hopding commented Sep 26, 2020

I'm going to close this issue for now. It only manifests when users choose to subset a font and does not appear to be an issue with pdf-lib specifically. It is also unlikely to be fixed in the near future unless an enterprising individual decides to work on it.

@znacloud
Copy link

I published a new package renamed to pdf-fontkit to fix the subsetting issues. I've tested it and it works fine. Maybe you can use this package to replace @pdf-lib/fontkit.

@Betree
Copy link

Betree commented Apr 11, 2024

@znacloud Thanks for sharing your solution, it works like a charm! It would be great to get this integrated into the main project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants