最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - How to convert some base64 string to pdf using javascript - Stack Overflow

matteradmin3PV0评论

There is a program (in asp mvc) on browser that connect to scanner, Scan document and show it as images. enter image description here

Src of image is like below:

data:application/octet-stream;base64,Qk0m2wEAAAAAAD4AAAAoAAAAOAMAAJEEAAABA//////////////////////wAGA/wAYMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAg13xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/

Now, I want convert all of images to one pdf and attach it to a file upload.

Could you help me please?

There is a program (in asp mvc) on browser that connect to scanner, Scan document and show it as images. enter image description here

Src of image is like below:

data:application/octet-stream;base64,Qk0m2wEAAAAAAD4AAAAoAAAAOAMAAJEEAAABA//////////////////////wAGA/wAYMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAg13xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/

Now, I want convert all of images to one pdf and attach it to a file upload.

Could you help me please?

Share Improve this question asked Mar 18, 2019 at 6:40 Farzaneh TalebiFarzaneh Talebi 9354 gold badges26 silver badges48 bronze badges 1
  • 1 Possible duplicate of Save base64 string as PDF at client side with JavaScript – ellipsis Commented Mar 18, 2019 at 6:41
Add a ment  | 

2 Answers 2

Reset to default 9

Please use the below code to convert Base64 to PDF with the client side JavaScript. Pass the base64 data to the function base64ToArrayBuffer

function base64toPDF(data) {
    var bufferArray = base64ToArrayBuffer(data);
    var blobStore = new Blob([bufferArray], { type: "application/pdf" });
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blobStore);
        return;
    }
    var data = window.URL.createObjectURL(blobStore);
    var link = document.createElement('a');
    document.body.appendChild(link);
    link.href = data;
    link.download = "file.pdf";
    link.click();
    window.URL.revokeObjectURL(data);
    link.remove();
}

function base64ToArrayBuffer(data) {
    var bString = window.atob(data);
    var bLength = bString.length;
    var bytes = new Uint8Array(bLength);
    for (var i = 0; i < bLength; i++) {
        var ascii = bString.charCodeAt(i);
        bytes[i] = ascii;
    }
    return bytes;
};

You need some kind of JavaScript PDF library to do this. jsPDF for instance has a addImage() method (https://rawgit./MrRio/jsPDF/master/docs/module-addImage.html) which accepts a base64 string as input

Post a comment

comment list (0)

  1. No comments so far