Convert react-barcode to pdf format to print

孤街浪徒 提交于 2020-01-06 07:02:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!