欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

使用Dynamsoft Document Viewer打開(kāi)一個(gè)PDF文件

1.創(chuàng)建一個(gè)包含以下模板的新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.在頁(yè)面中包含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">這里申請(qǐng)一個(gè)證書(shū)。

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)建一個(gè)新的文檔實(shí)例。

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

5.創(chuàng)建一個(gè)Browse Viewer實(shí)例,將其綁定到一個(gè)容器,然后用它來(lái)查看我們剛剛創(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選擇一個(gè)PDF文件并將其加載到文檔實(shí)例中,然后可以用Browse Viewer進(jìn)行查看。

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);
  })
}

打開(kāi)受密碼保護(hù)的PDF文件

如果PDF受密碼保護(hù),使用默認(rèn)配置打開(kāi)時(shí)會(huì)拋出錯(cuò)誤,提示我們需要輸入密碼才能打開(kāi)它。

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

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

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});  

保存為受密碼保護(hù)的PDF文件

不設(shè)密碼保存文檔為PDF,可以創(chuàng)建一個(gè)解密的PDF文件。設(shè)了密碼的話,則創(chuàng)建一個(gè)加密的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前端實(shí)現(xiàn)PDF加解密的文章就介紹到這了,更多相關(guān)JavaScript PDF加解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript實(shí)現(xiàn)的伸展收縮型菜單代碼

    JavaScript實(shí)現(xiàn)的伸展收縮型菜單代碼

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)的伸展收縮型菜單代碼,可實(shí)現(xiàn)JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)遍歷及修改頁(yè)面元素屬性的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • JS文件中加載jquery.js的實(shí)例代碼

    JS文件中加載jquery.js的實(shí)例代碼

    這篇文章主要介紹了JS文件中加載jquery.js的方法,JS文件添加其他JS的實(shí)例代碼,感興趣的小伙伴們可以參考一下,具體如下
    2018-05-05
  • uni-app和web-view頁(yè)面相互傳參方法實(shí)例

    uni-app和web-view頁(yè)面相互傳參方法實(shí)例

    web-view是一個(gè)web瀏覽器組件,可以用來(lái)承載網(wǎng)頁(yè)的容器,會(huì)自動(dòng)鋪滿整個(gè)頁(yè)面,下面這篇文章主要給大家介紹了關(guān)于uni-app和web-view頁(yè)面相互傳參的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • My Desktop :) 桌面式代碼

    My Desktop :) 桌面式代碼

    My Desktop 桌面式 代碼
    2008-12-12
  • javascript中caller和callee詳解

    javascript中caller和callee詳解

    有些小伙伴可能會(huì)問(wèn)caller,callee 是什么?在javascript 中有什么樣的作用?那么本篇會(huì)對(duì)于此做一些基本介紹。希望能夠?qū)Υ蠹依斫鈐avascript中的callee與caller有所幫助。
    2015-08-08
  • iOS和Android用同一個(gè)二維碼實(shí)現(xiàn)跳轉(zhuǎn)下載鏈接的方法

    iOS和Android用同一個(gè)二維碼實(shí)現(xiàn)跳轉(zhuǎn)下載鏈接的方法

    這篇文章給大家分享的是iOS和Android掃描同一個(gè)二維碼,分別跳到各自的下載鏈接的實(shí)現(xiàn)方法,文中給出了實(shí)例代碼,有需要的朋友們可以參考借鑒。
    2016-09-09
  • javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

    javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,實(shí)現(xiàn)四則運(yùn)算,小數(shù)點(diǎn),回退,歸0等功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 原JS實(shí)現(xiàn)banner圖的常用功能

    原JS實(shí)現(xiàn)banner圖的常用功能

    這篇文章主要為大家詳細(xì)介紹了原JS實(shí)現(xiàn)banner圖的常用功能,,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 在element-ui的select下拉框加上滾動(dòng)加載

    在element-ui的select下拉框加上滾動(dòng)加載

    這篇文章主要介紹了在element-ui的select下拉框加上滾動(dòng)加載,本文以以element-ui中的select為例,給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-04-04
  • js打印紙函數(shù)代碼(遞歸)

    js打印紙函數(shù)代碼(遞歸)

    js打印紙函數(shù)代碼,獲取并設(shè)置打印紙的具體信息的代碼,需要的朋友可以參考下。
    2010-06-06

最新評(píng)論