这是html字符串,我想在pdfmake中打印
let html_string = "<html><head></head><body><p>HI</p></body></html>";
//something like this
{ text: html_string,
pageBreak: 'after',
},
它应该在pdfmake的一部分中作为html内容打印:如何像在pdf中一样打印包含html的字符串。
答案 0 :(得分:0)
答案 1 :(得分:0)
使用html解析器获取所需的html内容。 https://www.npmjs.com/package/node-html-parser
在那之后,我将编写一个对象构建函数来获取您正在寻找的正确的html标签内容。
import { parse } from 'node-html-parser';
const root = parse('<ul id="list"><li>Hello World</li></ul>');
var htmlBody = root.querySelector('#list');
function getContentBuilder (htmlBody ) {
var record = {}
//Locate htmlBody childnode that you want and insert to record
return record;
}
//something like this
{ text: getContentBuilder(htmlBody) ,
pageBreak: 'after',
},
答案 2 :(得分:0)
选项1.将html转换为字符串,然后再将其传递给pdfmake。您可以使用html-to-text
const PdfPrinter = require("pdfmake");
const htmlToText = require('html-to-text');
let dd = {
pageSize: "A4",
pageOrientation: "portrait",
content: [
{
text: htmlToText.fromString("<p>Hello <strong>World</strong></p>", {
wordwrap: 130
}),
style: "",
}
],
}
let printer = new PdfPrinter(fonts);
let doc = printer.createPdfKitDocument(dd);
选项2。使用html-to-pdfmake 。如果要在Nodejs端进行操作,则必须添加jsdom。
const PdfPrinter = require("pdfmake");
let jsdom = require("jsdom");
let { JSDOM } = jsdom;
let { window } = new JSDOM("");
const htmlToPdfmake = require("html-to-pdfmake");
let dd = {
pageSize: "A4",
pageOrientation: "portrait",
content: [
{
text: htmlToPdfmake(salesConditions.salesConditionBody, {
window: window,
}),
style: "",
}
],
}
let printer = new PdfPrinter(fonts);
let doc = printer.createPdfKitDocument(dd);