pdf-from-html
Advanced tools
Comparing version 0.1.0-beta.2 to 0.1.0-beta.3
79
index.js
@@ -12,37 +12,50 @@ const puppeteer = require('puppeteer'); | ||
* of an HTML page. | ||
* @param filePathInput path for input file | ||
* @param outputDir output folder path | ||
* @param outputFile output filename | ||
* @param inputContent content to be rendered | ||
*/ | ||
exports.generatePDF = (outputDir, outputFileName, inputContent) => { | ||
(async () => { | ||
// start a browser with puppeter | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.setJavaScriptEnabled(true); | ||
// we assume, the input received externally, only have the body | ||
// so here, we have a template in order to apply styles | ||
const templatePath = path.join(__dirname, templateFile); | ||
const templateContent = String(fs.readFileSync(templatePath)); | ||
// put all data together | ||
const view = { | ||
content: inputContent, | ||
}; | ||
// calls the render engine | ||
const output = Mustache.render(templateContent, view); | ||
// verify if the docs/ folder exist and creates it if not | ||
const destinationDocsFolderPath = `${process.cwd()}/${outputDir}/`; | ||
if (!fs.existsSync(destinationDocsFolderPath)) { | ||
fs.mkdirSync(destinationDocsFolderPath); | ||
exports.generatePDF = (outputDir, outputFile, inputContent) => { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
// start a browser with puppeter | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.setJavaScriptEnabled(true); | ||
// we assume, the input received externally, only have the body | ||
// so here, we have a template in order to apply styles | ||
const templatePath = path.join(__dirname, templateFile); | ||
const templateContent = String(fs.readFileSync(templatePath)); | ||
// put all data together | ||
const view = { | ||
content: inputContent, | ||
}; | ||
// calls the render engine | ||
const output = Mustache.render(templateContent, view); | ||
// verify if the docs/ folder exist and creates it if not | ||
const destinationDocsFolderPath = path.join(process.cwd(), outputDir); | ||
if (!fs.existsSync(destinationDocsFolderPath)) { | ||
fs.mkdirSync(destinationDocsFolderPath); | ||
} | ||
// write it to a file | ||
let outputFileName; | ||
if (path.extname(outputFile).length > 0) { | ||
outputFileName = path.parse(outputFile).name; | ||
} else { | ||
outputFileName = outputFile; | ||
} | ||
const outputHTMLFilePath = path.join(process.cwd(), outputDir, `${outputFileName}.tmp.html`); | ||
const outputPDFFilePath = path.join(process.cwd(), outputDir, `${outputFileName}.pdf`); | ||
fs.writeFileSync(outputHTMLFilePath, output); | ||
// now, go to page | ||
await page.goto(`file://${outputHTMLFilePath}`, { waitUntil: 'networkidle2' }); | ||
await page.emulateMedia('screen'); | ||
// generate the pdf | ||
await page.pdf({ path: outputPDFFilePath, format: 'A4' }); | ||
// close the browser | ||
await browser.close(); | ||
} catch(e) { | ||
reject(e); | ||
} | ||
// write it to a file | ||
const outputHTMLFilePath = path.join(process.cwd(), outputDir, `${outputFileName}.html`); | ||
const outputPDFFilePath = path.join(process.cwd(), outputDir, `${outputFileName}.pdf`); | ||
fs.writeFileSync(outputHTMLFilePath, output); | ||
// now, go to page | ||
await page.goto(`file://${outputHTMLFilePath}`, { waitUntil: 'networkidle2' }); | ||
await page.emulateMedia('screen'); | ||
// generate the pdf | ||
await page.pdf({ path: outputPDFFilePath, format: 'A4' }); | ||
// close the browser | ||
await browser.close(); | ||
})(); | ||
resolve(0); | ||
}); | ||
} |
{ | ||
"name": "pdf-from-html", | ||
"version": "0.1.0-beta.2", | ||
"version": "0.1.0-beta.3", | ||
"description": "Generate PDF file using an HTML file as input for the body.", | ||
@@ -36,5 +36,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"expect.js": "^0.3.1", | ||
"mocha": "^6.1.3" | ||
"expect.js": "0.3.1", | ||
"mocha": "6.1.3" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
const expect = require('expect.js'); | ||
const fs = require('fs'); | ||
const { generatePDF } = require('../index'); | ||
@@ -6,11 +6,9 @@ | ||
describe('MapComments', () => { | ||
const filePath = 'test/example.html'; | ||
describe('generate with success', () => { | ||
it('generate a file with success', () => { | ||
it('generate a file with success', async () => { | ||
// verify | ||
generatePDF(filePath); | ||
}); | ||
const exampleContent = String(fs.readFileSync('test/example.html')); | ||
await generatePDF('test/result', 'example.html', exampleContent); | ||
}).timeout(10000); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77834
88
3