我想做客户端scrpting合并和拆分pdf,所以我想使用itextsharp.可以用于javascript.我是Javascript的新手.请帮助我提出宝贵的建议.
Nic*_*row 13
我使用 PDF-LIB 库找到了一个完全客户端的解决方案:https ://pdf-lib.js.org/
它使用 mergeAllPDFs 函数,该函数采用一个参数:urls,它是文件的 url 数组。
确保在标题中包含以下内容:
<script src='https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.js'></script>
<script src='https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js'></script>
Run Code Online (Sandbox Code Playgroud)
然后:
async function mergeAllPDFs(urls) {
// create an empty PDFLib object of PDFDocument to do the merging into
const pdfDoc = await PDFLib.PDFDocument.create();
// iterate over all documents to merge
const numDocs = urls.length;
for(var i = 0; i < numDocs; i++) {
// download the document
const donorPdfBytes = await fetch(urls[i]).then(res => res.arrayBuffer());
// load/convert the document into a PDFDocument object
const donorPdfDoc = await PDFLib.PDFDocument.load(donorPdfBytes);
// iterate over the document's pages
const docLength = donorPdfDoc.getPageCount();
for(var k = 0; k < docLength; k++) {
// extract the page to copy
const [donorPage] = await pdfDoc.copyPages(donorPdfDoc, [k]);
// add the page to the overall merged document
pdfDoc.addPage(donorPage);
}
}
// save as a Base64 URI
const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
// strip off the first part to the first comma "data:image/png;base64,iVBORw0K..."
const data_pdf = pdfDataUri.substring(pdfDataUri.indexOf(',')+1);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21005 次 |
| 最近记录: |