找回密码
 立即注册

QQ登录

只需一步,快速开始

Polariscbcbcbc

注册会员

26

主题

67

帖子

191

积分

注册会员

积分
191
Polariscbcbcbc
注册会员   /  发表于:2023-3-27 17:59  /   查看:1124  /  回复:4
1金币
看了论坛中很多文章,使用了方法但是还是乱码

代码如下

//注册PDF导出字体
const registerServerFont = (name, type, serverPath) => {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', serverPath, true);
  xhr.responseType = 'blob';
  xhr.onload = function (e) {
    console.log(e)
    if (e.target.status == 200) {
      var blob = e.target.response;
      var reader = new FileReader();
      reader.onload = function (e) {
        var fontrrayBuffer = reader.result;
        var fonts = GC.Spread.Sheets.PDF.PDFFontsManager.getFont(name) || {};
        fonts[type] = fontrrayBuffer;
        GC.Spread.Sheets.PDF.PDFFontsManager.registerFont(name, fonts);
      };
      reader.readAsArrayBuffer(blob);
    }
  };

  xhr.send();
}
registerServerFont('微软雅黑', 'normal', 'src/utils/font/msyh.ttf');
registerServerFont('Microsoft Yahei', 'normal', 'src/utils/font/msyh.ttf');
registerServerFont('simsun', 'normal', 'src/utils/font/simsun.ttf');
registerServerFont('simsun', 'bold', 'src/utils/font/simsunbd.ttf');

const handlePDF = () => {
  // 导出pdf
  spread.savePDF((blob) => {
      saveAs(blob, '测试' + '.pdf');
    },
    function (error) {
      console.log(error);
    },
  );
};

导出打开如下 1679911161888.png815432433.png

最佳答案

查看完整内容

你这边的代码还是有问题,可以向详细看一下我们的学习指南和API https://demo.grapecity.com.cn/spreadjs/SpreadJSTutorial/features/pdf/custom-font-export-pdf/purejs https://demo.grapecity.com.cn/spreadjs/help/api-15.1/GC.Spread.Sheets.PDF.PDFFontsManager.html#.registerFont 以GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('SimSun', font);为例, font 参数给的是CSS字体字符串base64字符串或ArrayBu ...

4 个回复

倒序浏览
最佳答案
最佳答案
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2023-3-27 17:59:33
来自 2#
你这边的代码还是有问题,可以向详细看一下我们的学习指南和API
https://demo.grapecity.com.cn/sp ... t-export-pdf/purejs
https://demo.grapecity.com.cn/sp ... .html#.registerFont


以GC.Spread.Sheets.PDF.PDFFontsManager.registerFont('SimSun', font);为例,
font 参数给的是CSS字体字符串base64字符串或ArrayBuffer,且至少要有normal常规字体
一个完整的font应该是下面这样
  1. var font = {
  2.         normal: "宋体常规字体base64字符串",
  3.         bold: "宋体粗体字体base64字符串",
  4.         italic: "宋体斜体字体base64字符串",
  5.         italic: "宋体加粗斜体字体base64字符串"
  6.     };
复制代码
回复 使用道具 举报
Polariscbcbcbc
注册会员   /  发表于:2023-3-28 10:52:34
3#
Richard.Ma 发表于 2023-3-28 09:19
你这边的代码还是有问题,可以向详细看一下我们的学习指南和API
https://demo.grapecity.com.cn/spreadjs/ ...

请问是font里面的四个类型都要配置上吗?
回复 使用道具 举报
Polariscbcbcbc
注册会员   /  发表于:2023-3-28 11:20:07
4#
Richard.Ma 发表于 2023-3-28 09:19
你这边的代码还是有问题,可以向详细看一下我们的学习指南和API
https://demo.grapecity.com.cn/spreadjs/ ...

这边只配置一个normal,并未加其他参数了,查看了event,确认ArrayBuffer是有的,但是还是乱码
1679973564583.png827487182.png


registerServerFont('微软雅黑', 'normal', 'src/utils/font/msyh.ttf');
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2023-3-28 13:55:17
5#
你直接给ttf文件不行的,请参考上面的回复,以及贴出的学习指南的链接,
ttf文件必须要转为base64字符串
比如它这边引用的MTCORSVA字体信息
https://demo.grapecity.com.cn/sp ... jsobv=1678955322557


image.png823891906.png
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部