#!/usr/bin/env node

/** 
 * Script to print a pdf from a html snippet, as input to stdin
*/

const htmlPdf = require('html-pdf-chrome');

const options = {
    port: 9222, // port Chrome is listening on
    printOptions: {
        printBackground: true,
        paperWidth: 8.267717,
        paperHeight: 11.69291,
        marginTop: .5,
        marginRight: .5,
        marginBottom: .5,
        marginLeft: .5
    },
};

const print = (data, options) => {
    htmlPdf.create(data, options).then((pdf) => {
        const pdfStream = pdf.toStream();
        pdfStream.pipe(process.stdout);
    });
}

'Read from stdin...'
console.log("Reading data from stdin");
const stdin = process.stdin;
// stdin.setEncoding('utf-8');
stdin.resume();
stdin.once('data', data => {
    console.log(data.toString());
    print("<html><head><meta charset=\"UTF-8\"><style>.entry { font-family: 'Fantasque Sans Mono'; line-height: 1.4; font-size: 125%; border-bottom: 1px solid black; display: block; padding-bottom: 1.4em; }</style></head><body>" + data.toString().replace(/\n/g, '<br />') + "</body></html>", options);
});