在浏览器和服务器上创建 PDF 文件的反应渲染器
如何安装
yarn add @react-pdf/renderer
如何运作
import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
// Create styles
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4',
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
},
});
// Create Document Component
const MyDocument = () => (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<Text>Section #1</Text>
</View>
<View style={styles.section}>
<Text>Section #2</Text>
</View>
</Page>
</Document>
);
Web. 在DOM中渲染
import ReactDOM from 'react-dom';
import { PDFViewer } from '@react-pdf/renderer';
const App = () => (
<PDFViewer>
<MyDocument />
</PDFViewer>
);
ReactDOM.render(<App />, document.getElementById('root'));
Node. 在文件中保存
import ReactPDF from '@react-pdf/renderer';
ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);
实例
尝试打开 output.pdf 看结果.
|
文本 |
继续 |
分块 |
|
节点 |
页面包装 |
贡献者
这个项目的存在要归功于所有作出贡献的人. [贡献]。 。 。 .
发起人
感谢所有赞助商成为提案国[ . ]
备份
谢谢你所有的支持者成为后卫[ . ]
许可证
麻省理工学院 迭戈·穆拉乔勒