antd 实现pdf 预览_使用react-pdf-js插件实现pdf文件预览(canvas)及打印,指定区域打印时出现空白怎么弄?...

该博客介绍了如何在 Ant Design 的 React 应用中利用 react-pdf-js 插件来展示 PDF 文件,并提供了一个预览及打印的示例。在预览过程中,详细讲解了页面切换以及在指定区域打印时可能出现空白问题的解决方案。
摘要由CSDN通过智能技术生成

import React, { Component } from 'react';

import {Button, Modal, Pagination} from 'antd'

import PDF from 'react-pdf-js';

import test from '../../assets/files/test.pdf';

export default class index extends Component {

state = {

page: 1,

pages: 5,

visible: false,

}

onDocumentComplete = (pages) => {

this.setState({ page: 1, pages });

}

handlePageChange = (page) => {

this.setState({page});

}

showModal = () => {

this.setState({

visible: true,

});

}

hideModal = () => {

this.setState({

visible: false,

});

}

handlePrint = (id) => {

// console.log( document.getElementById(id).innerHTML)

document.body.innerHTML = document.getElementById(id).innerHTML;

window.print();

window.location.reload();

}

render() {

const { page, pages } = this.state;

return (

<div>

<Button type='primary' onClick={this.showModal}>预览PDF文件</Button>

<Modal

title="PDF测试打印"

visible={this.state.visible}

onOk={this.hideModal}

onCancel={this.hideModal}

okText="确认"

cancelText="取消"

centered

width='643px'

>

<div id='pdfPrint'>

<PDF file={test} onDocumentComplete={this.onDocumentComplete} page={page} />

<Pagination defaultCurrent={page} total={pages} pageSize={1} onChange={this.handlePageChange} />

<div style={{textAlign: 'center'}}>

<span>第{page}页</span>/

<span>共{pages}页</span>

</div>

</div>

<Button type='ghost' onClick={this.handlePrint.bind(this,'pdfPrint')}>打印</Button>

</Modal>

</div>

)

}

}

您可以使用 `vue` 框架配合 `antd` 组件库来实现前端预览 `jpg`、`pdf`、`ceb` 格式的文件。下面是一种可能的实现方式: 1. 安装必要的依赖: ```bash npm install vue-pdf ant-design-vue ``` 2. 在您的 Vue 组件中引入所需的组件: ```javascript import { Button, Modal } from 'ant-design-vue'; import PdfViewer from 'vue-pdf'; export default { components: { Button, Modal, PdfViewer }, data() { return { previewVisible: false, previewFile: null, fileFormat: '' }; }, methods: { handlePreview(file) { this.previewFile = file; this.previewVisible = true; }, handleClosePreview() { this.previewVisible = false; this.previewFile = null; } }, render() { return ( <div> {/* 按钮组件 */} <Button type="primary" onClick={() => this.handlePreview('file.jpg')}> 预览 JPG </Button> <Button type="primary" onClick={() => this.handlePreview('file.pdf')}> 预览 PDF </Button> <Button type="primary" onClick={() => this.handlePreview('file.ceb')}> 预览 CEB </Button> {/* 文件预览模态框 */} <Modal title="文件预览" visible={this.previewVisible} onCancel={this.handleClosePreview} footer={null} > {this.previewFile && ( <div> {this.previewFile.endsWith('.pdf') && ( <PdfViewer src={this.previewFile} /> )} {/* 其他文件格式的预览组件,根据实际需求自行选择 */} {/* 示例:<ImageViewer src={this.previewFile} /> */} </div> )} </Modal> </div> ); } }; ``` 3. 在示例代码中,我们通过 `Button` 组件触发 `handlePreview` 方法来打开文件预览模态框。预览模态框使用 `Modal` 组件展示,并根据文件格式选择相应的预览组件(示例中使用了 `vue-pdf` 组件来预览 PDF 文件)。 请注意,上述示例中的 `PdfViewer` 组件仅支持预览 PDF 文件,对于其他文件格式(如 `jpg`、`ceb`),您需要自行选择相应的预览组件或者编写自定义组件来进行预览。 另外,您可能需要根据实际情况进行样式调整和其他逻辑处理。希望以上代码对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值