带有 PDFMake 的 htmlToPdf:如何在 pdf 中的每个内容后添加分页符 (Angular 8)?
htmlToPdf with PDFMake: How to add page break after every content in pdf (Angular 8)?
我正在通过 html-to-pdfmake
模块从 html 字符串创建 pdf,并通过 pdfmake
生成 pdf。
这是代码,其中有 convertToPDF
函数,它会通过 html-to-pdfmake
模块将循环中的每个 html 字符串转换为 pdf,然后将其推送到另一个数组,即 checkedArray
,之后我将代码插入到 pdfmake 的内容中并添加了一个 pagebreak
,但它不起作用,它将所有内容合并到一页中,我想要按照 pdfmake 中推送的内容的页面内容,
那我该怎么做呢?
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import htmlToPdf from 'html-to-pdfmake';
convertToPDF(resHtml) {
for (var i = 0; i < resHtml.length; i++) {
let data = resHtml[i].htmlString;
let docId = resHtml[i].docId;
let docTitle = resHtml[i].docTitle;
var docDef = htmlToPdf(data);
this.checkedArray.push({
id: docId,
title: docTitle,
content: docDef
});
}
}
const docDefinition = {
content: [],
styles: {
fontsize10: {
fontSize: 10,
bold: true
},
fontsize12: {
fontSize: 12,
bold: true
},
quote: {
italics: true
},
small: {
fontSize: 8
},
defaultStyle: {
fontSize: 10
},
decoration: {
decoration: 'underline'
},
tblHeader: {
fontSize: 12,
fillColor: '#1d86b8',
color: '#ffffff',
bold: true,
overflow: 'ellipsize',
tocItem: true,
},
subheader: {
fontSize: 14,
margin: [0, 10, 0, 5]
},
}
};
for (var i = 0; i < this.checkedArray.length; i++) {
docDefinition.content.push(this.checkedArray[i].content);
docDefinition.content.push({
text: '',
pagebreak: 'after'
});
}
const pdfDocGenerator = pdfMake.createPdf(docDefinition);
您可以将此函数添加到您的 docDefinition
<h1 class="pdf-pagebreak-before">My title on page 2</h1>
pageBreakBefore: function(currentNode) {
return currentNode.style && currentNode.style.indexOf('pdf-pagebreak-before') > -1;
}
我正在通过 html-to-pdfmake
模块从 html 字符串创建 pdf,并通过 pdfmake
生成 pdf。
这是代码,其中有 convertToPDF
函数,它会通过 html-to-pdfmake
模块将循环中的每个 html 字符串转换为 pdf,然后将其推送到另一个数组,即 checkedArray
,之后我将代码插入到 pdfmake 的内容中并添加了一个 pagebreak
,但它不起作用,它将所有内容合并到一页中,我想要按照 pdfmake 中推送的内容的页面内容,
那我该怎么做呢?
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import htmlToPdf from 'html-to-pdfmake';
convertToPDF(resHtml) {
for (var i = 0; i < resHtml.length; i++) {
let data = resHtml[i].htmlString;
let docId = resHtml[i].docId;
let docTitle = resHtml[i].docTitle;
var docDef = htmlToPdf(data);
this.checkedArray.push({
id: docId,
title: docTitle,
content: docDef
});
}
}
const docDefinition = {
content: [],
styles: {
fontsize10: {
fontSize: 10,
bold: true
},
fontsize12: {
fontSize: 12,
bold: true
},
quote: {
italics: true
},
small: {
fontSize: 8
},
defaultStyle: {
fontSize: 10
},
decoration: {
decoration: 'underline'
},
tblHeader: {
fontSize: 12,
fillColor: '#1d86b8',
color: '#ffffff',
bold: true,
overflow: 'ellipsize',
tocItem: true,
},
subheader: {
fontSize: 14,
margin: [0, 10, 0, 5]
},
}
};
for (var i = 0; i < this.checkedArray.length; i++) {
docDefinition.content.push(this.checkedArray[i].content);
docDefinition.content.push({
text: '',
pagebreak: 'after'
});
}
const pdfDocGenerator = pdfMake.createPdf(docDefinition);
您可以将此函数添加到您的 docDefinition
<h1 class="pdf-pagebreak-before">My title on page 2</h1>
pageBreakBefore: function(currentNode) {
return currentNode.style && currentNode.style.indexOf('pdf-pagebreak-before') > -1;
}