Open iKK001 opened 10 months ago
@iKK001 you need to provide the Arial.ttf
file so I can debug it
I am still clueless why the text is not rendered
My first thought was that it has nothing to do with the individual.ttf
files - but rather the fonts cannot be found for some reason.
So I made the following:
The font-file 'Arial.ttf' is now downloaded from a Firestore Storage Bucket. This way, I am absolutely sure to have the correct file at run-time.
import { getStorage } from 'firebase-admin/storage';
import { GlobalFonts, Image, loadImage } from '@napi-rs/canvas';
const fontFile = getStorage().bucket().file(`assets/fonts/Arial.ttf`);
const [fontUrl] = await fontFile.getSignedUrl({
version: 'v4',
action: 'read',
expires: Date.now() + 60 * 60 * 1000 * 12, // 12 hrs
});
GlobalFonts.registerFromPath(fontUrl, 'Arial');
I know that it works becasue I did the same with a logo. And the logo renders correctly.
--> But the text still does not render at all !!!!!
--> The Font Registry is corrupt !!!!
This is really nerv wrecking.
By the way, everything works nice and smooth if I run the same code locally on my Mac.
But as soon as I deploy it to the Google Cloud Run node:18-alpine
Docker container, then NO TEXT IS SHOWN ANYMORE !!!!
P.S. I'm using version @napi-rs/canvas v0.1.44
"dependencies": {
"@napi-rs/canvas": "^0.1.44",
}
P.S. My nodeJS server runs nestJS - not sure if that plays a role at all. It shouldn't...
There is a temporary workaround:
You can use a npm package for canvas
fonts as this example shows:
import { GlobalFonts } from '@napi-rs/canvas';
GlobalFonts.registerFromPath(
require('@canvas-fonts/helveticaneue'),
'HelveticaNeue',
);
Just make sure to add the corresponding npm package to your package.json
file:
"dependencies": {
"@napi-rs/canvas": "^0.1.44",
"@canvas-fonts/helveticaneue": "^1.0.4",
// ...
}
There are many more such packages out there if you need other fonts.
Still, open questions to the original issue with Google Cloud Run and node:18-alpine
remain:
do I need to COPY font files in my Dockerfile ? If yes, how exactly ? How can I persist font-files in a nodeJS server application hosted on Google Cloud Run ? How does the font-registry work exactly ?
When using a font-downloaddURL (as the example from the previous comment with the bucket-storage shows): Why is the registry not working from a web-URL ? GlobalFonts.registerFromPath(fontUrl, 'Arial');
?? Is it due to the corrupt font or is this not possible at all ?
Thank you for clarifying more on this.
In the meantime I can live with the extra npm-package font.
@iKK001 You are an absolute legend, I was stuck for hours trying to figure out why my text won't render using a digital ocean VPS until I came across your temporary fix.
It seems to be an issue across multiple providers since we are both using different ones. Bumping this issue as I'm sure its relevant across even more. Thank you.
I deploy my nodeJS (and nestJS) application by Google Cloud Run with
node:18-alpine
. Everything works - except:The Text in my canvas is not rendered !!!!!! (funny enough, rectangles are rendered. It is only simple Arial-text that does not seem to render correctly).
I am running
node:18-alpine
linux on the Cloud Run Docker (see Dockerfile further down...).Here is my canvas code (see below).
And here is the result-image.
As you can see, the small rectangle is drawn. But the text is MISSING. Why ?????
For completeness, here is my Dockerfile:
P.S. As mentioned here, I also tried adding the following to the dockerfile - but WITHOUT SUCCESS :(