Saturday, 6 May 2017

svg2png on Heroku - TypeError: Bad argument

I have a problem with svg2png package on Heroku. It works on my machine locally (Windows), but I can't get it working on Heroku free dyno.

The idea is to get svg from Wikipedia and return it as png. I've exposed two endpoints:

  • / - that uses code stored in separated file,
  • /simple - same code, but just in the callback.

When browsing both endpoints I receive Heroku Application error page. That's what I get in Heroku logs for both requests:

TypeError: Bad argument

Do you have an idea?

index.js

const svg2png = require('svg2png')
const axios = require('axios')
const express = require('express')
const bodyParser = require('body-parser')
const conv = require('./conv')

const svgUrl = 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg'

const app = express()

app.use(bodyParser.json({limit:'5mb'}))
app.use(bodyParser.urlencoded({extended:true,limit:'5mb'}))

app.get('/simple', function (req, res) {
    axios.get(svgUrl)
        .then(response => {
            return svg2png(response.data, { width: 150, height: 150 })
        })
        .then(pngFile => {
            var img = new Buffer(pngFile, 'base64')
            res.writeHead(200, {
                'Content-Type': 'image/png',
     'Content-Length': img.length
            })
            res.end(img)
        })
})

app.get('/', function (req, res) {
conv.send()    
        .then(pngFile => {
            var img = new Buffer(pngFile, 'base64')
            res.writeHead(200, {
                'Content-Type': 'image/png',
     'Content-Length': img.length
            })
            res.end(img)
        })
})

app.listen(process.env.PORT || 5000, function () {
    console.log('Application started')
})

conv.js

const axios = require('axios')
const svg2png = require('svg2png')

module.exports = {
    send() {
        const self = this;
                self.width = 300;
        self.height = 300;

        const svgUrl = 'https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg'
        return new Promise((resolve, reject) => {
            axios.get(svgUrl)
            .then(response => 

            svg2png(response.data,
             { width: self.width, height: self.height })
            .then(img=>resolve(img))
            .catch(e => {
                console.log('Something went wrong during svg2png conversion.' + e)
                reject(e)
            }))
        });

    }
}



via Robert Skarżycki

No comments:

Post a Comment