html-pdf-converter-node
Version:
you can use this package to convert html into pdf,you have to pass html,fileName,onSuccess and onError params, onSuccess,you will get file url and onError,you will get error if any occur while creating pdf.
34 lines (31 loc) • 834 B
JavaScript
const path = require("path");
const fs = require("fs");
const dotenv = require("dotenv").config();
const html_to_pdf = require("html-pdf-node");
const convertHTMLTOPDF = async (
htmlContent,
fileName,
folderName,
onSuccess,
onError
) => {
try {
let options = { format: "A4" };
let file = { content: htmlContent };
html_to_pdf
.generatePdf(file, options)
.then((pdfBuffer) => {
const name = `${Date.now()}-${fileName}.pdf`;
const filePath = path.join(process.cwd(), folderName, name);
fs.writeFileSync(filePath, pdfBuffer);
const url = `${process.env?.NODE_PDF_URL}/${folderName}/${name}`;
onSuccess(url);
})
.catch((error) => {
onError(error);
});
} catch (error) {
onError(error);
}
};
module.exports = convertHTMLTOPDF;