3

我正在尝试从我用 angular 创建的视图创建一个 pdf。问题是pdf看起来像这样:

在此处输入图像描述

它不能计算出角度标记吗?

   var docDefinition = { content: printHtml };
   pdfMake.createPdf(docDefinition).download('optionalName.pdf');
4

2 回答 2

0

Pdfmake 工作正常,它将所有原始 html 打印到 pdf 中。您希望它以 html 打印,但 pdfmake 不能这样工作,通过提供 html 代码,您无法生成格式化的 pdf 文件,您必须创建一个包含文档定义的 javascript 对象,此文档定义包含您的内容和样式。

您可以按照此链接获取基本示例,并在 pdfmake 的 github 页面上查看一些更复杂示例。

祝你好运

于 2016-08-22T10:27:43.870 回答
0

// use npm package and initialise the pdfMake.
const pdfMake = require('pdfmake/build/pdfmake.js')
const pdfFonts = require('pdfmake/build/vfs_fonts.js')
pdfMake.vfs = pdfFonts.pdfMake.vfs


function showPDF() {
const statHeader = { text: data in header not the Header 
header,bold: true}
const content = preparePDF(yourHTML)
const statFooter = 'data in footer not Footer footer, To add a 
footer check the pdfMake documentation for footer.'
const name = ''

const docDefinition = {
        // Add page count
        footer: function (currentPage, pageCount) {
          return {
            margin: 10,
            columns: [{ text: currentPage.toString() + ' of ' + 
pageCount, alignment: 'center' }]
          }
        },
        header: { text: name, style: 'gameName' },
        content: [
          statHeader,
          content,
          statFooter
        ],
        styles: {
          gameName: {
                        fontSize: 16,
            bold: true,
            alignment: 'left',
            margin: [40, 20, 0, 80]
         }
        }
    }
 // creates pdf formated file with give name. 
 pdfMake.createPdf(docDefinition).download(`${name}.pdf`)
}

Wrap it in a function like
const htmlToPDF = require('html-to-pdfmake')
function preparePDF (yourHTML) {
let htmltoPDFData = []
const html = <div>yourHTML</div>
htmltoPDFData = htmlToPDF(html)
return htmltoPDFData
}
于 2021-05-25T10:45:41.387 回答