JsPDF不支持日语
问题描述:
我正在将JsPDF用于我的React项目,并且在将pdf保存为日文版本时遇到了一些问题,但是它在英语版本中可以正常工作.
I am using JsPDF for my react project and facing some issues while saving pdf for Japanese version but it is working fine with English version.
问题有时它会打印一些随机的特殊字符,有时它在pdf上什么也不打印.
Issues Sometimes it is printing some random special chracters and sometimes it prints nothing on pdf.
任何帮助将不胜感激.
这是我的代码
import React from "react";
import jsPDF from 'jspdf';
import "./styles.css";
const HelloWorldJapanese = 'こんにちは世界';
export default function App() {
const downloadPdf = () => {
const doc = new jsPDF()
doc.text('Hello world!', 10, 10)
doc.save('a4.pdf')
}
const downloadJapanesePDF = () => {
const doc = new jsPDF();
doc.text(HelloWorldJapanese, 10, 10)
doc.save('a4.pdf');
}
return (
<div className="App">
<button onClick={downloadPdF}>Download Pdf</button>
<br />
<button onClick={downloadJapanesePDF}>Download Japanese Pdf</button>
</div>
);
}
SandBox演示: https://codesandbox.io/s/jspdf-bk7p3
SandBox Demo: https://codesandbox.io/s/jspdf-bk7p3
答
您必须手动添加字体文件.例如,在jsPDF中 git
You have to add the font-file manually. For instance, in jsPDF git
doc.addFont("test/reference/MouhitsuBold.ttf", "Mouhitsu", "bold");
doc.setFont("Mouhitsu", "bold"); // set font
我在小故障中添加了代码,以方便演示(预览pdf,故障仅在新窗口中显示时起作用)
I added code in glitch for easy demonstrate (preview pdf in glitch only work on show in new window)