-
Hi, Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 If you don't have it rendered you can try rendering on the fly and provide the result to the 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; // I'm not sure about width and height values
htmlCanvas.height = 120;
let canvas = bwipjs.toCanvas(htmlCanvas, {
bcid: 'code128', // Barcode type
text: '0123456789', // Text to encode
scale: 3, // 3x scaling factor
height: 10, // Bar height, in millimeters
includetext: true, // Show human-readable text
textxalign: 'center', // Always good to set this
});
return htmlCanvas.toDataURL('image/png', 1.0);
} You can use It doesn't actually look like anything async is happening so you ca probably just pass In case you have the canvas already rendered somewhere the logic is simpler export const BarcodeImage = (props) => (
<Image source={getBarcodeUri(props)} />
);
const getBarcodeUri = async (props) => {
const htmlCanvas = document.getElementById('myCanvas');
return htmlCanvas.toDataURL('image/png', 1.0);
} Another way would be to pass the export const BarcodeImage = ({ canvasRef, ...props }) => (
<Image source={canvasRef.toDataURL('image/png', 1.0)} />
); |
Beta Was this translation helpful? Give feedback.
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 bycanvas.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 functionSomething like this should work