react使用jsbarcode生成条形码

一、安装插件

npm install jsbarcode --save

二、封装为一个单独的js文件,也可以不封装直接使用。

//封装为一个单独的文件 Barcode.js
import React, { Component } from 'react';
import JsBarcode from 'jsbarcode';

// 条形码
export default class Barcode extends Component {

  static defaultProps = {
    format: 'CODE128',
    renderer: 'svg',
    width: 1.6,
    height: 25,
    displayValue: true,
    textAlign: 'center',
    textPosition: 'bottom',
    textMargin: 6,
    fontSize: 14,
    background: '#ffffff',
    lineColor: '#000000',
    margin: 0,
    marginBottom: 0,
  };

  constructor(props) {
    super(props);
    this.update = this.update.bind(this);
  };

  componentDidMount() {
    this.update();
  };

  componentDidUpdate() {
    this.update();
  };

  handleBarcode = (r) => {
    this.barcode = r;
  }

  update() {
    const {
      value,
      format,
      width,
      height,
      displayValue,
      textAlign,
      textPosition,
      textMargin,
      fontSize,
      background,
      margin,
      lineColor,
      marginBottom,
    } = this.props;
    JsBarcode(this.barcode, value, {
      format,
      width,
      height,
      displayValue,
      textAlign,
      textPosition,
      textMargin,
      fontSize,
      background,
      margin,
      lineColor,
      marginBottom,
    })
  };

  render() {
    const { renderer } = this.props;
    if (renderer === 'svg') {
      return (
        <svg ref={this.handleBarcode} />
      );
    } else if (renderer === 'canvas') {
      return (
        <canvas ref={this.handleBarcode} />
      );
    } else if (renderer === 'img') {
      return (
        <img ref={this.handleBarcode} alt="" />
      );
    }
  };
}

三、在react组件中使用

import Barcode from './Barcode.js';
//jsx内
<Barcode value={'102457899633'} height={50} width={2} />
//value为生成条形码的字符串,可由后端返回

PS:不封装为js,直接使用

import JsBarcode from 'jsbarcode';
import React,{useState,useEffect,useRef} from 'react';
const Barcode = () =>{
	const [barcode,setBarcode] = useState(null); //设置生成条形码的初始串为空
	const barcoderef = useRef()
	useEffect(() =>{
		initData()
	},[])
	useEffect(() =>{
		//检测barcode有值了之后,可调用生成条形码方法
		JsBarcode(barcoderef, barcode, {
	      displayValue: false,
	      width: 1.5,
	      height: 50,
	      margin: 0,
	    });
	},[barcode])
	const initData = () =>{
		//可在此处调用接口请求,然后将接口返回的生成条形码的串赋值给barcode
		setBarcode('10938388489494')
	}
	return (
      <div className="barcode-box">
        <svg
          ref={barcoderef}
        />
      </div>
    );
}

如有问题 欢迎指教

转自:https://blog.csdn.net/Aym_zzz/article/details/106691253

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 React生成 PDF 并且自动分页,可以使用第三方库 React-PDF。该库提供了一个 `<Document>` 组件和 `<Page>` 组件,其中 `<Page>` 组件可以自动分页。 要生成表格,可以使用 `<Table>` 组件。以下是一个示例代码: ```jsx import React from 'react'; import { Document, Page, Text, View, StyleSheet, Font, Table, TableRow, TableCell } from '@react-pdf/renderer'; // 定义字体 Font.register({ family: 'Noto Sans', src: 'https://fonts.gstatic.com/s/notosans/v12/o-0IIpQlx3QUlC5A4PNb4j5Ba_2iuOw.ttf', }); const styles = StyleSheet.create({ page: { padding: 20, fontFamily: 'Noto Sans', }, section: { marginBottom: 10, }, table: { width: '100%', borderStyle: 'solid', borderWidth: 1, borderColor: '#000', borderLeftWidth: 0, borderTopWidth: 0, }, tableRow: { flexDirection: 'row', }, tableCell: { width: '33.33%', borderStyle: 'solid', borderWidth: 1, borderColor: '#000', borderRightWidth: 0, borderBottomWidth: 0, }, }); const MyDocument = () => { return ( <Document> <Page style={styles.page}> <View style={styles.section}> <Text>这是第一页</Text> </View> <View style={styles.section}> <Table style={styles.table}> <TableRow style={styles.tableRow}> <TableCell style={styles.tableCell}> <Text>列1</Text> </TableCell> <TableCell style={styles.tableCell}> <Text>列2</Text> </TableCell> <TableCell style={styles.tableCell}> <Text>列3</Text> </TableCell> </TableRow> {/* 生成多行 */} {Array.from(Array(20).keys()).map((index) => ( <TableRow style={styles.tableRow} key={index}> <TableCell style={styles.tableCell}> <Text>{index + 1}</Text> </TableCell> <TableCell style={styles.tableCell}> <Text>内容</Text> </TableCell> <TableCell style={styles.tableCell}> <Text>内容</Text> </TableCell> </TableRow> ))} </Table> </View> {/* 自动分页 */} <View style={styles.section}> <Text>这是第二页</Text> </View> </Page> </Document> ); }; export default MyDocument; ``` 在上面的代码中,我们定义了一个 `<MyDocument>` 组件,其中包含了一个表格。表格使用了 `<Table>`、`<TableRow>`、`<TableCell>` 组件,每个单元格都有一个样式 `styles.tableCell`。我们使用了 `Array.from(Array(20).keys())` 来生成表格的多行,同时也演示了自动分页的效果。 最后,我们将 `<MyDocument>` 组件导出,然后在需要使用的地方引入即可。例如可以使用以下代码将 PDF 文件保存到本地: ```jsx import ReactPDF from '@react-pdf/renderer'; import MyDocument from './MyDocument'; const savePDF = async () => { const pdfBlob = await ReactPDF.renderToBlob(<MyDocument />); saveAs(pdfBlob, 'my-document.pdf'); }; ``` 在上面的代码中,我们使用了 `ReactPDF.renderToBlob()` 方法将 `<MyDocument>` 组件渲染成 PDF 文件,并将其保存到本地。需要注意的是,我们使用了第三方库 FileSaver.js 来实现下载操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值