Skip to content

How to use "canvas?.toDataURL('image/png')" as Image source? #1403

Answered by kidroca
hbinkle asked this question in Q&A
Discussion options

You must be logged in to vote

I've used something similar to render SVGs on a web page as Images inside a PDF

If you already have the canvas rendered somewhere all you need to do is pass the url returned by canvas.toDataURL as <Image source={url} />

If you don't have it rendered you can try rendering on the fly and provide the result to the Image
The source can be a Promise returning function

Something like this should work

import { Image } from '@react-pdf/renderer';
import bwipjs from 'bwip-js';

export const BarcodeImage = (props) => (
  <Image source={() => getBarcodeUri(props)} />
);

const getBarcodeUri = async (props) => {
        const htmlCanvas = document.createElement('canvas');
        htmlCanvas.width = 120;

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hbinkle
Comment options

Answer selected by hbinkle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants