多个PDF Base64转换为JavaScript或Jquery中的单个PDF文件

rekjcdws  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(150)

我们有一个多个PDF Base64字符串数组对象。将其转换为JavaScript或Jquery中的单个PDF文件

var base64String = "JVBERi0xLjcNCiWhs8XXDQoxIDAgb2JqDQo8PC9QYWdlcyAyIDAgUiAvVHlwZS9DYXRhbG9nPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvQ291bnQgMS9LaWRzWyA0IDAgUiBdL1R5cGUvUGFnZXM+";
function openPDF() {
  console.log('open pdf clicked');
  window.open("data:application/octet-stream;base64," + base64String);
}

function openPDFv2() {
  console.log('open pdf v2 clicked');
  const container = document.querySelector('#container');
  const tempLink = document.createElement('a');
  tempLink.href = `data:application/pdf;base64,${base64String}`;
  tempLink.setAttribute('download', 'my-fancy.pdf');
  console.log('click now!', tempLink.click);
  //container.append(tempLink);
  tempLink.click();
}

字符串
其工作单Base64.

zsbz8rwp

zsbz8rwp1#

其完成的代码为多个PDF Base64转换为一个单一的PDF文件

**https://cdn.jsdelivr.net/npm/pdf-lib@1.11.0/dist/pdf-lib.min.js**在html文件中添加脚本:

var base64PDFs = [
    "JVBERi0xLjcNCiWhs8XXDQoxIDAgb2JqDQo8PC9QYWdlcyAyIDAgUiAvVHlwZS9DYXRhbG9nPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvQ291bnQgMS9LaWRzWyA0IDAgUiBdL1R5cGUvUGFnZXM+Pg0KZW5kb2JqDQozIDAgb2JqDQo8PC9DcmVhdGlvbkRhdGUoRDoyMDIzMDcxMTE3MDk1NCkvQ3JlYXRvcihQREZpdW0pL1Byb2R1Y2VyKFBERml1bSk+Pg0KZW5kb2JqDQo0IDAgb2JqDQo8PC9Db250ZW50cyA1IDAgUiAvTWVkaWFCb3hbIDAgMCA2MTIgNzkyXS9QYXJlbnQgMiAwIFIgL1Jlc291cmNlczw8L0ZvbnQ8PC9GMSA2IDAgUiA+Pi9Qcm9jU2V0IDcgMCB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vV2luQW5zaUVuY29kaW5nL05hbWUvRjEvU3VidHlwZS9UeXBlMS9UeXBlL0ZvbnQ+Pg0KZW5kb2JqDQo3IDAgb2JqDQpbL1BERi9UZXh0XQ0KZW5kb2JqDQp4cmVmDQowIDgNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAxNyAwMDAwMCBuDQowMDAwMDAwMDY2IDAwMDAwIG4NCjAwMDAwMDAxMjIgMDAwMDAgbg0KMDAwMDAwMDIwOSAwMDAwMCBuDQowMDAwMDAwMzQzIDAwMDAwIG4NCjAwMDAwMDA2ODkgMDAwMDAgbg0KMDAwMDAwMDc4OCAwMDAwMCBuDQp0cmFpbGVyDQo8PA0KL1Jvb3QgMSAwIFINCi9JbmZvIDMgMCBSDQovU2l6ZSA4L0lEWzwzRTZEREM1MTBFRTlFQTVCQ0ZDNEI4MTNFNUI2OEM1Qj48M0U2RERDNTEwRUU5RUE1QkNGQzRCODEzRTVCNjhDNUI+XT4+DQpzdGFydHhyZWYNCjgxOA0KJSVFT0YNCg=="
];

mergePDFs(base64PDFs);

async function mergePDFs(base64PDFs) {
    const mergedPDF = await PDFLib.PDFDocument.create();

    for (const base64PDF of base64PDFs) {
        const pdfBytes = Uint8Array.from(atob(base64PDF), c => c.charCodeAt(0));
        const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes);
        const copiedPages = await mergedPDF.copyPages(pdfDoc, pdfDoc.getPageIndices());
        copiedPages.forEach(page => mergedPDF.addPage(page));
    }

    const mergedPDFBytes = await mergedPDF.save();
    const mergedPDFBase64 = btoa(String.fromCharCode.apply(null, mergedPDFBytes));
    // Use the mergedPDFBase64 as needed (e.g., download or display)
    console.log(mergedPDFBase64);
    makeMergePDF(mergedPDFBase64)
}

   
function makeMergePDF(base64Data) {
    var arrBuffer = base64ToArrayBuffer(base64Data);
    // It is necessary to create a new blob object with mime-type explicitly set
    // otherwise only Chrome works like it should
    var newBlob = new Blob([arrBuffer], { type: "application/pdf" });
    // IE doesn't allow using a blob object directly as link href
    // instead it is necessary to use msSaveOrOpenBlob
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(newBlob);
        return;
    }
    // For other browsers: 
    // Create a link pointing to the ObjectURL containing the blob.
    var data = window.URL.createObjectURL(newBlob);

    var link = document.createElement('a');
    document.body.appendChild(link); //required in FF, optional for Chrome
    link.href = data;
    link.download = "file.pdf";
    link.click();
    window.URL.revokeObjectURL(data);
    link.remove();
}
function base64ToArrayBuffer(data) {
    var binaryString = window.atob(data);
    var binaryLen = binaryString.length;
    var bytes = new Uint8Array(binaryLen);
    for (var i = 0; i < binaryLen; i++) {
        var ascii = binaryString.charCodeAt(i);
        bytes[i] = ascii;
    }
    return bytes;
};

字符串

相关问题