【HarmonyOS NEXT】在应用中如何预览Document中的PDF文件

【关键字】

预览Document中的PDF文件 / Web组件 / 传入本地PDF文件

【问题描述】

问题一:

项目中,用户选择文件夹中的PDF文件,返回一个文件路径(file://com.huawei.hmos.filemanager/data/storage/el2/cloud/%E6%A1%88%E4%BE%8B%E8%A6%81%E7%82%B9%E6%80%BB%E7%BB%93.pdf),使用Web组件加载报错。

问题二:

Web组件是否支持通过文档选择器(DocumentViewPicker)选择的PDF文件,传入file://*这样的path?

【解决方案】

问题一:

web组件预览PDF文件直接在组件的src属性传入文件的地址,支持本地和在线的。

参照文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-web-0000001774281246#ZH-CN_TOPIC_0000001774281246__mixedmode

参照Demo:

Web({ src: 'https://www.demo.test/cedes-Benz-G-Class.pdf', controller: this.controller })

问题二:

不支持直接传入本地PDF文件,可以通过本地H5加载本地PDF,然后使用web打开本地H5。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Pro 可以使用 `react-pdf` 这个第三方库来实现 PDF 文件预览和打印。具体实现步骤如下: 1. 安装 `react-pdf` 库 ``` npm install react-pdf ``` 2. 在组件引入 `Document`、`Page` 和 `pdfjs` 组件 ```javascript import { Document, Page, pdfjs } from 'react-pdf'; pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; ``` 3. 在页面使用 `Document` 和 `Page` 组件来渲染 PDF 文件 ```javascript <Document file="/example.pdf"> <Page pageNumber={1} /> </Document> ``` 其 `file` 属性指定要渲染的 PDF 文件路径,`Page` 组件的 `pageNumber` 属性指定要渲染的页码。 4. 添加打印功能 ```javascript import { pdfjs, Document, Page, pdf } from 'react-pdf'; import { Button } from 'antd'; pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; class PDFViewer extends React.Component { state = { numPages: null, pageNumber: 1, }; onDocumentLoadSuccess = ({ numPages }) => { this.setState({ numPages }); }; goToPrevPage = () => { this.setState(prevState => ({ pageNumber: prevState.pageNumber - 1 })); }; goToNextPage = () => { this.setState(prevState => ({ pageNumber: prevState.pageNumber + 1 })); }; printDocument = () => { pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; const loadingTask = pdfjs.getDocument(this.props.file); loadingTask.promise.then(pdf => { const printOptions = { ...pdfjs.DefaultPrintParams, printResolution: 150, embedJavascript: true, }; const pdfWindow = window.open('', '_blank', 'width=800,height=600,scrollbars=no'); pdfWindow.document.write('<html><head><title>Print</title></head><body>'); pdfWindow.document.write('<embed type="application/pdf" '); pdfWindow.document.write(`src="${this.props.file}" `); pdfWindow.document.write(`id="pdfDocument" `); pdfWindow.document.write(`print="print" `); pdfWindow.document.write(`options='${JSON.stringify(printOptions)}' `); pdfWindow.document.write('></embed>'); pdfWindow.document.write('</body></html>'); pdfWindow.document.close(); setTimeout(() => { pdfWindow.print(); }, 500); }); }; render() { const { pageNumber, numPages } = this.state; return ( <div> <p> Page {pageNumber} of {numPages} </p> <Document file={this.props.file} onLoadSuccess={this.onDocumentLoadSuccess}> <Page pageNumber={pageNumber} /> </Document> <Button disabled={pageNumber <= 1} onClick={this.goToPrevPage}> Previous </Button> <Button disabled={pageNumber >= numPages} onClick={this.goToNextPage}> Next </Button> <Button onClick={this.printDocument}>Print</Button> </div> ); } } export default PDFViewer; ``` 在上面的代码,我们添加了一个 `printDocument` 方法,该方法会打开一个新的窗口,并在窗口显示 PDF 文件,然后调用 `window.print()` 方法将 PDF 文件打印出来。注意,该方法使用了 PDF.js 库的 `getDocument` 方法来获取 PDF 文件的信息。 这样就可以在 Ant Design Pro 预览和打印 PDF 文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值