npm i react-native-html-to-pdf
- 向
AndroidManifest.xml
添加以下WRITE_EXTERNAL_STORAGE
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
使用:
import React, { Component } from 'react';
import {
Text,
TouchableHighlight,
View,
} from 'react-native';
import RNHTMLtoPDF from 'react-native-html-to-pdf';
export default class Example extends Component {
async createPDF() {
const options = {
html: '<h1>PDF 测试</h1>',
fileName: 'test',
directory: 'Documents',
};
const file = await RNHTMLtoPDF.convert(options);
// console.log(file.filePath);
alert(file.filePath);
}
render() {
return (
<View>
<TouchableHighlight onPress={this.createPDF}>
<Text>创建PDF</Text>
</TouchableHighlight>
</View>
);
}
}