使用 react-pdf 制作pdf or 简历生成的使用及遇见的中文乱码问题

使用 react-pdf 制作pdf or 简历生成的流程及遇见的中文乱码问题

安装插件
yarn add @react-pdf/renderer

1、创建基本文档

// 个人简历页面开发
import React,{memo,useState} from 'react'
import {PdfGeneratorWrapper} from "./style";
import {PDFViewer} from '@react-pdf/renderer'
import PdfView from '../../../../components/pdf-view/pdf-view'

const PdfGenerator = memo(function () {
  const [profile, setProfile] = useState({
    name: "嘿哈",
    about: "are you ok",
    email: "maqi1520@qq.com",
    avatar:"https://p6-passport.byteacctimg.com/img/user-avatar/585e1491713363bc8f67d06c485e8260~300x300.image",
  })


  return (
    <PdfGeneratorWrapper>
      <div style={{ flexGrow: 1 ,height:'900px'}} className={'right-pdf'}>
        <PDFViewer
          showToolbar={false}
          style={{
            width: '80%',
            height: '95%',
          }}
        >
          <PdfView profile={profile} />
        </PDFViewer>
      </div>
    </PdfGeneratorWrapper>
  )
})


export default PdfGenerator
// 个人简历页面开发
import React,{memo} from 'react'
import {styles} from "./style";
import { Document, Page,Text ,Image,View} from '@react-pdf/renderer'


const PdfView = memo(function (props) {
  const {profile} = props
  console.log(profile)
  return (
      <Document>
        <Page size='A4' style={styles.page}>
          <View style={styles.section}>
            <Image src={profile.avatar} />
            <View style={styles.section}>
              <Text style={styles} >{profile.name}</Text>
            </View>
            <Text style={styles}>{profile.about}</Text>
            <Text style={styles}>{profile.email}</Text>
          </View>
        </Page>
      </Document>
  )
})

export default PdfView

// 样式表
import {Font, StyleSheet} from "@react-pdf/renderer";
const aa = require('./mini.ttf')

Font.register({
  family: 'ariblk',
  src:aa
})
export const styles = StyleSheet.create({
  // 一个页面
  page:{
    fontFamily:'ariblk',
    flexDirection: 'column',
    backgroundColor: '#E4E4E4'
  },
  // 页面中的一个区域
  section:{
    flexDirection: 'column',
    backgroundColor: '#E4E4E4',
    justifyContent: 'center'
  }
});

这样最基本的一个展示就出来了,可以看官方文档 https://www.npmjs.com/package/@react-pdf/renderer

但是这时候其实会有一个中文乱码的问题

issue 中大家的解决办法 : https://github.com/diegomura/react-pdf/issues/267

解决办法就是通过 设置字体 来解决中文乱码的问题

import {Font, StyleSheet} from "@react-pdf/renderer";
const aa = require('./mini.ttf')

// 注册本地下载的字体文件并使用它就可以解决中文乱码的问题
Font.register({
  family: 'ariblk',
  src:aa
})
export const styles = StyleSheet.create({
  // 一个页面
  page:{
  // 使用自己本地下载的字体文件
    fontFamily:'ariblk',
    flexDirection: 'column',
    backgroundColor: '#E4E4E4'
  },
  // 页面中的一个区域
  section:{
    flexDirection: 'column',
    backgroundColor: '#E4E4E4',
    justifyContent: 'center'
  }
});

使用

import {styles} from "./style";
const PdfView = memo(function (props) {
  const {profile} = props
  console.log(profile)
  return (
      <Document>
        <Page size='A4' style={styles.page}>
          <View style={styles.section}>
            <Image src={profile.avatar} />
            <View style={styles.section}>
              <Text style={styles} >{profile.name}</Text>
            </View>
            <Text style={styles}>{profile.about}</Text>
            <Text style={styles}>{profile.email}</Text>
          </View>
        </Page>
      </Document>
  )
})

最终中文乱码解决,这时候就可以开心的按照编写 html 格式的方式来编写自己的代码啦

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
React使用react-pdf插件来实现弹窗预览PDF的功能是可行的。根据你提供的引用内容,你已经选择了react-pdf插件来实现这个需求。为了解决react-pdf插件的一些问题,你可以在引入react-pdf的下一行代码中添加以下代码来解决问题pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`。这段代码可以确保react-pdf插件正常工作。此外,你还提到了react-file-viewer插件可以用来预览docx和xlsx格式的文件。所以,你可以使用react-pdf插件来预览PDF文件,而使用react-file-viewer插件来预览docx和xlsx文件。这样可以满足你的需求。 #### 引用[.reference_title] - *1* *2* [react使用react-pdf弹窗预览pdf](https://blog.csdn.net/michen1127/article/details/131416422)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [React文件预览,React实现在线预览docx,xslx,pdf格式文件](https://blog.csdn.net/a82048195/article/details/107681577)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值