1

I have a code as follow:

var info = {
    "Company": "ABC",
    "Team": "JsonNode",
    "Number": 4,
    "Time": "1 day"
};

var layout = "<ol><li>Info: </li><ol><ul>Company:       {{info.Company}}</ul><ul>Team: {{info.Team}}</ul></ol><li>Number: {{info.Number}}</li><li>Time: {{info.Time}}</li><ol>";

function generatePdf(layout,info) 
{
    var wkhtmltopdf = require('wkhtmltopdf');
    console.log('generating pdf');
    wkhtmltopdf(layout, {
       //console.log('debug');
       output: './file.pdf',
       "viewport-size": "1280x1024",
       "page-width": "400",
       "page-height": "600"
    });
}
generatePdf(layout,info); 

And now I can generate a pdf file but pdf file's content is not right. is is:

    1. Info:
    Company: {{info.Company}}

    Team: {{info.Team}}
    2. Number: {{info.Number}}
    3. Time: {{info.Time}}

My problems here are:

1: How to separate html template(var layout) to a file.

2: info.Company must be ABC, info.Number must be 4

Please give me a help!

4
  • Your template HTML is incorrect to begin with, you almost randomly open and close ul and ol elements and some data isn't even inside an li (that stands for list item if that gives you an indication of whats going on) - Also, how does company info with a submenu count every thing in the submenu as if its in the main menu (aka how can number be 4 when its the second in the primary list?) Commented Oct 6, 2015 at 11:31
  • can you please post a fiddle Commented Oct 6, 2015 at 11:33
  • @KarthickKumar or, if possible, your code in a SO snippet (its the document icon with the <> in it) Commented Oct 6, 2015 at 11:34
  • give this code some php tidy Commented Oct 6, 2015 at 11:34

3 Answers 3

1

please rearrange your layout variable like this and check

var layout = "<ol><li>Info: </li><ol><ul>Company:"+info.Company+"</ul><ul>Team: "+info.Team+"</ul></ol><li>Number:"+info.Number+"</li><li>Time:"+info.Time+"</li><ol>";
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please help with the first question? The second one is done!
1

If your javascript runs in Node.js you can use

var layout = require("./layout.json");

to get your layout from a file. I think on your webapp you use angular to replace {{var}} with its content, but if you generate a PDF there is no angular to write the values, meaning you have to do it in JS like this

layout.replace("{{info.Company}}",info.Company);

3 Comments

When I require .html file. an error occured. logs:(function (exports, require, module, __filename, __dirname) { <ol><li>Info: </ ^ SyntaxError: Unexpected token <
Quick fix: change layout.html to layout.json and put "<ol><li>Info: </li><ol><ul>Company: {{info.Company}}</ul><ul>Team: {{info.Team}}</ul></ol><li>Number: {{info.Number}}</li><li>Time: {{info.Time}}</li><ol>" inside the layout.json
Thanks! But If layout file very large (1000 lines). I think your solution will have some problems. Do you have another way?
0

I found a solution from my friend: code as follow , and it work for my case. He used swig , a template engine:

// get json object from json file
var info = require('./data.json');

var wkhtmltopdf = require('wkhtmltopdf');
var swig  = require('swig');
// Compile a file and store it, rendering it later
var tpl = swig.compileFile('public/template.html');

var layout = tpl({ info: info});

function generatePdf(layout,info) {
    wkhtmltopdf(layout, {output: './file.pdf'});
  }
exports.create_pdf_file = function() {
    generatePdf(layout,info);
}

I post answer. maybe someone need it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.