tomoki1207 / vscode-pdfviewer

Show PDF preview in VSCode.
https://marketplace.visualstudio.com/items/tomoki1207.pdf
Other
295 stars 70 forks source link

Support Unicode in PDF by passing the right cMapUrl to pdf.js #116

Closed aifreedom closed 2 years ago

aifreedom commented 3 years ago

This PR fixes the bug that Unicode characters can't be rendered in vscode-pdfviewer by setting the right cMapUrl option of the PDF viewer.

cmaps are short for "character maps" and it's required by the core when rendering Unicode characters in PDF. By default, the option's value is ../web/cmaps (code). This will work if we are using the default web/viewer.html packaged in the dist of pdf.js. But because the PDF viewer is rendered inside an iframe in VS Code, ../web/cmaps isn't the right relative path for the cmaps. To fix the bug, we need to set the right cmap URL before rendering the PDF.

The PDF viewer has an option cMapUrl which tells it where to find the cmap files (ref). We can get the URL of the cmaps in pdfPreview.ts and set the option on load.

Because pdf.js expects the option to have trailing slash, the trailing slash after cmaps when generating the URL is required.

Tested with a few PDFs with Chinese characters and they are rendered without problems.

image

tomoki1207 commented 2 years ago

Great work! Thanks and sorry my late response.