[英]error TS2345: Argument of type 'Menu' is not assignable to parameter of type
[英]Angular 11 and PdfMake. Error TS2345: Argument of type {...} is not assignable to parameter of type TDocumentDefinitions
我正在尝试根据 Angular 11 应用程序中 pdfMake 的官方文档来定义 pdf 文档结构。 但收到错误:
error TS2345: Argument of type '{ pageSize: string; pageOrientation: string; content: ({ text: string; style: string; fontSize?: undefined; } | { text: string; fontSize: number; style?: undefined; })[]; }' is not assignable to parameter of type 'TDocumentDefinitions'.
Types of property 'pageOrientation' are incompatible.
Type 'string' is not assignable to type '"landscape" | "portrait" | undefined'.
75 pdfMake.createPdf(docDefinition).open();
我已经安装了所有员工:
npm install --save pdfmake
npm i --save-dev @types/pdfmake
并导入它们:
import * as pdfMake from "pdfmake/build/pdfmake";
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
(<any>pdfMake).vfs = pdfFonts.pdfMake.vfs;
代码:
getPdf() {
let docDefinition = {
pageSize: 'A5',
pageOrientation: 'landscape',
content: [
{
text: 'Some data',
style: 'header'
},
{
text: 'some data 2', fontSize: 15
}
]
}
pdfMake.createPdf(docDefinition).open();
}
当我从方法属性定义pageSize和pageOrientation 中删除时- 错误消失。 我还缺少什么?
在查看 Stackoverflow 上的一些帖子后,我找到了解决方案。 问题在于在 TypeScrpit 中使用 pdfMake 库(枚举类型)(由lukasgeiter在这篇文章中描述)。
所以,我改变了 pdfMake 库的导入
import * as pdfMake from "pdfmake/build/pdfmake";
至
const pdfMake = require('pdfmake/build/pdfmake.js');
然后通过 npm 安装节点类型:
npm i @types/node
最后在您的 Angular 应用程序根目录中的tsconfig.app.json文件中进行了一些更改(在行中的方括号中添加“节点”):
"types": []
更改后:
"types": ["node"]
就这样。 编译器终于开始了解文档定义中的所有内容类型,以便在pdfMake库的createPdf方法中制作 pdf
您可以将其转换为类型库中的导出类型。
import { PageSize } from 'pdfmake/interfaces';
并像这样创建你的风格
pageSize: 'A5' as PageSize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.