<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= title %></title>
</head>
<body style="font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size:10px; padding:20px 40px; margin: 0; line-height:1.5;">
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<% for( let i in column ){ %>
<th><%- column[ i ] %></th>
<% } %>
</tr>
<tr>
<% for( let i in value ){ %>
<% for( let v in value[ i ] ){ %>
<td><%- value[ i ][ v ] %></td>
<% } %>
<% } %>
</tr>
</table>
</body>
</html>
let params = {
"title": fileName,
"column": arrayColumn,
"value": resultData["result"]
}
ejs.renderFile( manifest.file.templ.pdf, params, ( err, html ) => {
if( err ) console.log( err );
const options = { "format": "Letter"}
console.log(`${timePath}${fileName}`);
convert_HTML_PDF.create( html, options ).toFile( `${timePath}${fileName}`, (err, res) => {
console.log(res);
if( err ) console.log( err );
})
})
{
"name": "html-pdf-converter",
"version": "1.0.0",
"description": "HTML to PDF converter",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "2.5.7",
"html-pdf": "^3.0.1"
}
}
const http = require('http');
const fs = require('fs');
const port = 6006;
const htmlPdf = require('html-pdf');
const ejs = require('ejs');
const requestHandler = (request, response) => {
if (request.url.indexOf('.') != -1){
response.end(fs.readFileSync(__dirname + request.url));
} else {
let date = new Date();
let month = date.getMonth() + 1;
month = (month < 10 ? '0' + month : month);
let day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
date = day + '.' + month + '.' + date.getFullYear();
const params = {
name: "Аида Дроган",
email: "aida.drogan.box@gmail.com",
id: "127001",
company: "#BlondieCode",
logo: "/images/logo.png",
date: date,
quantity: 1,
price: 66,
currency: "BTC",
product: "Вундер-Вафля (меч, серебро)",
units: "штука"
};
ejs.renderFile(__dirname + '/template.ejs', params, (err, html) => {
const options = { format: 'A4'};
const fileName = __dirname + '/file.pdf';
const renderHtml = html.replace(/img src=\"\//g, 'img src="file://' + __dirname + "/");
htmlPdf.create(renderHtml, options).toFile(fileName, (err) => {
if (err) {
console.log('Ошибка конвертации', err)
}
response.end(html);
});
})
}
};
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.log('Ошибка сервера', err)
}
console.log(`Вишу на порту ${port}`);
});