【问题标题】:pdfmake - using own fonts not workingpdfmake - 使用自己的字体不起作用
【发布时间】:2017-04-01 02:22:28
【问题描述】:

我正在使用 pdfmake 在客户端创建 PDF。我们有一个所见即所得的编辑器,允许用户创建一个 pdf。然后将其解析为与 pdfmake 一起使用。

但是,我无法使用普通字体。该插件使用vfs_fonts.js在PDF上创建字体,默认为Roboto。

我正在尝试让它与 Times New Roman 等类似的作品一起使用。

我曾尝试像这样更改文件:

window.pdfMake = window.pdfMake || {};
window.pdfMake.vfs = {
  Roboto: {
		"Roboto-Italic.ttf": "BASE 64 HERE",
        "Roboto-Medium.ttf": "BASE 64 HERE",
        "Roboto-Regular.ttf": "BASE 64 HERE"
  },
  TimesNewRoman: {
        "Roboto-Italic.ttf": "BASE 64 HERE",
        "Roboto-Medium.ttf": "BASE 64 HERE",
        "Roboto-Regular.ttf": "BASE 64 HERE"
  }
}

我使用了与 Roboto 相同的字体作为测试,但它仍然无法正常工作。这是我回来的错误

Uncaught Error: No unicode cmap for font

下面是我的代码。您将此字符串粘贴到pdf tester here 中并查看结果

{  
   "content":[  
      {  
         "stack":[  
            {  
               "text":[  
                  {  
                     "text":""
                  }
               ]
            },
            {  
               "text":"ygjjkjgjkhjkjghk",
               "style":"style_2",
               "lineHeight":"1"
            }
         ],
         "style":"style_1"
      },
      {  
         "stack":[  
            {  
               "text":[  
                  {  
                     "text":""
                  }
               ]
            },
            {  
               "text":" ",
               "style":"style_4",
               "lineHeight":"1"
            }
         ],
         "style":"style_3"
      },
      {  
         "stack":[  
            {  
               "text":[  
                  {  
                     "text":""
                  }
               ]
            },
            {  
               "text":"",
               "style":"style_7",
               "font":"TimesNewRoman",
               "lineHeight":"1"
            },
            {  
               "text":"sdfghfdghfghdgfgfh",
               "style":"style_8",
               "font":"TimesNewRoman",
               "lineHeight":"1"
            }
         ],
         "style":"style_5"
      }
   ],
   "styles":{  
      "style_1":{  
         "opacity":"1"
      },
      "style_2":{  
         "opacity":"1"
      },
      "style_3":{  
         "opacity":"1"
      },
      "style_4":{  
         "opacity":"1"
      },
      "style_5":{  
         "opacity":"1"
      },
      "style_6":{  
         "opacity":"1"
      },
      "style_7":{  
         "font":"TimesNewRoman",
         "opacity":"1"
      },
      "style_8":{  
         "opacity":"1"
      }
   },
   "pageSize":"A4",
   "pageOrientation":"portrait",
   "pageMargins":[  
      40,
      20,
      40,
      20
   ]
}

还有其他人使用过这个库吗?如果是这样,你是否使用了自定义字体,你是如何让它们工作的?如果需要,我可以发布更多代码,谢谢

【问题讨论】:

    标签: javascript pdfmake


    【解决方案1】:

    有关如何在客户端 here 上使用自定义字体的 Pdfmake 文档。

    vfs_fonts.js 文件格式类似于:

    this.pdfMake = this.pdfMake || {}; this.pdfMake.vfs = {
      "Roboto-Italic.ttf": "AAEAAAASAQAABAAgR0RFRtRX1"
    }
    

    因此,你应该像下面这样定义它:

    window.pdfMake.vfs["Times-New-Roman-Regular.ttf"] = "BASE 64 HERE";
    window.pdfMake.vfs["Times-New-Roman-Medium.ttf"] = "BASE 64 HERE";
    window.pdfMake.vfs["Times-New-Roman-Italic.ttf"] = "BASE 64 HERE";
    

    之后,还需要赋值pdfMake.fonts:

    pdfMake.fonts = {
        // Default font should still be available
        Roboto: {
            normal: 'Roboto-Regular.ttf',
            bold: 'Roboto-Medium.ttf',
            italics: 'Roboto-Italic.ttf',
            bolditalics: 'Roboto-Italic.ttf'
        },
        // Make sure you define all 4 components - normal, bold, italics, bolditalics - (even if they all point to the same font file)
        TimesNewRoman: {
            normal: 'Times-New-Roman-Regular.ttf',
            bold: 'Times-New-Roman-Bold.ttf',
            italics: 'Times-New-Roman-Italics.ttf',
            bolditalics: 'Times-New-Roman-Italics.ttf'
        }
    };
    

    然后,您可以像现在一样使用RobotTimesNewRoman 作为pdf 定义的字体:

    {  
       content:[  
          {  
             text: 'some text using Roboto font'
             style: 'style_1'
          },
          {  
             text: 'some text using Times New Roman font'
             font: 'TimesNewRoman'
          }
       ],
       styles:{  
          style_1:{  
             opacity: '1',
             font: 'Roboto'
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      基于@spm 的回答,我使用 Times New Roman 字体 javascript 文件构建了一个 github 存储库,以及如何使用它的说明!

      PDFMake-Fonts: 我计划最终添加新字体。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-06
        • 1970-01-01
        • 1970-01-01
        • 2014-02-27
        • 2012-04-22
        相关资源
        最近更新 更多