Hello,
I am looking to a way of creating dynamic images (something like BannerBear) but with this library: html2canvas.
Basically what it does is creating a JPG image from an HTML document. So you can create your own images. I installed html2canvas on my server where I have n8n, but I don’t know how to use it with n8n.
Following this tutorial, it is easy creating a JPG. You just need to include the library to your HTML head section: <script src="assets/js/lib/html2canvas.js"></script>
And then call the html2canvas method. Here’s an example:
//var container = document.getElementById("image-wrap"); //specific element on page
var container = document.body; // full page
html2canvas(container).then(function(canvas) {
var link = document.createElement("a");
document.body.appendChild(link);
link.download = "html_image.png";
link.href = canvas.toDataURL("image/png");
link.target = '_blank';
link.click();
});
But how to use it on n8n? How can I get the image result and process it on n8n?
Do not think that you can use that library, and it seems to work just on the client-side. There must be alternatives libraries to achieve the same result in nodejs.
Thank you!
Sorry because I don’t know a lot about servers…
I have installed Puppeteer on my server, as your link suggests. But now I don’t know how to use it on n8n. If I try to run in on a Function node I get the error ERROR: Access denied to require 'puppeteer'.
I tried the two solutions here but I didn’t manage it to work…
You need to create a custom image that has that library installed. Then you have to the env variable NODE_FUNCTION_ALLOW_EXTERNAL with the name of the package.
Finally I managed to start n8n with that ENV variable, following these instructions. I added this line to my docker-compose.yml file:
NODE_FUNCTION_ALLOW_EXTERNAL=puppeteer
Now, when I start n8n again and test a Function node with const puppeteer = require('puppeteer'); on it, I get this error: ERROR: The module 'puppeteer' is not whitelisted in VM.
I searched for more information about this error and I found this, where @jan says "add the package to your packages/cli/package.json" but I don’t found this directory. It doesn’t exists. I do have an /home/n8n/package.json with this content: