JsPDF不支持日语 [英] JsPDF not supporting Japanese language

查看:112
本文介绍了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)

这篇关于JsPDF不支持日语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆