Node-Webkit Download PDF

孤者浪人 提交于 2019-12-23 03:16:39

问题


I'm building a node-webkit app where I receive a request from a website to download a particular file. When I initiate the web service call I get back the file in the response.body. I'm trying to use the example in the fs api to save the pdf to my local folder i.e.:

(i'm passing the response.body into the data field, and passing a string 'binary' to specify encoding under options)

var options = { encoding: 'binary' };
console.log('File name:' + fileName);
fileOperations.write(fileName, response.body, options, null);

In fileOperations:

module.exports = {
    write: function (filename, data, options, callback) {
    fs.writeFile(filename, data, options, function (err) {
      if (err) throw err;
      console.log('It\'s saved!');
    });
  }
};

The file is saved to the local folder with the correct file name and extension, as well as file size. However when opened in preview each page is blank. Am I specifying the wrong encoding type?


回答1:


This sounds similar to a problem I had. Downloading files (not just pdf) resulted in strange results. This is more likely your issue....not the fs functions. Rather than using the built in node http stuff we chose to use the Request library (npm request) and performed downloads in this fashion:

 request({
      method: 'GET',
      uri: baseUrl + '/api/v1/documents/versions/contents/doc33',
      headers: {"Access-Control-Allow-Origin": baseUrl, "Cookie": cookie}
    }, function (error, response, body) {

      var contentDisp = response.headers['content-disposition'].split('"');
      var ext = contentDisp[1].split('.')[1];

      // you can rename the downloaded file (temp) and add the proper extension here...

    }).pipe(fs.createWriteStream('temp')); // you can append a directory to the temporary name as well..
}

I would give this a shot and see if it works for you. Working with files across platforms can be difficult.




回答2:


To export a pdf file from your node-webkit app. Frist you'll need pdfkit; you can see doc here ..

Then you have just to require it in you js :

var pdfkit = require('pdfkit');
var fs = require('fs');

You have to use a save as box like here: An example from my app :

<button id="export">Export</button>
<input style="display:none" id="fileDialog" type="file" class="button small" accept=".pdf" nwsaveas="">

With the export function:

$('#export').click(function(event) {
    chooseFile('#fileDialog');
});

function chooseFile(name)
{
    var chooser = $(name);
    chooser.change(function(event) {
        event.preventDefault();
        exp_to = $(this).val(); // where to export
            console.log($(this).val());
        exporT();
    });
        chooser.trigger('click');
}

and =>finally

function exporT() // export() is already reserved
    {
        event.preventDefault();
        var doc = new pdfkit();
        doc.pipe(fs.createWriteStream(exp_to));
        doc.fontSize(20) // font size
            .text('your text here')//.whatyouwant()

            .end();
    }


来源:https://stackoverflow.com/questions/23767663/node-webkit-download-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!