Converting svg to png in node - undefined buffer

Hi. I’m trying to convert my local svg image to png using svg2img (svg2img - npm). My code:

svg2img(__dirname+'/HellYeha.svg', function(error, buffer) {
        fs.writeFileSync('foo3.png', buffer);

it returns an error “TypeError [ERR_INVALID_ARG_TYPE]: The “data” argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined” Console.logging buffer returns ‘undefined’. How can I avoid this error and convert svg to png?

I tried your code and it works fine.
Possible issues:
1) SVG file is not in the same directory as the JS code you’re running.
2) You’re on windows and that forward slash in filename is messing up the path. Try to use path.join to create valid path.

  1. I’ve checked, the file is in the same directory as my server.js file where I’m running convert function
  2. I’ve rewritten path with path.join like this path.join(__dirname, 'HellYeha.svg') but the error persists.

Can you post a screenshot of your file directory?

index.js is my server where I run the conversion and HellYeha.svg is svg file I want to convert

Looks correct.
I’m running out of ideas, but maybe try to rename the svg file to only consist of lowercase letters.

No luck.
Can it be because svg file is too complicated? Here is an example of the file I’m trying to convert -

Yep, it’s because of the complexity of the file. Looks like those are limitations of the underlying canvg library.

1 Like

Ok, thx for your help. In the end of day I decided to make a screenshot of this svg with capture-website - npm

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.