【问题标题】:Save a PDF File in Ponegap using cordova-plugin-file使用 cordova-plugin-file 在 Ponegap 中保存 PDF 文件
【发布时间】:2018-09-06 11:08:05
【问题描述】:

您好,我尝试使用 cordova-plugin-file 在 Phonegap 中保存 PDF 数据字符串

示例:

%PDF-1.3
%ºß¬à
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/MediaBox [0 0 595.28 841.89]
/Contents 4 0 R
>>
endobj
4 0 obj
<</Length 8757>>
   

使用此功能转至 PDF 文件:

savePDF("Storecheck"+surveyID+".pdf", pdfOutput)

function savePDF(fileName, fileData) {

  console.log(fileData);

  //function writeToFile(fileName, data) {

  window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(directoryEntry) {
    directoryEntry.getFile(fileName, {
      create: true
    }, function(fileEntry) {
      fileEntry.createWriter(function(fileWriter) {
        fileWriter.onwriteend = function(e) {
          // for real-world usage, you might consider passing a success callback
          console.log('Speichern von "' + fileName + '"" abgeschlossen.');
        };

        fileWriter.onerror = function(e) {
          // you could hook this up with our global error handler, or pass in an error callback
          console.log('speichern fehlgeschlagen ' + e.toString());
        };

        var blob = new Blob([fileData], {
          type: 'application/pdf'
        });
        fileWriter.write(blob);
      }, );
    }, );
  }, );
}

我没有收到错误消息,但文件没有出现。 另外,我想将 PDF 保存在“文档”中,而不是“.file.dataDirectory”中

谁能告诉我我做错了什么?

【问题讨论】:

    标签: javascript cordova pdf save


    【解决方案1】:

    哦,是的,我在三天后解决了它
    其实我只需要改一行代码

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    

    LocalFileSystem.PERSISTENT 仅适用于 Android WITH ROOT

    LocalFileSystem.PERSISTENT 代表 Android 中的系统 URL,您可以在文档中看到:File System Layouts

    我把它改成了 cordova.file.externalDataDirectory

    在 Phongap 桌面上输出 NULL 您必须构建应用程序才能工作

    所以我想出了这个:

    savePDF("Storecheck.pdf", pdfOutput)
    
     function savePDF(fileName, fileData) {
    
                    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) {
                        dir.getFile(fileName, {create: true, exclusive: false}, function (fileEntry) {
                            fileEntry.createWriter(function (writer) {
                                writer.onwrite = function (evt) {
                                    console.log("write success");
                                    alert(fileName+" wurde gespeichert unter " + cordova.file.externalDataDirectory );
                                };
    
                                console.log("writing to file");
                                writer.write(fileData);
                            })
    
                        }, function () {
    
    
                            console.log("ERROR SAVEFILE");
                            alert ("heute gibts keine PDF")
    
                        });
                    });
                }
    

    【讨论】:

    • 如果我想打开那个 pdf 文件怎么办?
    【解决方案2】:

    根据the cordova-plugin-file documentaion我更改了功能。 https://jsfiddle.net/frief/7umnwj0r/1/

    我也加了

    <preference name="AndroidPersistentFileLocation" value="Internal" />
        <preference name="AndroidPersistentFileLocation" value="Compatibility" />
        <preference name="AndroidExtraFilesystems" value="sdcard,cache" />
    到 config.xml 现在看起来像这样

    <?xml version='1.0' encoding='utf-8'?>
    <widget id="de.check" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <name> check</name>
    <description>
        check
    </description>
    <author email="info@heins.de" href="http://ally.de">
        Development
    </author>
    
    
    <preference name="AndroidPersistentFileLocation" value="Internal" />
    <preference name="AndroidPersistentFileLocation" value="Compatibility" />
    <preference name="AndroidExtraFilesystems" value="sdcard,cache" />
    
    <preference name="iosPersistentFileLocation" value="Library" />
    <preference name="iosPersistentFileLocation" value="Compatibility" />
    
    
    <!-- Don't block any requests -->
    <access origin="*" />
    
    <content src="index.html" />
    <plugin name="cordova-plugin-battery-status" spec="~1.2.2" />
    <plugin name="cordova-plugin-camera" spec="~2.3.1" />
    <plugin name="cordova-plugin-file" spec="~4.3.1" />
    <plugin name="cordova-plugin-file-transfer" spec="~1.6.1" />
    <plugin name="cordova-plugin-geolocation" spec="~2.4.1" />
    <plugin name="cordova-plugin-vibration" spec="~2.1.3" />
    <plugin name="cordova-plugin-statusbar" spec="~2.2.1" />
    <plugin name="cordova-plugin-whitelist" source="npm"/>
    
    <engine name="android" spec="^6.4.0" />
    
    
    </widget>

    现在我遇到的问题是我总是收到无法创建文件的错误。 我认为这与访问权限有关,但我不确切知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2015-11-03
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多