具有Unicode支持的JavaScript pdf生成库 [英] JavaScript pdf generation library with Unicode support

查看:119
本文介绍了具有Unicode支持的JavaScript pdf生成库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在客户端使用JavaScript生成pdf文件.首先,我尝试使用jsPdf API.但这不适用于中文之类的Unicode字符.

I want to generate a pdf file using JavaScript at client side . Firstly I tried using jsPdf API . But it does not work with Unicode character like Chinese.

是否可以选择增强jspdf以支持Unicode或任何其他支持Unicode的库.

Is there any option to enhance jspdf to support Unicode or any other library which supports Unicode .

Pdfmake API说它支持Unicode,但是当我尝试也无法解决问题时,我在那里检查了演示示例,放置Unicode字符.

Pdfmake API says it supports Unicode but when I tried it also does not work out, I checked in there demo example placing Unicode character .

我尝试在Node.js中使用pdfkit,但是无法正确创建pdf

I tried using pdfkit in Node.js but pdf is not getting created properly

var PDFDocument = require("pdfkit");
var fs = require('fs');

var doc = new PDFDocument;

doc.pipe(fs.createWriteStream('output.pdf'));

doc.fontSize(15);
doc.text('Generate PDF! 漢字漢字漢字漢字');

doc.end();

在pdf中,它被创建为Generate PDF! o"[Wo" [Wo"[Wo" [W

In pdf it created as Generate PDF! o"[Wo"[Wo"[Wo"[W

也可以在pdfMake中添加多种字体

Also , Can I add multiple font in pdfMake

 var fontDescriptors = {
    Roboto: {
      normal: 'examples/fonts/Roboto-Regular.ttf',
      bold: 'examples/fonts/Roboto-Medium.ttf',
      italics: 'examples/fonts/Roboto-Italic.ttf',
      bolditalics: 'examples/fonts/Roboto-Italic.ttf'
    }
  };

  var printer = new pdfMakePrinter(fontDescriptors);

推荐答案

我将使用Node.js和 pdfmake (内部使用 PDFKit .

I'll describe a solution using Node.js and PDFKit since you mentioned it but it also applies to pdfmake which internally uses PDFKit.

在大多数情况下,这些库中使用的默认字体不接受中文字符.您必须添加和使用与需要支持的语言兼容的字体.例如,如果我们以 pdfmake 为例,则他们将Roboto作为其默认字体,并且实际上不接受汉字.

Most of the time, the default font used in these libraries do not accept Chinese characters. You have to add and use fonts that are compatible for the languages you need to support. If we take pdfmake for example, they use Roboto as their default font and it indeed does not accept Chinese characters.

使用您的代码示例,我们可以使用Microsoft YaHei字体(msyh.ttf)添加对中文的支持,例如仅增加1行代码即可.

Using your code example, we can add support for Chinese using the Microsoft YaHei font (msyh.ttf) for instance with only 1 additional line of code:

var PDFDocument = require("pdfkit");
var fs = require('fs');

var doc = new PDFDocument;

doc.pipe(fs.createWriteStream('output.pdf'));

doc.font('fonts/msyh.ttf');
doc.fontSize(15);
doc.text('Generate PDF! 漢字漢字漢字漢字');

doc.end();

输出看起来像这样:

这篇关于具有Unicode支持的JavaScript pdf生成库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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