UNPKG

from-data-to-pdf

Version:

This library converts html files, URL and character string from html files to PDF files or PDF buffer.

147 lines (125 loc) 3.82 kB
const a = require("../dist/index"); const responseModel = { name: undefined, failed: false, time: undefined, error: { expected: undefined, res: undefined } } const result = []; main(); async function main() { const data1 = [ { projectName: "Test23", fileName: "template1/project1.html" } ]; const data2 =[ { name: 'Google', url: 'https://www.google.com' } ]; // const result = await a.fromHtmlFileToPdf(data1, true, { // toGetFiles: 'D:/DEV_nodejs/MyPackages/fromDataToPDF/temp/target', // toSaveFiles: 'D:/DEV_nodejs/MyPackages/fromDataToPDF/temp/target' // }); // console.log(result); // to check if is an app // if ((process.config.variables as any).built_with_electron) console.log((process.config.variables as any).built_with_electron); await testInitDefaultFolder(); await testFromHtmlFileToPdf(); await formatResult(); } async function testInitDefaultFolder() { let res = {...responseModel}; // SUCCESS // Return void try { const e = []; const r = await a.initDefaultFolder(); if (typeof r !== 'boolean') e.push('Response type error') if (e.length > 0) throw e; } catch (error) { res.error.res = error; res.failed = true; res.error.expected = null; } finally { res.name = "InitDefaultFolder"; result.push(res); res = {...responseModel}; } return; } async function testFromHtmlFileToPdf() { let res = {...responseModel}; const e = []; const data = [ { projectName: "Test23", fileName: "template1/project1.html" }, { projectName: "Test553", fileName: "template1/project1.html" } ]; try { const before = Date.now(); const r = await a.fromHtmlFileToPdf(data, false); res.time = Date.now() - before; console.log(res.time) if (!Array.isArray(r)) e.push('Response type error'); console.log('IS ARR', Array.isArray(r)) if (r.length !== 5) e.push('Response length error'); if (!r[0].name && !r[0].buffer) e.push('Response param error'); if (e.length > 0) throw e; } catch (error) { res.error.res = error; res.failed = true; res.error.expected = null; } finally { res.name = "fromHtmlFileToPdf"; result.push(res); res = {...responseModel}; } return; } async function formatResult() { console.log('\n*\n*\n*\n*\n*\n*'); const length = result.length; const failed = result.filter(x => x.failed).length; const rate = 100 - failed / length * 100; console.log('\x1b[1m\x1b[4mRESULT\x1b[0m\x1b[0m - ' + rate + '%\n'); for (let i = 0; i < length; i++) { let res = result[i].failed ? ' \x1b[91mFAILED\x1b[0m ' : ' \x1b[92mSUCCESS\x1b[0m '; result[i].name = colorYellow(result[i].name); let str = 'TEST ' + res + result[i].name ; if (result[i].time) { str += ' '+ result[i].time +'ms '; } if (result[i].failed) { str += ' MESSAGE: ' + result[i].error.res.map(x => colorRed(x)).join(' - '); } console.log(str); } } function colorPurple(str) { return '\x1b[95m' + str + '\x1b[0m'; } function colorRed(str) { return '\x1b[91m' + str + '\x1b[0m'; } function colorGreen(str) { return '\x1b[92m' + str + '\x1b[0m'; } function colorYellow(str) { return '\x1b[93m' + str + '\x1b[0m'; }