user2717619 February 2016

Node JS, Ideas on Template For PDF Creation

How to create PDF Documents in Node.JS.? Is there any better solution to manage templates for different types of PDF creation.

I am using PDFKit to create PDF Documents and this will be server side using Javascript. I can not use HTML to create PDF. It will blob of paragraphs and sections with replacing tags with in.

Does anyone know Node.js has any npm package that can deal templates with paragraphs sections headers.

Something like getTemplateByID() returns a template that contains sections , headers, paragraphs and then i use to replace appropriate tags within the template.

Answers


Nicolas Leucci February 2016

In my case, I have to get my HTML template from my database (PostgreSQL) stocked as stream. I request the db to get my template and I create a tmp file.

Inside my template, I have AngularJS tags so I compile this template with datas thanks to the 'ng-node-compile' module:

var ngCompile        = require('ng-node-compile');
var ngEnvironment    = new ngCompile();
var templateHTML     = getTemplateById(id);

templateHTML         = ngEnvironment.$compile(templateHTML)(datas);

Now I have my compiled template (where you can set your paragraph etc.) and I convert them into PDF thanks to a PhantomJS module 'phantom-html-to-pdf'

var phantomHTML2PDF = require('phantom-html-to-pdf')(options);
phantomHTML2PDF(convertOptions, function (error, pdf) {
    if(error) console.log(error);
        // Here you have 'pdf.stream.path' which is your tmp PDF file
        callback(pdf);
    });

Now you have your compiled and converted template (pdf), you can do whatever you want ! :)

Useful links:

I hope this help !

Post Status

Asked in February 2016
Viewed 3,691 times
Voted 10
Answered 1 times

Search




Leave an answer