How to handle if the src given in docx Hyperlink image is not available.



  • Hi, We are using docx report to generate the report. But when hyperlink is given to image and if the image is not available it Throws error 'Error: Error while executing docx recipe. getaddrinfo ENOTFOUND picsm.photos\n at module.exports ...........'.
    Is there any other methods I can render images ? for example with base64?
    and if the image is not available can I show nothing?


  • administrators

    hi! yes the image also accepts base64 input but in the shape of a base64 data uri, see this example that uses base64 data uri for the image.



  • What happens when the image is not available? It's giving Error: Error while executing docx recipe. getaddrinfo ENOTFOUND image.
    Is there any way around so that I can render remaining part of the report if image is not available. Please help..its urgent


  • administrators

    yes, right now it just throws an error, there is no special handling on this. perhaps we can consider implementing some kind of fallbackSrc when this happens, but this can also be done right now using helpers.

    here is link that shows how to implement a custom fallback https://playground.jsreport.net/w/anon/qM203vEC

    you need to update the helper call in your hyperlink to be like this

    0_1663951059839_Screenshot 2022-09-23 at 11.36.42@2x.jpg

    and in your template have this helper:

    const jsreport = require('jsreport-proxy')
    const axios = await jsreport.npm.require('axios@0.24.0')
    
    async function docxImageWithFallback (options) {
        const src = options.hash.src
    
        if (src.startsWith('data:')) {
            return src
        }
    
        try {
            console.log('sending request...')
            const response = await axios({
                url: src,
                responseType: 'arraybuffer',
                method: 'GET'
            })
    
            const contentType = response.headers['content-type'] || response.headers['Content-Type']
    
            if (!contentType) {
            throw new Error(`Empty content-type for remote image at "${src}"`)
            }
    
            const extensionsParts = contentType.split(';')[0].split('/').filter((p) => p)
    
            if (extensionsParts.length === 0 || extensionsParts.length > 2) {
            throw new Error(`Invalid content-type "${contentType}" for remote image at "${src}"`)
            }
    
            // some servers returns the image content type without the "image/" prefix
            imageExtension = extensionsParts.length === 1 ? extensionsParts[0] : extensionsParts[1]
            imageBuffer = Buffer.from(response.data)
            imgDataUri = `data:image/${imageExtension};base64,${imageBuffer.toString('base64')}`
        } catch (err) {
            imgDataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK8AAAAtCAIAAABXiKzxAAAAAXNSR0IArs4c6QAAAARnQU1BAACx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UVmEpIR0cHaoqMjNTpdLm5uQaDQfFYoCk/lg5Gc01uKtmNTSEQRYnyhe9k+M5Iu2tkfgicXtrngdsFusP5XTUFoA2mKtjmcHO3eq784W+YsrRCBdGUi9uMJMg7iaJRvGFK5oAziC/FQR+5/7yoAZBXdXzdLSa7m1n346WYAtPnh4/C9+AZZQL7jp1Iyy/MKtqesfVjw5ZthvxC45Zt4G9gwb9tn2z+sizt1/asbib9NrPpl8t49NBIohR9r6FBYrhPRkrZk7jDj1V4n4Gpohyz+vokTavJ5kGDgfUImwQodQr/9Uk8B/CH/wGcu4Mvr9x/vtQQFR2db2013GYyu5nP7jI03gcw40zgyINAyi0mtYvRhfyt72I2uJmNbialrjV21fS+T0+R+dkiTxWBI4waAGDNz9t3UH/uXnnPEBIBYmxi4si9wYyOwfybQ2F/m9ufZto6taY9YDvB44YFqoFyKCy9s4KoYWrCqyHIulTd/fv3kRTATdK8Z49mtXZtcsp0fgkaDR5lWpTV4q/P+GgwlxA1TM0M1ABIS0s7derU+fPni4qKsGlegBUDu7BbsqHPBUQNUzMzNRAWN0QNBB6iBgIPUQOBh6iBwEPUQOAhaiDwEDUQeIgaCDxEDQQeogYCh0r1H3rynmXRZBCKAAAAAElFTkSuQmCC'
        }
    
        return docxImage.call(this, {
            ...options,
            hash: {
                ...options.hash,
                src: imgDataUri
            }
        })
    }
    

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.