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

Garbled error when set chinese in outline #516

Closed
suninuni opened this issue Jul 6, 2020 · 2 comments
Closed

Garbled error when set chinese in outline #516

suninuni opened this issue Jul 6, 2020 · 2 comments

Comments

@suninuni
Copy link

suninuni commented Jul 6, 2020

Hi @Hopding ,

I just tried to use chinese as outline item title(refer to #127 (comment)), but i got some garbled strings, e.g

image

Actually in the code, some of them are:

image

So, i found a pdf which has chinese in outline and print it in the console. They are also garbled strings, such as þÿ{,\\u00001zà\\u0000 g. But when i set þÿ{,\\u00001zà\\u0000 g as a outline item title, i got 第一章 in the result pdf's outline which is display in the right way.

Is there any suggestion to resole this?

Thank you.

Some informations which may be helpful:

@inetfuture
Copy link

@Hopding any suggestions on this? thanks.

@suninuni
Copy link
Author

https://tex.stackexchange.com/questions/236863/incorrect-encoding-in-pdf-outlines/237277#237277

According this, i found a function utf16Encode() in

export const utf16Encode = (
input: string,
byteOrderMark = true,
): Uint16Array => {

After this, i found PDFHexString.fromText() which used this function

static fromText = (value: string) => {
const encoded = utf16Encode(value);
let hex = '';
for (let idx = 0, len = encoded.length; idx < len; idx++) {
hex += toHexStringOfMinLength(encoded[idx], 4);
}
return new PDFHexString(hex);
};

So i resolved this garbled problem by dict.set(PDFName.Title, title ? PDFHexString.fromText(title) : title);

And i saw you are working at #486 which about PDFOutline, so i recommend using PDFHexString.fromText instead of PDFString.of when set PDFName.Title in https://github.com/Hopding/pdf-lib/pull/486/files#diff-cf25ad19a97bf84bda22a1a3621dbdf1R28

Thank you anyway.

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

2 participants