2016-09-13 44 views
2

我試圖重新創建this example試圖用英特爾XDK保存PDF文件

但是當我點擊按鈕時,我得到錯誤:「未捕獲的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> 

回答

0

這是你的代碼的修改,我測試在本地使用Chrome 53和Windows 7

的主要變化是:

  • 在保存到設備之前,您需要撥打requestQuota()以在本地獲取空間ce - 例如參見the documentationthis question;
  • 將由pdf.output()返回的字符串轉換爲BlobFileWriter;
  • 刪除不必要的參考LocalFileSystem

寫入文件的位置取決於您的操作系統和瀏覽器;如果你使用的是Chrome,you can find more information about where the file was written here

在Windows 7中,我發現這裏的文件:C:\Users\USERNAME\AppData\Local\Google\Chrome\User Data\Default\File System\018\p\00\00000016

我能夠通過附加.pdf到文件名在Adobe Reader中打開它。

<!DOCTYPE html> 
    <html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>jsPDF</title>  
     <script src="jquery.min.js"></script> 
     <script src="jspdf.js"></script> 
     <script src="FileSaver.js"></script> 
     <script src="html2canvas.js"></script> 
     <script> 
     window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; 
     function saveFile(){ 
      var pdf = new jsPDF('p', 'pt', 'a4'); 
      var source = $('#spendingTable')[0]; 
      var PDFFILE =''; 
      var filename = prompt("Enter a file name:"); 

      pdf.specialElementHandlers = { 
       '#bypassme': function (element, renderer) { 
        return true; 
       } 
      }; 

      pdf.fromHTML(source, 15, 15, {'width': 170}, function (dispose) { 
       PDFFILE = new Blob([pdf.output()], {type: "application/pdf"}); 
      }); 

      //NEXT SAVE IT TO THE DEVICE 
      var requestedBytes = 1024*1024*10; // 10MB 
      navigator.webkitPersistentStorage.requestQuota (
       requestedBytes, function (grantedBytes) { 
        window.requestFileSystem(PERSISTENT, 1024*1024, function (fileSystem) { 
         fileSystem.root.getFile(filename +".pdf", {create: true}, function (entry) { 
          var fileEntry = entry; 
          entry.createWriter(function(writer) { 
           writer.onwrite = function(evt) { 
            alert("Saved to root folder!"); 
           }; 
           writer.write(PDFFILE); 
          }, 
          function (error) { 
           alert(error); 
          }); 
         }, 
         function (error) { 
          alert(error); 
         }); 
        }, 
        function (event) { 
         alert(event.target.error.code); 
        }); 
       } 
      ); 
     } 
     </script> 
    </head> 
    <body> 
    <h1>JSPDF EXAMPLE</h1> 
     <hr> 
     <div contenteditable="true" id="spendingTable" style="border: 1px black solid">This text is saved into a PDF.</div> 
     <br> 
     <input type="button" value="Save" onclick="saveFile();"> 
    </body>