繁体   English   中英

在Javascript中将PDF转换为Base64编码的字符串

[英]Convert PDF to a Base64-encoded string in Javascript

我需要使用Javascript将PDF文件编码为Base64。 我可以在Javascript中创建Base64编码的jpeg或png图像,但是我找不到任何方法或示例代码来从PDF文件创建Base64编码的字符串。

是否有使用HTML5画布的任何解决方案?

谢谢。

试试这个 :-

<input id="inputFile" type="file" onchange="convertToBase64();" />

<script type="text/javascript">
    function convertToBase64() {
        //Read File
        var selectedFile = document.getElementById("inputFile").files;
        //Check File is not Empty
        if (selectedFile.length > 0) {
            // Select the very first file from list
            var fileToLoad = selectedFile[0];
            // FileReader function for read the file.
            var fileReader = new FileReader();
            var base64;
            // Onload of file read the file content
            fileReader.onload = function(fileLoadedEvent) {
                base64 = fileLoadedEvent.target.result;
                // Print data in console
                console.log(base64);
            };
            // Convert data to base64
            fileReader.readAsDataURL(fileToLoad);
        }
    }
</script>

以下是一个人如何做到这一点:

以下链接提供了其他几种可能的解决方案:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM