更改数据表 pdfmaker 扩展中的字体 [英] Changing font in datatables pdfmaker extension

查看:23
本文介绍了更改数据表 pdfmaker 扩展中的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在谷歌上搜索了一整天,并搜索了 StackOverflow 以找到一种解决方案来更改数据表的 pdf 导出中的字体.但是,我没有遇到解决方案.当我将表格导出为 pdf 脚本字体时,有些东西无法辨认.看看下面的图片.它显示了我表格中的两列.

dataTables 论坛和 pdfMaker 文档都含糊不清.任何人都可以帮我解决这个问题.我需要为数据表的 pdfMaker 扩展指定字体.以下是 vfs_fonts.js 的样子:

this.pdfMake = this.pdfMake ||{};this.pdfMake.vfs = {pdfMake.fonts = {瓦兹尔:{正常:'Vazir-FD.ttf',粗体:'Vazir-FD.ttf',斜体:'Vazir-FD.ttf',粗体字:'Vazir-FD.ttf'}};}

以下也是我的数据表的按钮块:

按钮:[{扩展:'pdfHtml5',exportOptions:{ 列:[0, 1, 2, 3, 4, 5, 6] },自定义:功能(文档){doc.defaultStyle.font = Vazir},},]

请注意,在上面的代码块中,当我添加自定义"块时,pdfMaker 按钮不会准备任何 pdf 报告;没有它,它可以工作,但是,字体无法辨认.提前致谢.

解决方案

这里有一个解决方案.

数据表代码

HTML 如下:

<html lang="en"><头><元字符集=UTF-8"><title>导出为 PDF</title><script src=https://code.jquery.com/jquery-3.3.1.min.js"></script><script src=https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script><link rel="样式表";类型=文本/css";href=https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"><link rel="样式表";类型=文本/css";href=https://datatables.net/media/css/site-examples.css"><!-- 按钮--><link rel="样式表";类型=文本/css";href=https://cdn.datatables.net/buttons/1.6.1/css/buttons.dataTables.min.css"><script src=https://code.jquery.com/jquery-3.3.1.js"></script><script src=https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script><script src=https://cdn.datatables.net/buttons/1.6.1/js/dataTables.buttons.min.js"></script><script src=https://cdn.datatables.net/buttons/1.6.1/js/buttons.flash.min.js"></script><script src=https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script><script src=https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script><!--<script src=https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>构建 vfs_fonts.js 文件的自定义本地版本,其中包含您需要的任何字体.这文件的结构是这样的:this.pdfMake = this.pdfMake ||{};this.pdfMake.vfs = {arial.ttf":AAAAA...MYXRu",another_one.ttf":XXXX...XXXX"};替换AAAAA...MYXRu"使用字体文件的 base64 编码字符串.您可以使用此站点生成字符串:https://dataurl.sveinbjorn.org/#dataurlmaker--><script src="vfs_fonts.js"></script><script src=https://cdn.datatables.net/buttons/1.6.1/js/buttons.html5.min.js"></script><script src=https://cdn.datatables.net/buttons/1.6.1/js/buttons.print.min.js"></script><身体><div style="margin: 20px;"><table id="example"class="display nowrap dataTable cell-border";样式=宽度:100%"><头><tr><th>姓名</th><th>数据</th></tr></thead><tr><td>阿德莱德尼克松</td><td><font face="verdana">الفبایفارسی ۱۲۳۴</font></td></tr></tbody><脚><tr><th>姓名</th><th>数据</th></tr></tfoot>

<script type="text/javascript">$(document).ready(function() {$('#example').DataTable({dom: 'Bfrtip',纽扣: [{扩展:'pdf',自定义:功能(文档){流程文档(文档);}}]});});函数 processDoc(doc) {////https://pdfmake.github.io/docs/fonts/custom-fonts-client-side/////使用 pdfmake 中可用的字体更新 pdfmake 的全局字体列表//定制的 vfs_fonts.js 文件(不要删除 Roboto 默认值):pdfMake.fonts = {机器人:{正常:'Roboto-Regular.ttf',粗体:'Roboto-Medium.ttf',斜体:'Roboto-Italic.ttf',粗体字:'Roboto-MediumItalic.ttf'},宋体:{正常:'arial.ttf',粗体:'arial.ttf',斜体:'arial.ttf',粗体字:'arial.ttf'}};//修改 PDF 以使用不同的默认字体:doc.defaultStyle.font = "arial";变量 i = 1;}

作为网页的数据表

上述 HTML 生成以下网页:

PDF 文件

当您单击另存为 PDF"时按钮,PDF 文档如下所示:

如何实施

此处所述,pdfMake使用Roboto默认字体.此字体不支持波斯字符/脚本.为了解决这个问题,我将默认字体更改为 Arial,这确实提供了对波斯字符/脚本的支持.

请参阅最后关于 Arial 使用的附加说明 - 另一种字体可能更适合避免许可问题.

为了进行此更改,我采取了以下步骤:

我生成了一个新的 vfs_fonts.js 文件,其中包含一个 arial TTF 文件的内容.我还参考了这个新的本地 vfs_fonts.js 文件,而不是 Cloudflare 版本.

vfs_fonts.js 文件的结构如下:

this.pdfMake = this.pdfMake ||{};this.pdfMake.vfs = {arial.ttf":AAAAA...MYXRu",another_one.ttf":XXXX...XXXX"};

每个 "AAEAA...MYXRu 字符串都是相关字体文件的 base64 编码表示.

要为您的 TTF 文件生成字符串,您可以使用 pdfmake 提供的实用程序(见下文),或者您可以使用任何 base64 编码器.一个例子是dataurlmaker.

将 dataurlmaker 生成的(很长)字符串粘贴到您的 vfs_fonts.js 文件中.不要包含 dataurlmaker 提供的任何前导码(data:application/octet-stream;base64").仅包含 base64 字符串本身.

或者...

使用pdfmake提供的工具:

为了生成这个新的 vfs_fonts.js 文件,我按照 此页面.

(a) 我已经安装了 npm.

(b) 我运行了 npm install pdfmake

(c)我改到pdfmake安装目录:

C:Users
ode_modulespdfmake

(d) 我在 pdfMake 目录中创建了 examples/fonts 子目录.

(e) 我将我的 Windows arial.ttf 文件复制到这个新的 fonts 目录中.

(f) 我运行了 npm install(来自 pdfMake 目录)以确保安装了所有必备模块.

(g) 我使用 npm install gulp --global

安装了 gulp

(h) 我运行 gulp buildFonts 来创建一个新的 build/vfs_fonts.js.

(i) 我在我的网页代码中包含了这个新的 build/vfs_fonts.js 文件(如上所示).

采取这些步骤后,我能够使用 Arial 字体生成 PDF.


更新

请阅读@anotherfred 提供的评论一些重要的注意事项:

关于Arial的具体用法(重点是我的):

<块引用>

请注意,Arial 的许可可能会禁止.Noto Sans 等字体是免费的国际字体,但您必须仔细选择版本才能获得所需的语言.

您可以使用在线工具,例如 Google FontsFont Squirrel 找到与您的语言相匹配的字体&字符/字形要求.

关于如何引用您选择的字体文件:

<块引用>

此外,为了避免在数据表选项中设置默认字体,您可以在 pdfMake.fonts Roboto(无论您实际使用的任何 ttf 文件)中命名您的密钥,然后它将自动使用.

如果可以在未来版本的 DataTables(带有 pdfmake 的升级版本)中开箱即用,那就太好了

<块引用>

您也可以使用字体 url 代替 vfs_fonts,但这需要比数据表建议的更新版本的 pdfMake.

I have googled enough for a whole one day, and searched StackOverflow to find a solution for changing the font in pdf exports of dataTables. However, I didn't run into a solution. When I export the table into pdf script fonts are something undecipherable. Just look at the picture below. It shows two columns from my table.

Both dataTables forum and pdfMaker documentations are vague. Can anyone please help me out of the problem. I need to specify a font for pdfMaker extension of datatables. The following is what vfs_fonts.js looks like:

this.pdfMake = this.pdfMake || {}; this.pdfMake.vfs = {
 pdfMake.fonts = {
        Vazir: {
                normal: 'Vazir-FD.ttf',
                bold: 'Vazir-FD.ttf',
                italics: 'Vazir-FD.ttf',
                bolditalics: 'Vazir-FD.ttf'
        }
};
}

The following is also my buttons block of my datatables:

buttons: [
                { extend: 'pdfHtml5', exportOptions:
                        { columns: [0, 1, 2, 3, 4, 5, 6] },
                    customize: function (doc) {
                    doc.defaultStyle.font = Vazir},

                },
 ]

Note that in the above block of code, when I add 'customize' block, the pdfMaker button won't prepare any pdf reports; without it, it works, however, the fonts are undecipherable. Thanks in advance.

解决方案

Here is a solution.

The DataTable Code

The HTML is as follows:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Export to PDF</title>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
  <link rel="stylesheet" type="text/css" href="https://datatables.net/media/css/site-examples.css">

  <!-- buttons -->
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.6.1/css/buttons.dataTables.min.css">
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
  <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/buttons/1.6.1/js/dataTables.buttons.min.js"></script>
  <script src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.flash.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
  
  <!--
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>

  Build a custom local version of the vfs_fonts.js file, containing whatever fonts you need. The 
  structure of the file is this:
  
  this.pdfMake = this.pdfMake || {}; this.pdfMake.vfs = {
    "arial.ttf": "AAEAA...MYXRu",
    "another_one.ttf": "XXXX...XXXX"
  };

  Replace the "AAEAA...MYXRu" with the base64-encoded string of your font file.
  You can use this site to generate the string: https://dataurl.sveinbjorn.org/#dataurlmaker

  -->
  <script src="vfs_fonts.js"></script>
  

  <script src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.html5.min.js"></script>
  <script src="https://cdn.datatables.net/buttons/1.6.1/js/buttons.print.min.js"></script>

</head>

<body>

<div style="margin: 20px;">

<table id="example" class="display nowrap dataTable cell-border" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Data</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Adélaïde Nixon</td>
                <td><font face="verdana">الفبای فارسی ۱۲۳۴</font></td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Data</th>
            </tr>
        </tfoot>
    </table>

</div>

<script type="text/javascript">

  $(document).ready(function() {
    $('#example').DataTable({

      dom: 'Bfrtip',
      buttons: [{
        extend: 'pdf',
        customize: function ( doc ) {
          processDoc(doc);
        }
      }]
    });
  });

  function processDoc(doc) {
    //
    // https://pdfmake.github.io/docs/fonts/custom-fonts-client-side/
    //
    // Update pdfmake's global font list, using the fonts available in
    // the customized vfs_fonts.js file (do NOT remove the Roboto default):
    pdfMake.fonts = {
      Roboto: {
        normal: 'Roboto-Regular.ttf',
        bold: 'Roboto-Medium.ttf',
        italics: 'Roboto-Italic.ttf',
        bolditalics: 'Roboto-MediumItalic.ttf'
      },
      arial: {
        normal: 'arial.ttf',
        bold: 'arial.ttf',
        italics: 'arial.ttf',
        bolditalics: 'arial.ttf'
      }
    };
    // modify the PDF to use a different default font:
    doc.defaultStyle.font = "arial";
    var i = 1;
  }

</script>

</body>

The DataTable as a Web Page

The above HTML produces the following web page:

The PDF File

When you click on the "Save as PDF" button, the PDF document looks like this:

How to Implement

As explained here, pdfMake uses the Roboto font by default. This font does not support Persian characters/script. To work around this, I changed the default font to Arial, which does provide support for Persian characters/script.

Please see the additional notes at the end regarding the use of Arial - another font may be more appropriate to avoid licensing issues.

To make this change I took the following steps:

I generated a new vfs_fonts.js file, containing the contents of an arial TTF file. I also refer to this new local vfs_fonts.js file, instead of the Cloudflare version.

The vfs_fonts.js file has the following structure:

this.pdfMake = this.pdfMake || {}; this.pdfMake.vfs = {
  "arial.ttf": "AAEAA...MYXRu",
  "another_one.ttf": "XXXX...XXXX"
};

Each of the "AAEAA...MYXRu strings is the base64-encoded representation of the related font file.

To generate the string for your TTF file, you can use the utilities provided by pdfmake (see below), or you can use any base64 encoder. One example is dataurlmaker.

Paste the (very long) string generated by dataurlmaker into your vfs_fonts.js file. Do NOT include any preamble provided by dataurlmaker ("data:application/octet-stream;base64,"). Include only the base64 string itself.

Alternatively...

Using the tools provided by pdfmake:

To generate this new vfs_fonts.js file, I followed the relevant instructions on this page.

(a) I already had npm installed.

(b) I ran npm install pdfmake

(c) I changed to the pdfmake installation directory:

C:Users<myUserID>
ode_modulespdfmake

(d) I created the examples/fonts subdirectory in my pdfMake directory.

(e) I copied my Windows arial.ttf file into this new fonts directory.

(f) I ran npm install (from the pdfMake directory) to ensure all prerequisites modules were installed.

(g) I installed gulp using npm install gulp --global

(h) I ran gulp buildFonts to create a new build/vfs_fonts.js.

(i) I included this new build/vfs_fonts.js file in my web page code (as shown above).

After taking these steps, I was able to generate a PDF using the Arial font.


Update

Please read the comments provided by @anotherfred for some important notes:

Regarding the specific use of Arial (emphasis is mine):

Note that Arial's licence may forbid this. Fonts like Noto Sans are free international fonts but you have to carefully choose the version to get the languages you want.

You can use online tools such as Google Fonts and Font Squirrel to find fonts which match your language & character/glyph requirements.

Regarding how to reference your chosen font file(s):

Also, to avoid having to set the default font in datatables options, you can just name your key in pdfMake.fonts Roboto (whatever ttf files you actually use in it) and it will be used automatically.

It would be great if the following could be usd out-of-the-box in a future version of DataTables (with an upgraded version of pdfmake)

You can also use a font url instead of vfs_fonts, but this requires a newer version of pdfMake than datatables suggest.

这篇关于更改数据表 pdfmaker 扩展中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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