【问题标题】:How to open a pdf in reactjs?如何在reactjs中打开pdf?
【发布时间】:2017-05-21 03:43:35
【问题描述】:

对于 web api,我将 pdf 作为 :-

[EnableCors("*", "*", "*")]
[HttpGet]
public byte[] GetReportData()
{
 string filePath = "D:\\Decoding.pdf";
 byte[] bytes = File.ReadAllBytes(filePath);
 return bytes;
}

我在 action 和 reducer 中调用该方法:-

行动:-

export  function LoadReportData () {
    return (dispatch) => {
    dispatch({
      type: REQUEST_REPORT,//defining the name of the action to identify in the reducer
    });

    fetch(APIPATH+'Requisition/GetReportData')//calling the service
    .then((response) => response.json())
    .then((report) => dispatch(receiveReport(report)));//passing the data to other actions
  }
}


//this method use to pass the data coming for the lab test result to the component
export  function receiveReport(report) {

  return {
    type:RECEIVE_REPORT,//defining the name of the action to identify in the reducer
    report:report//passing the data to reducer
  };
}

减速器:-

case  'RECEIVE_REPORT':
        return Object.assign({}, state, {
          report: action.report,
          loadingReport: false,
        });
    case 'REQUEST_REPORT':
        return Object.assign({}, state, {
          loadingReport: true
        });

现在报告以我从 web api 传递的字节形式出现。现在我的要求是如何在我的组件或浏览器的下一个选项卡中显示这个来自 web api 的字节数组的 pdf 文件? 注意:报告包含字节数组

【问题讨论】:

    标签: reactjs react-redux


    【解决方案1】:

    您必须使用作为 jar 的 pdfbox 来打开 pdf 文件

    【讨论】:

      【解决方案2】:

      我正在发布我的代码,这对我来说很好用:-

      import React,{Component} from 'react'
      import ReactDOM from "react-dom";
      import PDF from "react-pdfjs";
      class PreviewComponent extends Component{
       constructor(props) {
          super(props);
          this.state={
            reportData:this.props.reportData,
            page:1,
            scale:1.0
          }
         this.onDocumentCompleted=this._onDocumentCompleted.bind(this);
           this.onPageCompleted=this._onPageCompleted.bind(this);
        }
      
       render() {
              return <PDF content={this.state.reportData} page={this.state.page} scale={this.state.scale} onDocumentComplete={this._onDocumentComplete} onPageComplete={this._onPageComplete} loading={(<span>Your own loading message ...</span>)} />
        }
        _onDocumentCompleted(pages){
          this.setState({pages: pages});
        }
        _onPageCompleted(page){
          this.setState({currentPage: page});
        }
      }
      export default PreviewComponent;
      

      【讨论】:

        【解决方案3】:

        你应该安装react-pdf

        Install with npm install react-pdf
        

        在您的应用中使用:

        var PDF = require('react-pdf');
        
        var MyApp = React.createClass({
          render() {
        
            return '<PDF content="YSBzaW1wbGUgcGRm..." page="1" scale="1.0" onDocumentComplete={this._onDocumentComplete} onPageComplete={this._onPageComplete} loading={(<span>Your own loading message ...</span>)} />';
          },
          _onDocumentCompleted(pages){
            this.setState({pages});
          },
          _onPageCompleted(page){
            this.setState({currentPage: page});
          }
        });
        

        【讨论】:

        • @Vadim 我试过了,但是“未捕获的 ReferenceError:PDFJS 未定义”作为错误出现
        猜你喜欢
        • 2017-05-24
        • 2019-08-12
        • 2012-05-15
        • 1970-01-01
        • 1970-01-01
        • 2013-05-09
        • 2020-06-01
        • 2019-06-23
        • 2011-09-27
        相关资源
        最近更新 更多