简体   繁体   English

Node-Webkit下载PDF

[英]Node-Webkit Download PDF

I'm building a node-webkit app where I receive a request from a website to download a particular file. 我正在构建一个node-webkit应用程序,在该应用程序中,我从网站接收到下载特定文件的请求。 When I initiate the web service call I get back the file in the response.body. 当我发起Web服务调用时,我在response.body中取回文件。 I'm trying to use the example in the fs api to save the pdf to my local folder ie: 我正在尝试使用fs api中的示例将pdf保存到本地文件夹,即:

(i'm passing the response.body into the data field, and passing a string 'binary' to specify encoding under options) (我将response.body传递到数据字段中,并传递字符串'binary'以指定选项下的编码)

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

In fileOperations: 在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? 我指定的编码类型错误吗?

This sounds similar to a problem I had. 这听起来与我遇到的问题相似。 Downloading files (not just pdf) resulted in strange results. 下载文件(不仅仅是pdf)会导致奇怪的结果。 This is more likely your issue....not the fs functions. 这很可能是您的问题。。。不是fs函数。 Rather than using the built in node http stuff we chose to use the Request library (npm request) and performed downloads in this fashion: 我们没有使用内置节点http的东西,而是选择使用Request库(npm request)并以这种方式执行下载:

 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. 跨平台处理文件可能很困难。

To export a pdf file from your node-webkit app. 要从您的node-webkit应用程序导出pdf文件。 Frist you'll need pdfkit; 首先,您需要pdfkit; you can see doc here .. 你可以在这里看到文档..

Then you have just to require it in you js : 然后,您只需要在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();
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM