问题
I'm trying to print generated barcode but it prints as [object, object] tried using JSON.stringify and all other sources that i got but didn't work. Could someone suggest what is the best way to do this.
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV
Contents</title>');
printWindow.document.write('</head><body >');
for(var i=0; i<selectedRows.length;i++){
printWindow.document.write(
<Barcode
value="Rakesh"//{this.state.value}
width="2"
height="100"
format="CODE128"
displayValue=""
fontOptions=""
font="monospace"
textAlign="center"
textPosition="bottom"
textMargin="2"
fontSize="20"
background="#ffffff"
lineColor="#000000"
margin="10"
marginTop=""
marginBottom=""
marginLeft=""
marginRight=""
/>
);
}
printWindow.document.write('</body></html>');
printWindow.document.close();
// printWindow.print();
回答1:
Depending on the version of React you're using, you want to wrap that component in a React.renderToString
, React.renderComponent
, or a ReactDOMServer.renderToString
call. Of course, this also means that you need to make sure React/ReactDOMServer is within the scope of the popup window generator. See also this answer.
var ReactDOMServer = require('react-dom/server');
// ...
ReactDOMServer.renderToString(<Barcode
value="Rakesh"// {this.state.value}
width="2"
height="100"
format="CODE128"
displayValue=""
fontOptions=""
font="monospace"
textAlign="center"
textPosition="bottom"
textMargin="2"
fontSize="20"
background="#ffffff"
lineColor="#000000"
margin="10"
marginTop=""
marginBottom=""
marginLeft=""
marginRight=""
/>
)
来源:https://stackoverflow.com/questions/47708955/convert-react-barcode-to-pdf-format-to-print