使用JavaScript將圖片合并為PDF的實現(xiàn)
介紹
在日常工作中,我們可能需要拍攝一些照片并將圖像合并到PDF文件中。這可以通過許多應用來完成。Dynamsoft Document Viewer讓這一操作更加方便,因為它可以在瀏覽器中執(zhí)行??梢约稍摬僮鞯轿覀兊脑诰€網(wǎng)站、CRM系統(tǒng)中。
在本文中,我們將使用Dynamsoft Document Viewer創(chuàng)建一個Web應用,用JavaScript將圖像合并到PDF中。
新建HTML文件
創(chuàng)建一個包含以下模板的新HTML文件。
<!DOCTYPE html> <html> <head> <title>Document Scanner</title> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> <style> </style> </head> <body> <h2>Merge Images into PDF</h2> <script> </script> </body> </html>
添加依賴項
在頁面中包含Dynamsoft Document Viewer。
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@1.1.0/dist/ddv.js"></script> <link rel="stylesheet" rel="external nofollow" >
選擇文件
接下來,選擇要合并的文件。
添加用于選擇多個文件的input
元素。
<input type="file" name="file" id="file" multiple="multiple">
然后,我們可以使用以下JavaScript獲取所選文件:
let fileInput = document.getElementById("file"); let files = fileInput.files;
將圖像合并為PDF
在頁面中添加按鈕以執(zhí)行合并圖像到PDF的操作。
HTML:
<button onclick="merge()">Merge into PDF</button>
JavaScript:
使用許可證初始化Dynamsoft Document Viewer。可以在此處申請許可證。
Dynamsoft.DDV.Core.license = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="; // Public trial license which is valid for 24 hours Dynamsoft.DDV.Core.engineResourcePath = "https://cdn.jsdelivr.net/npm/dynamsoft-document-viewer@1.1.0/dist/engine";// Lead to a folder containing the distributed WASM files await Dynamsoft.DDV.Core.loadWasm(); await Dynamsoft.DDV.Core.init(); Dynamsoft.DDV.setProcessingHandler("imageFilter", new Dynamsoft.DDV.ImageFilter());
使用Dynamsoft Document Viewer的DocumentManager
創(chuàng)建新的文檔實例。
const doc = Dynamsoft.DDV.documentManager.createDocument();
以blob格式讀取文件并將其加載到文檔中。
let fileInput = document.getElementById("file"); let files = fileInput.files; for (let index = 0; index < files.length; index++) { const file = files[index]; const source = await readFileAsBlob(file); try { await doc.loadSource(source); } catch (error) { console.log(error); } } function readFileAsBlob(file){ return new Promise((resolve, reject) => { let fileReader = new FileReader(); fileReader.onload = function(e){ const blob = new Blob([new Uint8Array(e.target.result)], {type: file.type }); resolve(blob); }; fileReader.onerror = function () { reject(); }; fileReader.readAsArrayBuffer(file); }) }
將圖像保存為PDF。
const blob = await doc.saveToPdf();
通過以下函數(shù)下載PDF。
function downloadBlob(blob){ const link = document.createElement('a') link.href = URL.createObjectURL(blob); link.download = "scanned.pdf"; document.body.appendChild(link) link.click() document.body.removeChild(link) URL.revokeObjectURL(link.href); }
使用編輯界面
Dynamsoft Document Viewer附帶一系列組件。在合并到PDF文件之前,我們可以使用其EditViewer查看和編輯圖像。
在HTML中添加容器。
<div id="container" style="height:480px;"></div>
使用以下代碼啟動它。
let editViewer = new Dynamsoft.DDV.EditViewer({ container: "container", }); editViewer.openDocument(doc.uid);
我們可以旋轉(zhuǎn)、裁剪、重新排序和刪除圖像,并為圖像應用濾鏡。
以上就是使用JavaScript將圖片合并為PDF的實現(xiàn)的詳細內(nèi)容,更多關于JavaScript圖片合并為PDF的資料請關注腳本之家其它相關文章!
相關文章
JS中confirm,alert,prompt函數(shù)使用區(qū)別分析
JS中confirm,alert,prompt函數(shù)使用區(qū)別分析,需要的朋友可以參考下。2010-04-04微信公眾平臺開發(fā)教程(四) 實例入門:機器人回復(附源碼)
本篇文章主要介紹了微信公眾平臺開發(fā)機器人,可以實現(xiàn)簡單對話和查詢天氣等,有需要的可以了解一下。2016-12-12Jquery把獲取到的input值轉(zhuǎn)換成json
本篇文章主要介紹了Jquery把獲取到的input值轉(zhuǎn)換成json的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05