hexo在线查看pdf

场景 #

由于在看《mysql是如何运行的》,做md文件笔记时,发现好多都是按pdf一字不漏打出来。所以想着能不能直接本地编辑pdf,然后博客上支持在线查看

事后觉得这个方式有待斟酌,电脑上/平板上查看没啥问题,手机上查看字有点小,但也还能接受。==>待斟酌
不过下面的方案是可行的。

准备 #

需要到官网下载 pdf.js

https://github.com/mozilla/pdf.js/releases ,这里选择 v3.4.120中的 pdfjs-3.4.120-dist.zip ,最新版本好像有问题

操作 #

pdfjs处理 #

在source/下创建myjs/pdfjs文件夹,并解压到这个文件夹下
ly-20241212141830564

修改pdfjs/web/viewer.js

if (fileOrigin !== viewerOrigin) {//1563行左右
	throw new Error("file origin does not match viewer's");
}
//注释掉,为了处理跨域问题,注释掉后允许在线访问其他网站的pdf
// if (fileOrigin !== viewerOrigin) {
//	throw new Error("file origin does not match viewer's");
//}

hexo配置修改 #

# 找到# Directory下的skip_render项,添加忽略渲染的文件夹
skip_render:  ['myjs/pdfjs/**/*']

清理hexo中public及其他缓存文件 #

 hexo clean & hexo g

文件预览测试 #

本地文件 #

我们在hexo的source文件夹下,放置这样一个文件: source/pdf/my.pdf
ly-20241212141830812

MD文件修改 #

<iframe src='/myjs/pdfjs/web/viewer.html?file=/pdf/my.pdf' style="padding: 0;width:100%;" marginwidth="0" frameborder="no" scrolling="no" height="2000px"></iframe>

操作并查看 #

hexo g & hexo s

ly-20241212141831013

远程文件 #

ly-20241212141831197

也就是在我的账号(lwmfjc)下,创建一个仓库(仓库名 pdfs),然后创建一个文件夹及文件 temp/01.pdf ,这个地址是 https://raw.githubusercontent.com/lwmfjc/pdfs/main/temp/01.pdf

注意修改账号名仓库名 :lwmfjc/pdfs/
文件夹文件:temp/01.pdf

MD文件修改 #

<iframe src='/myjs/pdfjs/web/viewer.html?file=https://raw.githubusercontent.com/lwmfjc/pdfs/main/mysql/01.pdf' style="padding: 0;width:100%;" marginwidth="0" frameborder="no" scrolling="no" height="2000px"></iframe>

操作并查看 #

hexo g & hexo s

ly-20241212141831381