繁体   English   中英

如何在使用react-pdf库创建的react中保存pdf?

[英]How to save pdf in react created using react-pdf library?

我正在创建一个 React 应用程序,我必须在其中生成一个 pdf 并在单击按钮时保存该 pdf。 如何将此 pdf 保存在我的本地目录中?

创建PDF.js

import React, { Component } from 'react';
import { Document, Page, Text, View, StyleSheet, Image } from '@react-pdf/renderer';

class CreatePdf extends Component {
    constructor() {
        super();
        this.state = {
            styles: {} // all styles here
        }
    }

MyDocument = () => (
        <Document>
            <Page style={this.state.styles.body}>
                <Text style={this.state.styles.header} fixed>
                    ~ Created with react-pdf ~
                </Text>
            </Page>
        </Document>

    render() {
        return (
            <button onClick={(this.MyDocument, `${__dirname}/example.pdf`)}>Print PDF</button>
        )
    }
}
export default CreatePdf;

应用程序.js

render() {
    return (
         <MyDocument />
    )
}

至于现在,当我点击按钮时,什么也没有发生。 就像在jsPDF 中一样,我们有.save()类似地我们如何保存这个 pdf ?

您可以使用 PdfDownLoadLink 包装您的文档以下载链接

import { PDFDownloadLink} from '@react-pdf/renderer';


<PDFDownloadLink document={<MyDocument />} fileName={"FileName"}> 

       <button> Download </button> 

</PDFDownloadLink> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM