Generate PNG/JPEG images from HTML with simple API in Node.js.
This library use electron to generate PNG/JPEG images from HTML.
Install the lib and add it as a dependency :
npm install @lesjoursfr/html-to-image
Then put this in your code:
const { HtmlToImage } = require("@lesjoursfr/html-to-image");
const htmlToImage = new HtmlToImage(target, output, selector, outputType);
htmlToImage
.render()
.then(() => {
console.log("Image Generated Successfully!");
})
.catch((err) => {
console.error("Failed to generate Image because of ", err);
});target: The URL of the HTML pageoutput: The image file pathselector: The CSS selector of the content to captureoutputType: The image type ("png" or "jpeg", default to "jpeg")
Previously, the electron npm package would download the Electron binary from the repository's GitHub Releases in the package's postinstall script. With recent supply chain security attacks against the npm ecosystem with postinstall scripts as a common attack vector, Electron will now download itself dynamically the first time that its main bin script is run (e.g. via npx electron). With this change, you can now use Electron with the npm --ignore-scripts flag.
If you need to download the Electron binary on-demand, you can now call the install-electron script:
npx install-electronSee the release post for more details.