简体   繁体   English

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

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

I need to encode a PDF file to Base64 with Javascript. 我需要使用Javascript将PDF文件编码为Base64。 I can create Base64-encoded jpeg or png images in Javascript, but I could not find any way or sample code to create a Base64-encoded string from a PDF file. 我可以在Javascript中创建Base64编码的jpeg或png图像,但是我找不到任何方法或示例代码来从PDF文件创建Base64编码的字符串。

Is it there any solution using a HTML5 canvas? 是否有使用HTML5画布的任何解决方案?

Thanks. 谢谢。

Try this :- 试试这个 :-

<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>

Here is how one person did it: 以下是一个人如何做到这一点:

Here is a link that suggests several other possible solutions: 以下链接提供了其他几种可能的解决方案:

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

相关问题 如何从 javascript 中的本地文件图像创建 base64 编码的字符串 - How to create a base64-encoded string from a local file image in javascript 使用Java脚本删除Base64编码的URL参数 - Strip Base64-encoded URL parameters with Javascript 如何在sails.js 或通常在服务器端JavaScript 中将图像转换为base64 编码的数据URL? - How do I convert an image to a base64-encoded data URL in sails.js or generally in the servers side JavaScript? 如何下载 base64 编码的图像? - How to download a base64-encoded image? 将 base64 字符串编码图像转换为 javascript 中的 tiff 格式 - Convert a base64 string encoded image to a tiff format in javascript Chrome中的文件大小问题,用于OBJECT标记中的base64编码的PDF数据 - File size issue in Chrome for base64-encoded PDF data in OBJECT tag Sails.js如何将base64编码的图像转换为图像并将其存储在磁盘中? - Sails.js how to convert a base64-encoded to image and store it in disk? 处理javascript(客户端)中许多base64编码图像的实用方法? - Practical way to handle many base64-encoded images in javascript (client-side)? 如何在节点js中解码base64编码的json object字符串 - How to decode base64-encoded json object string in node js base64编码的ajax损坏发送对此的一些想法 - Corruption of base64-encoded ajax sends -some thoughts on this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM