尝试使用Intel XDK保存PDF文件

时间:2016-09-13 23:30:20

标签: javascript jspdf

我正在尝试重新创建this example

但是当我点击按钮时,我收到错误:“未捕获的ReferenceError:未定义LocalFileSystem”。

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jsPDF</title>    
    <script src="js/jquery-1.7.1.min.js"></script>
    <script src="js/jspdf.js"></script>
    <script src="js/FileSaver.js"></script>
    <script src="js/html2canvas.js"></script>
    <script>
        function guardar(){
            var pdf = new jsPDF('p', 'pt', 'a4');
            var source = $('#tabelagastos')[0];
            var PDFFILE ='';
            var arquivo = prompt("Qual o nome do arquivo?");

            pdf.specialElementHandlers = {
                '#bypassme': function (element, renderer) {
                 return true;
             }
            };
            pdf.fromHTML(source, 15, 15, {'width': 170},
                         function (dispose) {
                            PDFFILE = pdf.output();
                         });

            //NEXT SAVE IT TO THE DEVICE
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                fileSystem.root.getFile(arquivo +".pdf", {create: true}, function(entry) {
                    var fileEntry = entry;
                    entry.createWriter(function(writer) {
                    writer.onwrite = function(evt) {
                        alert("Save (root folder)!");
                    };

                    writer.write( PDFFILE );
                        }, function(error) {
                    alert(error);
                    });

                }, function(error){
                    alert(error);
                });
            },
            function(event){
                alert( evt.target.error.code );
            });
    }
    </script>
</head>
<body>
<h1>EJEMPLO DE JSPDF</h1>
    <hr />
    <div id="tabelagastos">some text</div>
    <input type="button" id="iniciador" value="Guardar" onclick="guardar();">
</body>

1 个答案:

答案 0 :(得分:0)

以下是我使用Chrome 53和Windows 7在本地测试的代码修改。

主要变化是:

  • 在保存到设备之前,您需要致电requestQuota()以获取本地设备上的空间 - 请参阅the documentationthis question以获取示例;
  • pdf.output()返回的字符串转换为Blob的{​​{1}};
  • 删除对FileWriter
  • 的不必要引用

写入文件的位置取决于您的操作系统和浏览器;如果你正在使用Chrome,you can find more information about where the file was written here

在Windows 7上,我在此处找到了该文件:LocalFileSystem

我可以通过将C:\Users\USERNAME\AppData\Local\Google\Chrome\User Data\Default\File System\018\p\00\00000016附加到文件名来在Adobe Reader中打开它。

.pdf
相关问题