最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript前端實現(xiàn)PDF加解密

 更新時間:2024年12月28日 09:54:34   作者:xulihang  
Dynamsoft?Document?Viewer是一個用于文檔掃描和查看的JavaScript?SDK,可以在前端對PDF文件進行加密和解密,下面我們就來看看它的具體使用吧

我們可以對PDF文件加密以保護其內(nèi)容。在這種情況下,需要輸入密碼才能查看或編輯內(nèi)容。Dynamsoft Document Viewer是一個用于文檔掃描和查看的JavaScript SDK,可以在前端對PDF文件進行加密和解密。在本文中,我們將探討如何使用它。

使用Dynamsoft Document Viewer打開一個PDF文件

1.創(chuàng)建一個包含以下模板的新HTML文件。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <title>Browse Viewer</title>
  <style>
  </style>
</head>
<body>
</body>
<script>
</script>
</html>

2.在頁面中包含Dynamsoft Document Viewer的文件。

<script src="https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@2.1.0/dist/ddv.js"></script>
<link rel="stylesheet" >

3.使用許可證初始化Dynamsoft Document Viewer??梢栽?a target="_blank">這里申請一個證書。

Dynamsoft.DDV.Core.license = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="; //one-day trial
Dynamsoft.DDV.Core.engineResourcePath = "https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@2.1.0/dist/engine";// Lead to a folder containing the distributed WASM files
await Dynamsoft.DDV.Core.init();

4.創(chuàng)建一個新的文檔實例。

const docManager = Dynamsoft.DDV.documentManager;
const doc = docManager.createDocument();

5.創(chuàng)建一個Browse Viewer實例,將其綁定到一個容器,然后用它來查看我們剛剛創(chuàng)建的文檔。

HTML:

<div id="viewer"></div>

JavaScript:

const browseViewer = new Dynamsoft.DDV.BrowseViewer({
  container: document.getElementById("viewer"),
});

browseViewer.openDocument(doc.uid);

CSS:

#viewer {
  width: 320px;
  height: 480px;
}

6.使用input選擇一個PDF文件并將其加載到文檔實例中,然后可以用Browse Viewer進行查看。

HTML:

<label>
  Select a PDF file to load:
  <br/>
  <input type="file" id="files" name="files" accept=".pdf" onchange="filesSelected()"/>
</label>

JavaScript:

async function filesSelected(){
  let filesInput = document.getElementById("files");
  let files = filesInput.files;
  if (files.length>0) {
    const file = files[0];
    const blob = await readFileAsBlob(file);
    await doc.loadSource(blob); // load the PDF file
  }
}

function readFileAsBlob(file){
  return new Promise((resolve, reject) => {
    const fileReader = new FileReader();
    fileReader.onload = async function(e){
      const response = await fetch(e.target.result);
      const blob = await response.blob();
      resolve(blob);
    };
    fileReader.onerror = function () {
      reject('oops, something went wrong.');
    };
    fileReader.readAsDataURL(file);
  })
}

打開受密碼保護的PDF文件

如果PDF受密碼保護,使用默認配置打開時會拋出錯誤,提示我們需要輸入密碼才能打開它。

Error: Failed to read the PDF file because it's encrypted and the correct password is not provided.

我們可以捕獲該錯誤并要求用戶輸入密碼。

HTML:

<div class="modal input-modal">
  <div>
    <label>
      Please input the password:
    </label>
    <br/>
    <input type="password" id="password"/>
    <br/>
    <button id="okayBtn">Okay</button>
  </div>
</div>

CSS:

.modal {
  display: flex;
  align-items: flex-start;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  min-width: 250px;
  border: 1px solid gray;
  border-radius: 5px;
  background: white;
  z-index: 9999;
  padding: 10px;
  visibility: hidden;
}

.input-modal.active {
  visibility: inherit;
}

JavaScript:

function filesSelected(){
  //...
  try {
    await doc.loadSource(blob);
  } catch (error) {
    if (error.cause.code === -80202) {
      askForPassword();
    }
  }
}

function askForPassword(){
  document.getElementById("password").value = ""; //clear previous password
  document.getElementsByClassName("input-modal")[0].classList.add("active");
}

document.getElementById("okayBtn").addEventListener("click",async function(){
  document.getElementsByClassName("input-modal")[0].classList.remove("active");
  try {
    await doc.loadSource({fileData:blob,password:document.getElementById("password").value});  
  } catch (error) {
    alert(error);
  }
})

需要將密碼傳入loadSource方法:

await doc.loadSource({fileData:blob,password:document.getElementById("password").value});  

保存為受密碼保護的PDF文件

不設密碼保存文檔為PDF,可以創(chuàng)建一個解密的PDF文件。設了密碼的話,則創(chuàng)建一個加密的PDF文件。

<div>
  <label>Set a password:
    <input type="password" id="newPassword"/>
  </label>
</div>
<script>
let newPassword = document.getElementById("newPassword").value;
let blob;
if (newPassword) {
  blob = await doc.saveToPdf({password:newPassword});
}else{
  blob = await doc.saveToPdf();
}
</script>

到此這篇關(guān)于JavaScript前端實現(xiàn)PDF加解密的文章就介紹到這了,更多相關(guān)JavaScript PDF加解密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

吴川市| 江安县| 大埔县| 葵青区| 弥勒县| 沙坪坝区| 东平县| 彭水| 刚察县| 彭泽县| 石狮市| 海林市| 广元市| 庆城县| 常宁市| 清苑县| 南漳县| 梁河县| 澄城县| 古丈县| 洞口县| 安仁县| 阳谷县| 石林| 舟曲县| 江阴市| 上杭县| 图们市| 阿合奇县| 阿鲁科尔沁旗| 河源市| 高淳县| 乌苏市| 上杭县| 罗甸县| 特克斯县| 金昌市| 封开县| 惠州市| 绵竹市| 邵阳县|