pro-collection / interview-question

目标:收集全网经典面试问题
536 stars 37 forks source link

如何实现预览 PDF 文件【热度: 173】 #803

Open yanlele opened 1 month ago

yanlele commented 1 month ago

关键词:预览 PDF 文件

在前端实现 PDF 文件预览功能,主要有以下几种常用方法:

1. 使用浏览器内置的 PDF 查看器

浏览器像 Chrome 和 Firefox 等内置了 PDF 查看器,可以直接在浏览器中打开和预览 PDF 文件。实现方式非常简单,只需将 PDF 文件的 URL 设置为<a>标签的href属性,或者使用window.open方法在新标签页中打开 PDF 文件。

<!-- 方法1: 使用超链接 -->
<a href="/path/to/your/document.pdf" target="_blank">预览PDF</a>

<!-- 方法2: 使用JavaScript -->
<button onclick="window.open('/path/to/your/document.pdf', '_blank')">预览PDF</button>

2. 使用 PDF.js

PDF.js是一个由 Mozilla 开发的开源库,它使用 HTML5 Canvas 来渲染 PDF 文件。PDF.js 提供了广泛的 API 来实现 PDF 的加载、渲染、缩放、打印等功能。

如何使用:

  1. 引入 PDF.js 首先,你需要在你的项目中包含 PDF.js。可以从其GitHub 仓库中直接下载或使用 CDN。
<!-- 引入pdf.js和pdf.worker.js -->
<script src="/path/to/pdf.js"></script>
<script src="/path/to/pdf.worker.js"></script>
  1. 渲染 PDF 文件 使用 PDF.js 提供的 API 来加载和渲染 PDF 文件。
<!-- PDF容器 -->
<div id="pdf-container"></div>

<script>
  // 初始化PDF.js
  pdfjsLib.getDocument("/path/to/your/document.pdf").promise.then(function (pdfDoc) {
    // 获取第一页
    pdfDoc.getPage(1).then(function (page) {
      // 设置视口和比例
      var scale = 1.5;
      var viewport = page.getViewport({ scale: scale });

      // 准备用于渲染的Canvas
      var canvas = document.createElement("canvas");
      var ctx = canvas.getContext("2d");
      canvas.height = viewport.height;
      canvas.width = viewport.width;

      // 将Canvas添加到DOM中
      document.getElementById("pdf-container").appendChild(canvas);

      // 通过Canvas渲染PDF页面
      var renderContext = {
        canvasContext: ctx,
        viewport: viewport,
      };
      page.render(renderContext);
    });
  });
</script>

3. 使用第三方服务

也可以使用第三方服务如 Google Docs Viewer 来预览 PDF。这种方法的优点是容易实现,但依赖于外部服务。

<iframe
  src="http://docs.google.com/gview?url=http://path.to/your/document.pdf&embedded=true"
  style="width:600px; height:500px;"
  frameborder="0"
></iframe>

其中,将http://path.to/your/document.pdf替换为你的 PDF 文件的真实 URL。

选择适合的方法