Canvas 元素在多次使用或循环使用时会导致渲染不正确 - PDFMake
Canvas element causes incorrect rendering when used more than once or in a loop - PDFMake
我正在尝试使用 PDFMake 在服务器端生成多个 PDF。每个 PDF 都有一个特定的部分,该部分在所有要生成的 PDF 中都是不变的,general
。然后 general
部分由 line
分隔,然后在其下方加载特定于该文档的信息。
这在第一个 PDF 上效果很好。然而,在第二个,布局中断了。我该如何解决这个问题?
第一个 PDF:
第二个 PDF:
"use strict";
const fs = require("fs");
const PdfPrinter = require("pdfmake");
const fonts = {
Helvetica: {
normal: "Helvetica",
bold: "Helvetica-Bold",
italics: "Helvetica-Oblique",
bolditalics: "Helvetica-BoldOblique",
},
};
const printer = new PdfPrinter(fonts);
const general = [{ text: "This is general text" }];
const first = [{ text: "This is the text for the first pdf" }];
const second = [{ text: "This is the text for the second pdf" }];
const line = [{canvas: [{ type: 'line', x1: 0, y1: 5, x2: 595-2*40, y2: 5, lineWidth: 2 }],margin: [ 0, 10, 0, 10 ]},];
let docDefinition = {
pageSize: "letter",
defaultStyle: {
font: "Helvetica"
}
};
docDefinition.content = [general,line,first];
let pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('one.pdf'));
pdfDoc.end();
docDefinition.content = [general,line,second];
pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('two.pdf'));
pdfDoc.end();
所以我在 GitHub issues page 上问了一个问题,得到的答复是:
One docDefinition
variable can be used only once to generate PDF.
虽然这可能是正确的,但我确实找到了一个适当的解决方法,方法是仅使用带有 headerLine 的 table。这不是我的主意,但我不记得我从哪里得到的。
function line() {
//Usually one would use a canvas to draw the line
//{canvas: [{ type: 'line', x1: 0, y1: 5, x2: 595-2*40, y2: 5, lineWidth: 2 }],margin: [ 0, 10, 0, 0 ]},
//For some reason, that's not working and the layout just breaks
return {
table : {
headerRows : 1,
widths: ['100%'],
body : [
[''],
['']
]
},
layout : 'headerLineOnly'
}
}
然后在 docDefinition
中任何需要一行的地方使用 line()
。
"use strict";
const fs = require("fs");
const PdfPrinter = require("pdfmake");
const fonts = {
Helvetica: {
normal: "Helvetica",
bold: "Helvetica-Bold",
italics: "Helvetica-Oblique",
bolditalics: "Helvetica-BoldOblique",
},
};
const printer = new PdfPrinter(fonts);
const general = [{ text: "This is general text" }];
const first = [{ text: "This is the text for the first pdf" }];
const second = [{ text: "This is the text for the second pdf" }];
let docDefinition = {
pageSize: "letter",
defaultStyle: {
font: "Helvetica"
}
};
docDefinition.content = [general,line(),first];
let pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('one.pdf'));
pdfDoc.end();
我正在尝试使用 PDFMake 在服务器端生成多个 PDF。每个 PDF 都有一个特定的部分,该部分在所有要生成的 PDF 中都是不变的,general
。然后 general
部分由 line
分隔,然后在其下方加载特定于该文档的信息。
这在第一个 PDF 上效果很好。然而,在第二个,布局中断了。我该如何解决这个问题?
第一个 PDF:
第二个 PDF:
"use strict";
const fs = require("fs");
const PdfPrinter = require("pdfmake");
const fonts = {
Helvetica: {
normal: "Helvetica",
bold: "Helvetica-Bold",
italics: "Helvetica-Oblique",
bolditalics: "Helvetica-BoldOblique",
},
};
const printer = new PdfPrinter(fonts);
const general = [{ text: "This is general text" }];
const first = [{ text: "This is the text for the first pdf" }];
const second = [{ text: "This is the text for the second pdf" }];
const line = [{canvas: [{ type: 'line', x1: 0, y1: 5, x2: 595-2*40, y2: 5, lineWidth: 2 }],margin: [ 0, 10, 0, 10 ]},];
let docDefinition = {
pageSize: "letter",
defaultStyle: {
font: "Helvetica"
}
};
docDefinition.content = [general,line,first];
let pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('one.pdf'));
pdfDoc.end();
docDefinition.content = [general,line,second];
pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('two.pdf'));
pdfDoc.end();
所以我在 GitHub issues page 上问了一个问题,得到的答复是:
One
docDefinition
variable can be used only once to generate PDF.
虽然这可能是正确的,但我确实找到了一个适当的解决方法,方法是仅使用带有 headerLine 的 table。这不是我的主意,但我不记得我从哪里得到的。
function line() {
//Usually one would use a canvas to draw the line
//{canvas: [{ type: 'line', x1: 0, y1: 5, x2: 595-2*40, y2: 5, lineWidth: 2 }],margin: [ 0, 10, 0, 0 ]},
//For some reason, that's not working and the layout just breaks
return {
table : {
headerRows : 1,
widths: ['100%'],
body : [
[''],
['']
]
},
layout : 'headerLineOnly'
}
}
然后在 docDefinition
中任何需要一行的地方使用 line()
。
"use strict";
const fs = require("fs");
const PdfPrinter = require("pdfmake");
const fonts = {
Helvetica: {
normal: "Helvetica",
bold: "Helvetica-Bold",
italics: "Helvetica-Oblique",
bolditalics: "Helvetica-BoldOblique",
},
};
const printer = new PdfPrinter(fonts);
const general = [{ text: "This is general text" }];
const first = [{ text: "This is the text for the first pdf" }];
const second = [{ text: "This is the text for the second pdf" }];
let docDefinition = {
pageSize: "letter",
defaultStyle: {
font: "Helvetica"
}
};
docDefinition.content = [general,line(),first];
let pdfDoc = printer.createPdfKitDocument(docDefinition);
pdfDoc.pipe(fs.createWriteStream('one.pdf'));
pdfDoc.end();