简体   繁体   English

从 jsPDF 生成的 PDF 复制文本时粘贴乱码

[英]Pastes gibberish when copied text from a PDF generated from jsPDF

Well I needed to use a font which supports unicode characters to generate PDFs from jsPDF in order to serve my purpose.好吧,我需要使用支持 unicode 字符的字体从 jsPDF 生成 PDF 以达到我的目的。 For example the '€' sign since it was generating a gibber when jsPDF inbuild fonts were used as it does not support unicode chars.例如“€”符号,因为它在使用 jsPDF 内置字体时会产生乱码,因为它不支持 unicode 字符。

However good news is you can use custom true type fonts(ttf) and a lot ttf support unicode chars and my issue above is solved.不过好消息是你可以使用自定义的真字体(ttf)和很多 ttf 支持 unicode 字符,我上面的问题已经解决了。

Now bad news or the next side effect is after generating the pdf if someone tries to copy the exact text from the pdf and wants to paste it somewhere else like text file, word document etc., generates this:现在坏消息或下一个副作用是在生成 pdf 之后,如果有人试图从 pdf 中复制确切的文本并希望将其粘贴到其他地方,如文本文件、word 文档等,则生成以下内容:

"ࣆࣆGROODUGROODUORWRIGROODUࣆࣆKDKDLDPULFK" "ࣆࣆGROODUGROODUORWRIGROODUࣆࣆKDKDLDPULFK"

instead of:代替:

"€€ euro euro lot of euro €€ - haha i am rich ..." “€€€€€€€€€€€€€€€——哈哈,我很有钱……”

we tried all sorts of workaround with the acrobat pdf settings.我们尝试了各种使用 acrobat pdf 设置的解决方法。 Nothing works in particular.没有什么特别有效的。 Downloaded and tried a lot of different fonts but no solution.下载并尝试了很多不同的字体,但没有解决方案。

jspdf version is 1.4.1. jspdf 版本是 1.4.1。

Any idea why this is happenening or any solution for this ?知道为什么会发生这种情况或对此有任何解决方案吗?

Any help will be so grateful.任何帮助将不胜感激。

Thanks.谢谢。

Well, the issue was with jspdf version.好吧,问题出在 jspdf 版本上。 This problem was solved in 1.5.0 and the version that i was using was 1.4.1这个问题在 1.5.0 中解决了,我使用的版本是 1.4.1

See release notes: https://github.com/MrRio/jsPDF/releases查看发行说明: https : //github.com/MrRio/jsPDF/releases

Move to: commit "c53beb6": Font Dictionary now has toUnicode entry to be able to copy paste out from pdf viewer.移至:commit "c53beb6":字体字典现在具有 toUnicode 条目,以便能够从 pdf 查看器中复制粘贴。

Edit: I tried to recreate your problem.编辑:我试图重现你的问题。 I used jsPDF as you did jsPDF and I got it to work pretty fast.我用jsPDF像你一样jsPDF ,我得到了它相当快的工作。 Please tell me if this is what you wanted.请告诉我这是否是您想要的。

Here is a working example codepen这是一个工作示例代码笔

 var doc = new jsPDF() doc.text('€€Hello world!€€', 10, 10); function save() { doc.save('a4.pdf'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/" crossorigin="anonymous"></script> <button onclick="save()">Click me to download the PDF</button>


I first suggested that you implement a cmap but this would be too much because you would need to remap every character as described here Implement a cmap table with a cmap table.我首先建议您实现一个 cmap 但这太多了,因为您需要按照此处所述重新映射每个字符 用 cmap 表实现cmap 表。

Just try out my above code, I hope it helps and otherwise let me know if there are further problems.试试我上面的代码,我希望它有帮助,否则让我知道是否有进一步的问题。


Note that the text from the created PDF also shows the euro sign so this should basically fix your problem请注意,创建的 PDF 中的文本还显示了欧元符号,因此这基本上应该可以解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM