krt
krt

Reputation: 23

Using jsrender to render and return a pdf-file from jsreport

How to pass jsrender template to jsreport?

jsreport content section requires html. How can I use that jsrender template in jsreport content section

var jsreport = require('jsreport');

// Require the jsrender node module
var jsrender = require('jsrender');

// Load a template 
var tmpl = jsrender.templates('./personTemplate.html');

// Render
var html = tmpl.render({data: 'hello'});

// Generating PDF file using jsreport
jsreport.render({
    template: {
        content: ,                
        engine: "jsrender",
        recipe: "phantom-pdf"
    }
}).then(function(out){         
    out.stream.pipe(fs.createWriteStream('..//test.pdf'));
});

Upvotes: 2

Views: 1988

Answers (2)

dipneupane
dipneupane

Reputation: 63

As you know the content for the Jsreport is .html file. So first you have to read the .html file using your server side code and pass that as the content for your jsreport.

Let me assume that you are using c# as your server side and your content.html file(template) is in the location like this.

string _contentPath = @"D:/WF/DemoApp/Templates/content.html";

now let's read the file content as following.

public string ReadContent()
{
  return File.ReadAllText(_contentPath);
}

Now pass the the content in Jsreport as following.

template: {
    content: ReadContent(),                
    engine: "jsrender",
    recipe: "phantom-pdf"
}

This is what I have done and fixed the similar issue in my project. Further Read the article about Getting Started with JsReport for more Information.

Upvotes: 0

Jan Blaha
Jan Blaha

Reputation: 3095

jsreport will compile and render jsrender template for you. You are suppose to pass the template string to content.

jsreport.render({
        template: {
            //content: '{{:foo}}'
            content: fs.readFileSync('personTemplate.html'),                
            engine: "jsrender",
            recipe: "phantom-pdf",
            data: { 'foo': 'hello' }
        }
    })

If you want compile and render the template on your own, you can set the engine to none and provide final html to the content.

Upvotes: 1

Related Questions