Vue项目中,利用iframe在线预览pdf/图片等

在components下新建 src\components\iFrame\index.vue

<template>
    <div v-loading="loading" :style="'height:' + height">
        <iframe :src="src" frameborder="no" style="width: 100%; height: 100%" scrolling="auto" />
    </div>
</template>
<script>
export default {
    props: {
        src: {
            type: String,
            required: true
        },
    },
    data() {
        return {
            height: document.documentElement.clientHeight - 94.5 + "px;",
            loading: true,
            url: this.src
        };
    },
    mounted: function () {
        setTimeout(() => {
            this.loading = false;
        }, 300);
        const that = this;
        window.onresize = function temp() {
            that.height = document.documentElement.clientHeight - 94.5 + "px;";
        };
    }
};
</script>

在components下新建 src/components/FilePreview/index.vue

<template>
    <el-dialog :title="title" :visible.sync="visible" @close="visible = false" :width="width" append-to-body v-dialogDrag :close-on-click-modal="false">
        <div :style="[{height:`${height}px`}]" class="styleObj">
            <template>
                <i-frame :src="url" />
            </template>
        </div>
    </el-dialog>
</template>

<script>
import iFrame from "@/components/iFrame/index";
export default {
    props: {
        //宽
        width: {
            type: String,
            default: '60%',
        },
        //高
        height: {
            type: Number,
            default: 500,
        },
        //标题
        title: {
            type: String,
            default: '文件预览',
        }
    },
    name: "FilePreview",
    components: { iFrame },
    data() {
        return {
            visible: false,
            url: ''
        };
    },
    methods: {
        preview(fileUrl) {
            if (fileUrl) {
                this.url = fileUrl;
                this.visible = true
            } else {
                this.$modal.msgError("文件不存在!");
            }
        },
    }
};
</script>

<style scoped>
.styleObj {
    overflow: auto;
}
</style>

在组件中引用

<template>
    <div>
        <!-- 其余代码省略 -->
        <FileViewer ref="FileViewer" :width="`80%`" :height="700"/>
    </div>
</template>

<script>
import FileViewer from "@/components/FilePreview"
export default {
    components: { FileViewer },
    data() {
        return {},
    },
    mounted(){
        let url = "文件路径";
        this.$refs.FileViewer.preview(url);
    }
}
</script>

效果如下

Vue 3 中使用 `<iframe>` 实现 PDF 预览,可以通过动态绑定 `src` 属性来加载 PDF 文件的地址。这种方式适用于嵌入 PDF 文件到页面中,并且可以避免重新打开新的浏览器页签。以下是一个完整的实现方法。 ### 示例代码 ```vue <template> <div class="pdf-container"> <iframe class="pdf-iframe" :src="pdfUrl" width="100%" height="600px" title="PDF 预览" ></iframe> </div> </template> <script> export default { data() { return { // PDF 文件的地址 pdfUrl: 'https://example.com/sample.pdf' // 替换为你的 PDF 地址 }; } }; </script> <style scoped> .pdf-container { margin: 20px; } .pdf-iframe { border: 1px solid #ccc; border-radius: 8px; } </style> ``` ### 动态更新 PDF 地址 如果需要根据用户操作动态更新 PDF预览内容,可以通过方法修改 `pdfUrl` 的值。例如: ```vue <template> <div class="pdf-container"> <input v-model="inputUrl" placeholder="输入 PDF 地址" /> <button @click="updatePdfUrl">更新预览</button> <iframe class="pdf-iframe" :src="pdfUrl" width="100%" height="600px" title="PDF 预览" ></iframe> </div> </template> <script> export default { data() { return { inputUrl: '', pdfUrl: 'https://example.com/sample.pdf' }; }, methods: { updatePdfUrl() { this.pdfUrl = this.inputUrl; } } }; </script> ``` ### 注意事项 - **浏览器兼容性**:大多数现代浏览器(如 Chrome、Edge)支持直接通过 `<iframe>` 或 `<embed>` 标签预览 PDF,但某些旧版本浏览器可能需要依赖 Adobe Acrobat 插件。 - **跨域问题**:如果 PDF 文件来自不同的域名,需确保服务器允许跨域请求,否则可能无法正确加载。 - **样式调整**:可以根据需要调整 `width` 和 `height` 属性,以适应不同设备的显示需求。 ### 浏览器设置 对于 Chrome 浏览器,可以前往 `chrome://settings/content/pdfDocuments` 设置页面,选择“在 Chrome 中打开 PDF 文件”选项,以便在 `<iframe>` 中直接预览 PDF 内容 [^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值