Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Does not export images in React #170

Open
m9rco opened this issue May 15, 2018 · 4 comments
Open

Does not export images in React #170

m9rco opened this issue May 15, 2018 · 4 comments
Labels
bug needs reproduction An bug that needs a complete example in order to reproduce

Comments

@m9rco
Copy link

m9rco commented May 15, 2018

Before that, I began to SVG files to be included in the state

const syncShow =  (<svg
        id="diagram"
        viewBox="0 0 640 640"
        width="100%"
        height="100%"
>...</svg>)
this.setState({syncShow});

The following is my export examples

exportExample = () => {
  return saveSvgAsPng(document.getElementById('diagram'), 'diagram.png');
}
@exupero exupero changed the title I use it in the React,I find it does not export images Does not export images in React May 16, 2018
@exupero exupero added the bug label May 16, 2018
@exupero
Copy link
Owner

exupero commented May 16, 2018

Could you provide a JSFiddle or other minimal reproduction of the problem so I can investigate? Right off, I can't think of any reason it wouldn't work from a DOM constructed by React.

@exupero exupero added the needs reproduction An bug that needs a complete example in order to reproduce label May 16, 2018
@m9rco
Copy link
Author

m9rco commented May 16, 2018

I used React for about 2 minutes.Sometimes they fail.

I used JSX as HTML in the following manner.

import React from 'react';

function getDisplayName(ele) {
  if (typeof ele.type === 'string') {
    return ele.type;
  }
  return ele.type.name || ele.type.displayName || 'No Name';
}

function getAttrs(attrs) {
  return Object.keys(attrs).map(attr => (attr === 'children' ? '' : `${attr}="${attrs[attr]}"`)).join('');
}

export default function transfer(ele) {
  if (typeof ele === 'string' || typeof ele === 'number') {
    return ele;
  }

  const props = ele.props || {};
  const children = React.Children.toArray(props.children || []);

  const html = children.map(transfer);
  const tag = getDisplayName(ele);

  return `<${tag} ${getAttrs(props)}>${html.join('')}</${tag}>`;
}

@exupero
Copy link
Owner

exupero commented May 17, 2018

I'm going to need a working JSFiddle that reproduces the failure or error you're encountering. The above code doesn't look like any React code I'm familiar with.

@vojto
Copy link

vojto commented Oct 2, 2019

I had the same issue with React. This was my code:

<image
  href="http://example.com/image.jpg"
  width={32}
  height={32}
  x={0}
  y={0}
  preserveAspectRatio="xMidYMid slice"
/>

After running svgAsPngUri, I noticed that the library added another href attributed, which contained my image as base64 string. When inspecting DOM, I saw href attribute twice, one with my original URL, the other with base64 string of my image.

I assume it was trying to replace remote link with a local representation of image - to avoid starting another network request during load. (Probably related to #223)

Anyway, simple solution, I just replaced href with xlinkHref in my React render:

<image
  xlinkHref="http://example.com/image.jpg"
  width={32}
  height={32}
  x={0}
  y={0}
  preserveAspectRatio="xMidYMid slice"
/>

Now when I inspect DOM, I see only one xlink:href attribute, and its content is set to base64 representation of my image. The original URL is gone, which is fine.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug needs reproduction An bug that needs a complete example in order to reproduce
Projects
None yet
Development

No branches or pull requests

3 participants