How to put variables in PhantomJS generated HTML headers for PDF conversion?

I have a huge problem . I want to automatically generate PDF files from HTML templates with custom headers and footers using express.js, phantomjs and EJS.

I have no problem installing any “hard-coded” HTML lines in PhantomJS created by footer headers (they work):

footer: {
    height: "3cm",
    contents: ph.callback(function(pageNum, numPages) {
        return "<div style='padding: .2em; font-size: 10pt;border-top: 1px solid #ccc; color: #999;'> FOOTER <span style='float:right'> Página " + pageNum + " / " + numPages + "</span></div>";
    })

But, when I try to configure it programmatically:

var pdfHeader = ejs.compile(fs.readFileSync(path.join('server/components/mail/html-templates/pdf-header.html'), 'utf8'));
    pdfHeader = pdfHeader({info: info});

    header: {
        height: "3cm",
        contents: ph.callback(function(pageNum, numPages) {
            if (pageNum == numPages) {
                return "";
            }
            return pdfHeader;
        })
    },

It does not work and gives me this message:

phantom stdout: SyntaxError: Unexpected EOF

How can I put custom HTML with some user data in the header?

+4
1

, phatomjs.

  • ph.callback . , . . https://github.com/amir20/phantomjs-node/issues/224. , @zgotts, Git, .

    var headerHtml = ejs.compile(fs.readFileSync(path.join('server/components/mail/html-templates/pdf-header.html'), 'utf8'));
    var createHeader = function(html) {
            return function (pageNum, numPages) {
                //console.log(html);
                console.log('Header set!');
                return '%HTML%';
            }.toString().replace(/%HTML%/, html);
        };
    
        renderSession.createPage()
            .then(function(_page) {
                page = _page;
                var file = 'file.pdf';
    
                page.property('paperSize', {
                    format: 'Letter',
                    header: {
                        height: "3.75cm",
                        contents: renderSession.callback(createHeader(headerHtml))
                    }
                })
            });
    
  • , , , , ejs . , , , .
0

Source: https://habr.com/ru/post/1617527/


All Articles