安装

npm install @react-pdf/renderer —save

使用

  1. import React from 'react';
  2. import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
  3. // Create styles
  4. const styles = StyleSheet.create({
  5. page: {
  6. flexDirection: 'row',
  7. backgroundColor: '#E4E4E4'
  8. },
  9. section: {
  10. margin: 10,
  11. padding: 10,
  12. flexGrow: 1
  13. }
  14. });
  15. // Create Document Component
  16. const MyDocument = () => (
  17. <Document>
  18. <Page size="A4" style={styles.page}>
  19. <View style={styles.section}>
  20. <Text>Section #1</Text>
  21. </View>
  22. <View style={styles.section}>
  23. <Text>Section #2</Text>
  24. </View>
  25. </Page>
  26. </Document>
  27. );

参考资料

https://react-pdf.org/
https://github.com/diegomura/react-pdf