Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the contents of a screen in react native?

is there a way to print the contents of a screen, that contain images rendered, to a printer in React Native? I looked into react-native-print but it only accepts HTML text whilst react-native-xprinter only supports Android. Thanks.

like image 900
Jason Avatar asked Oct 17 '22 01:10

Jason


1 Answers

Managed to make it work. See codes below.

import RNPrint from 'react-native-print';

var htmlString = `<div style="(your CSS style here)"> (HTML contents here) </div>`;

RNPrint.print({html: htmlString});

If you want to embed an image in the HTML, see below:

var htmlString = `<div style="(your CSS style here)"> 
    <img src="data:image/png;base64, ${imgData}" style="width: $imgWidth; height: $imgHeight"/>
</div>`;

You can replace "image/png" with other image type. Hope this helps.

like image 67
Jason Avatar answered Oct 21 '22 00:10

Jason