【问题标题】:How to generate a pdf as a result of HTTP request firebase function? [closed]如何通过 HTTP 请求 firebase 功能生成 pdf? [关闭]
【发布时间】:2020-09-14 14:12:22
【问题描述】:

如何通过 pdfmake 的 HTTP 请求 firebase 函数生成 pdf?

假设我有以下 JSON

{
  "user" : "John Doe",
  "booksOwned":[
      {
        "title":"Fancy Coffins to Make Yourself",
        "author":"Dale L. Power"
      },
      {
        "title":"Knitting With Dog Hair",
        "author":"K Crolius"
      },
      {
        "title":"Everything I Want to Do is Illegal",
        "author":"Joel-Salatin"
      }
    ]
}

我想要一个 PDF 来迎接用户并在表格中列出所有书籍。这应该是我用这个 JSON 调用 firebase 函数时的结果。 如何使用 pdfmake 做到这一点?

【问题讨论】:

标签: firebase pdf-generation pdfmake


【解决方案1】:

先安装pdfmake

npm install pdfmake

并使用以下功能,注意您必须根据需要调整docDefinition

exports.getPDF = functions.https.onRequest(async (req, res) => {
    //...
    var data = JSON.parse(req.query.json); //asume json is given as parameter
    //fonts need to lay in the functions directory
    var fonts = {
        Roboto: {
            normal: './fonts/Roboto-Regular.ttf',
            bold: './fonts/Roboto-Medium.ttf',
            italics: './fonts/Roboto-Italic.ttf',
            bolditalics: './fonts/Roboto-MediumItalic.ttf'
        },
    };

    var PdfPrinter = require('pdfmake'); //needs to installed via "npm install pdfmake"
    var printer = new PdfPrinter(fonts);

    var docDefinition ={
content: [
        
        {text: 'Hello User:', style: 'header'},
        {
            table: {
                body: [
                    ['book', 'author',],
                    ['book1','author1'],
                    ['book2','author2'],
                    ['book3','author3']
                ]
            }
        }
    ]
}


    var options = {
        // ...
    }

    var pdfDoc = printer.createPdfKitDocument(docDefinition, options);
    pdfDoc.pipe(res.status(200));
    pdfDoc.end();

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2018-01-07
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多