我正在尝试使用pdfmake生成带水印的pdf。但是,在使用属性“watermark:“test”时,没有生成pdf。控制台上弹出一个错误,说“在文档定义的字体部分中没有定义样式“normal”中的字体“undefined”。如何使用pdfmake JS库生成带水印的pdf。

<html>
        <head>

            <script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/pdfmake.min.js'></script>
            <script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.20/vfs_fonts.js'></script>
            <script type="text/javascript">

        function myFunction()
        {


            var docDefinition = {
                watermark:'test',
      background: 'simple text',
      content: 'This is an sample PDF printed with pdfMake'

    };
        pdfMake.createPdf(docDefinition).download('Report.pdf');

        }
        </script>
        </head>
    <body>

    <button type="button" onclick="myFunction()">Click Me!</button>
    </body>
    </html>

最佳答案

这个问题在更新版本的pdfMake中解决了,请确保使用最新版本,它将正常工作。

function myFunction() {
  var docDefinition = {
    watermark: 'test',
    background: 'simple text',
    content: 'This is an sample PDF printed with pdfMake'
  };
  pdfMake.createPdf(docDefinition).download('Report.pdf');
}

<script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.31/pdfmake.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.31/vfs_fonts.js'></script>

<button type="button" onclick="myFunction()">Click Me!</button>

10-06 00:31