其實(shí)我是不建議預(yù)覽的時(shí)候在前端加水印洋满,這種方式簡直就是掩耳盜鈴,但是在后端處理的成本比較大珍坊,服務(wù)器資源有限牺勾,有時(shí)候也要干點(diǎn)知道是蠢事的蠢事。
pdfjs
官網(wǎng)提供的下載包里有一個(gè)viewer.html
這里,如果想偷懶可以直接套用這個(gè)阵漏,訪問這個(gè)文件的時(shí)候加上file="xxxxx.pdf"
驻民,需要預(yù)覽的路徑就行了,這里不多說履怯,主要是加水印回还,加水印理論上只需要修改viewer.js
這個(gè)文件就行了,如果需要?jiǎng)討B(tài)賦值水印內(nèi)容叹洲,可以在viewer.html
這個(gè)文件使用的head
標(biāo)簽中引用viewer.js
之前使用JavaScript
賦值柠硕。
viewer.js需要改動(dòng)的地方:
可以直接搜索
textLayerDiv.className = "textLayer";
textLayerDiv.style.width = canvasWrapper.style.width;
textLayerDiv.style.height = canvasWrapper.style.height;
可以定位到大概位置(不同版本語法可能不一樣)
將上圖紅框中的代碼替換成如下代碼:
//---------------------水印開始---------------------
var cover = document.createElement('div');
cover.className = "cover";
for (var y = 0; y < 5; y++) {
for (var x = 0; x < 4; x++) {
let c = document.createElement('div')
c.className = "cover"
c.style.position = 'absolute';
c.style.left = (10+x/4*100)+'%';
c.style.top = (10+y/5*100)+'%';
c.style.transform = "rotate(315deg)";
c.style.color = "rgba(0, 191, 255, 0.2)";
// c.innerText = text;//text為水印內(nèi)容,可以在viewer.html中傳入运提,也可以直接替換成固定的字符串如:c.innerText = "這是一個(gè)水印";
c.innerText = "這是一個(gè)水印";
cover.appendChild(c);
}
}
if (this.annotationLayer?.div) {
div.insertBefore(textLayerDiv, this.annotationLayer.div);
div.appendChild(cover);
} else {
div.appendChild(textLayerDiv);
div.appendChild(cover);
}
//---------------------水印結(jié)束---------------------
修改之后大概是這個(gè)樣子
修改之后可以訪問試試