Add image in header using html-pdf node module

后端 未结 6 1572
深忆病人
深忆病人 2021-02-13 23:28

I am using this to convert the html to PDF.The conversions are really good.But the problem is to add header and footer in the PDF pages.In the options if i add the header text i

6条回答
  •  滥情空心
    2021-02-13 23:46

    Refering to this issue on the github, you can't put your image directly in options.header, you have to put it in the body inside a

    :

    var pdf = require('html-pdf');
    var path = require('path');
    
    // this is very important, you have to put file:// before your path
    // and normalize the resulting path
    var imgSrc = 'file://' + __dirname + '/350x120.png';
    imgSrc = path.normalize(imgSrc);
    // or var imgSrc = path.join('file://', __dirname, '/350x120.png');
    
    // Options
    var options = {
        "header": {
          "height": "45mm",
          "contents": ""
        },
        "footer": {
          "height": "28mm",
          "contents": "{{page}}/{{pages}}"
        }
      }
    // put your entire header here and the content of the page outside the 
    var result = "";
    result += "

    Lorem ipsum dolor sit amet, consectetur adipisicing elit.

    "; var fileName = __dirname + '/test.pdf'; pdf.create(result, options).toFile(fileName, function(err, res) { if (err) { console.error(err); } });

    With this code, I get this pdf:

    generated pdf

提交回复
热议问题