React-pdf/renderer not uploading image from Plotly

150 Views Asked by At

I have an image from base64 data string I am generating from Plotly that I want to include as the src attribute in the Image tag. I am able to get the base64 data string but when I render the Image tag nothing shows up.

This is the base64 string that is saved in the variable 'url':

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUAAAADwCAYAAABxLb1rAAAAAXNSR0IArs4c6QAAFwBJREFUeF7tncuLHVd6wL9+3271S6+Wxz3tSXpMzIBBwyCaZOGQOGDsIC+0NvgP0EJayAgLJIyQQEJYC3mhlbe2wRstJOJVSMAxAUeBGBwGgqPMIEuy25Ja/b79UHf4bqu6q6vr3ltV91Z9Vad+tRGSquo75/ed+t1zqk6d6tjc3NwUNghAAAIlJNCBAEuYdaoMAQjUCCBAGgIEIFBaAgiwtKmn4hCAAAKkDUAAAqUlgABLm3oqDgEIIEDaAAQgUFoCCLC0qafiEIAAAqQNQAACpSWAAEubeioOAQggQNoABCBQWgIIsLSpp+IQgAACpA1AAAKlJYAAS5t6Kg4BCCBA2gAEIFBaAgiwtKmn4hCAAAKkDUAAAqUlgABLm3oqDgEIIEDaAAQgUFoCCLC0qafiEIAAAqQNQAACpSWAAEubeioOAQggQNoABCBQWgIIsLSpp+IQgAACpA1AAAKlJYAAS5t6Kg4BCCBA2gAEIFBaAgiwtKmn4hCAAAKkDUAAAqUlgABLm3oqDgEIIEDaAAQgUFoCCLC0qafiEIAAAqQNQAACpSWAAEubeioOAQggQNoABCBQWgIIsLSpp+IQgAACpA1AAAKlJYAAC5j6e/fuydmzZ+W9996TEydO1K3BjRs35LvvvpPr16/L/v37C1hTigyBdAkgwHT5bp/91q1bcvPmzV3RRkZG5Nq1azI5ORmrFGEC9M5/5coVOXbsWO18WQpwZmZGzpw5I0ePHpXTp0/vqk9Y2WJVuE07e2W8f//+rjMeP358T5nbFJLT5JwAAkw5Qd5FNzc3t0d2nhhOnjzZsCcXLCICjJ80j3VQdl5+9Iz0lONzLfoRCDDFDFarVbl06ZI8evSo7sWlMvvhhx/krbfeilySPA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deyJDAALGBBCgcQIIDwEI2BFAgHbsiQwBCBgTQIDGCSA8BCBgRwAB2rEnMgQgYEwAARongPAQgIAdAQRox57IEICAMQEEaJwAwkMAAnYEEKAdeyJDAALGBBCgcQIIDwEI2BFAgHbsiQwBCBgTQIDGCSA8BCBgRwAB2rEnMgQgYEwAARongPAQgIAdAQRox57IEICAMQEEaJwAwkMAAnYEEKAdeyJDAALGBBCgcQIIDwEI2BFAgHbsiQwBCBgTQIDGCSA8BCBgRwAB2rEnMgQgYEwAARongPAQgIAdAQRox57IEICAMQEEaJwAwkMAAnYEEKAdeyJDAALGBBCgcQIIDwEI2BFAgHbsiQwBCBgTQIDGCSA8BCBgRwAB2rEnMgQgYEwAARongPAQgIAdAQRox57IEICAMYH/B7adGyHM5d6fAAAAAElFTkSuQmCC

This is my code:

import React, { useState, Fragment  }  from 'react';
import { Image, Document, Page, Text, View, StyleSheet, PDFViewer } from '@react-pdf/renderer';
import Plot from 'react-plotly.js';
import * as Plotly from "plotly.js";

function Analyze(props:{items:string; analysisResults:any; recievedJson:boolean}) {
    const [url, setUrl] = useState('');
    const [isPngDownload, setDownloadPng] = useState(false);
    const plot = React.createElement(Plot,
            { data: [
                {
                    x: [1, 2, 3, 4],
                    y: [1, 2, 3, 4],
                    type: 'scatter',
                    mode: 'lines+markers',
                    marker: {color: '#add8e6'},
                },
                ]
            , layout: { width: 320, height: 240, title: 'Calibration Curve'}
            ,divId: "calCurve"
            })

    const downloadPdf = async() => {
            const url = await Plotly.toImage("calCurve",  {format: 'png', width: 320, height: 240});
            setUrl(url);
            setDownloadPng(true)
            console.log(url)
        }
        
    const pdfButton =  React.createElement("button",
            {
              className: "panel-btn-close hide-btn",
              onClick: downloadPdf
            },
            "Download PDF")

    return (
        <div className="plotly_div">
            <div>
                {plot}
            </div>
            <br />
            {pdfButton}
            {isPngDownload &&
                <Fragment>
                    <PDFViewer width="1000" height="600">
                        <Image src={url}/>
                    </PDFViewer>
                </Fragment>
            }
        </div>

}

According to the console.log in the downloadPdf function, I am definitely getting back the base64 value (see above) and it is being set to the url variable. So later when I select the download Pdf button it should have everything it needs to render the image.

I looked at this stackoverflow question as it appears to be the exact solution I'm looking for but for some reason it is not working for me (React-pdf show image from base64 data string)

I'm wondering if maybe there is an issue with the version of react-pdf and plotly I am using. For reference:

"dependencies": {
    **"@react-pdf/renderer": "^3.1.12",**
    "assert": "^2.0.0",
    "browserify-zlib": "^0.2.0",
    "buffer": "^6.0.3",
    "concurrently": "^8.0.1",
    "cross-env": "^7.0.3",
    "electron": "^24.3.1",
    "electron-cgi": "^1.0.6",
    **"plotly.js": "^2.24.2",**
    "process": "^0.11.10",
    "react": "^18.2.0",
    "react-csv": "^2.2.2",
    "react-dom": "^18.2.0",
    **"react-plotly.js": "^2.6.0",**
    "react-router-dom": "^6.11.2",
    "react-scripts": "^5.0.1",
    "stream": "^0.0.2",
    "stream-browserify": "^3.0.0",
    "util": "^0.12.5",
    "wait-on": "^7.0.1"
  },

If anyone could please help me understand what I'm missing I would greatly appreciate it. Thanks!

0

There are 0 best solutions below