Skip to content

Commit

Permalink
Merge pull request #5 from CrazyMrYan/feature/#1
Browse files Browse the repository at this point in the history
feat: 支持PDF文件预览(#1)
  • Loading branch information
CrazyMrYan authored Apr 28, 2022
2 parents 40b4458 + a950681 commit b81f2d9
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 5 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.DS_Store
node_modules
/dist


# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"core-js": "^3.6.5",
"highlight.js": "^11.5.1",
"jszip": "^3.9.1",
"pdfjs-dist": "^2.13.216",
"vue": "^2.6.11"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/components/Dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<script>
import ImageCom from "./theme/Image.vue";
import TextCom from "./theme/Text.vue";
import PDF from "./theme/PDF.vue";
export default {
components: { ImageCom },
props: {
visible: {
type: Boolean,
Expand All @@ -35,6 +36,7 @@ export default {
fileComponent: {
image: ImageCom,
text: TextCom,
pdf: PDF,
},
}),
methods: {
Expand Down
5 changes: 3 additions & 2 deletions src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<script>
import JSZip from "jszip";
import { image, text, settleDocuments } from "../utils/processFile";
import { image, text, settleDocuments, PDF } from "../utils/processFile";
import {
queryFileType,
processIgnoreFolder,
Expand Down Expand Up @@ -92,9 +92,10 @@ export default {
fileInfo = await text(FileInformationSet, file);
fileInfo.codeFormat = getFileNameExtension(file.id);
console.log(fileInfo);
} else if (queryFileType(file.id, "pdf")) {
fileInfo = await PDF(FileInformationSet, file);
}
this.fileInfo = fileInfo;
console.log(this.fileInfo);
this.showDialog = true;
this.loading = false;
},
Expand Down
166 changes: 166 additions & 0 deletions src/components/theme/PDF.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<template>
<div class="home_wrap">
<div class="pdf_down">
<div class="pdf_set_left" @click="scaleD()">➕</div>
<div class="pdf_set_middle" @click="scaleX()">➖</div>
</div>
<div :style="{ width: pdf_div_width, margin: '0 auto' }">
<canvas
v-for="page in pdf_pages"
:id="'the_canvas' + page"
:key="page"
></canvas>
</div>
</div>
</template>

<script>
import * as PDFJS from "pdfjs-dist/legacy/build/pdf";
PDFJS.GlobalWorkerOptions.workerSrc = require("pdfjs-dist/legacy/build/pdf.worker.entry.js");
export default {
name: "PDF",
props: {
data: {
type: Object,
default: null,
},
},
data() {
return {
pdf_scale: 1.0,
pdf_pages: [],
pdf_div_width: "",
currentPage: 1,
};
},
mounted() {
this.loadFile();
},
methods: {
scaleD() {
//放大
let max = 0;
if (window.screen.width > 1440) {
max = 1.4;
} else {
max = 1.2;
}
if (this.pdf_scale >= max) {
return;
}
this.pdf_scale = this.pdf_scale + 0.1;
this.loadFile();
},
scaleX() {
//缩小
let min = 1.0;
if (this.pdf_scale <= min) {
return;
}
this.pdf_scale = this.pdf_scale - 0.1;
this.loadFile();
},
async loadFile() {
//初始化pdf
this.pdfDoc = await PDFJS.getDocument(this.data.content).promise;
this.pdf_pages = this.pdfDoc.numPages;
this.$nextTick(() => this.renderPage());
},
async renderPage(num = 1) {
this.currentPage = num;
//渲染pdf页
const page = await this.pdfDoc.getPage(num);
const canvas = document.getElementById("the_canvas" + num);
// const canvas = document.getElementById("the_canvas");
const ctx = canvas.getContext("2d");
const dpr = window.devicePixelRatio || 1;
const bsr =
ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio ||
1;
const ratio = dpr / bsr;
const viewport = page.getViewport({ scale: this.pdf_scale });
canvas.width = viewport.width * ratio;
canvas.height = viewport.height * ratio;
canvas.style.width = viewport.width + "px";
this.pdf_div_width = viewport.width + "px";
canvas.style.height = viewport.height + "px";
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
const renderContext = {
canvasContext: ctx,
viewport: viewport,
};
page.render(renderContext);
if (this.pdf_pages > num) {
setTimeout(() => {
return this.renderPage(num + 1);
});
}
},
nextPage() {
if (this.pdf_pages > this.currentPage) {
this.renderPage(this.currentPage + 1);
}
},
prePage() {
if (this.currentPage > 1) {
this.renderPage(this.currentPage - 1);
}
},
},
};
</script>

<style scoped>
.home_wrap {
width: 100%;
height: 100%;
}
.home_wrap .pdf_down {
position: fixed;
display: flex;
z-index: 20;
right: 26px;
bottom: 7%;
cursor: pointer;
}
.home_wrap .pdf-pre {
position: fixed;
display: flex;
z-index: 20;
right: 160px;
bottom: 9%;
cursor: pointer;
}
.home_wrap .pdf-next {
position: fixed;
display: flex;
z-index: 20;
right: 100px;
bottom: 9%;
}
.home_wrap .pdf_down .pdf_set_left {
width: 30px;
height: 40px;
color: #408fff;
font-size: 15px;
padding-top: 25px;
text-align: center;
margin-right: 5px;
cursor: pointer;
}
.home_wrap .pdf_down .pdf_set_middle {
width: 30px;
height: 40px;
color: #408fff;
font-size: 15px;
padding-top: 25px;
text-align: center;
margin-right: 5px;
cursor: pointer;
}
</style>
8 changes: 6 additions & 2 deletions src/utils/processFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ export const image = async (fliesList, file) => {
};

export const text = async (fliesList, file) => {
console.log(fliesList[file.name]);
const content = await fliesList[file.name]?.async("string");
console.log(content);
return new CreateFileInfo("text", file.id, content);
};

export const PDF = async (fliesList, file) => {
const fileInfo = await fliesList[file.name]?.async("blob");
const content = fileInfo.arrayBuffer();
return new CreateFileInfo("pdf", file.id, await content, fileInfo.size);
};

export const settleDocuments = (file, fileName) => {
const { name, parent, date, dir } = file;
const id = getFileId(dir, fileName);
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6285,6 +6285,13 @@ pbkdf2@^3.0.3:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

pdfjs-dist@^2.13.216:
version "2.13.216"
resolved "https://registry.yarnpkg.com/pdfjs-dist/-/pdfjs-dist-2.13.216.tgz#251a11c9c8c6db19baacd833a4e6986c517d1ab3"
integrity sha512-qn/9a/3IHIKZarTK6ajeeFXBkG15Lg1Fx99PxU09PAU2i874X8mTcHJYyDJxu7WDfNhV6hM7bRQBZU384anoqQ==
dependencies:
web-streams-polyfill "^3.2.0"

performance-now@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
Expand Down Expand Up @@ -8407,6 +8414,11 @@ wcwidth@^1.0.1:
dependencies:
defaults "^1.0.3"

web-streams-polyfill@^3.2.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

webpack-bundle-analyzer@^3.8.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/webpack-bundle-analyzer/-/webpack-bundle-analyzer-3.9.0.tgz#f6f94db108fb574e415ad313de41a2707d33ef3c"
Expand Down

0 comments on commit b81f2d9

Please sign in to comment.