如何通过Node.js以正确的编码保存pdf [英] How to save pdf in proper encoding via nodejs

查看:186
本文介绍了如何通过Node.js以正确的编码保存pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试使用脚本从网站下载pdf文件,但问题是该文件在此过程中被破坏了,我很确定这是因为使用了错误的编码.

So I'm trying to download a pdf file from a website with my script but the problem is that the file gets broken in the process and I'm pretty sure it's because of wrong encoding being used.

我正在使用请求库下载文件,并且已将Content-type设置为application-pdf

I'm using request lib for downloading the file and I've set the Content-type to application-pdf

我的代码非常简单:4

My code is pretty simple:4

var fs = require('fs');
var request = require("request");


request({uri: 'xxxxxxxxxxxxxx.pdf', headers: { 'Content-type' : 'applcation/pdf' }} , function (error, response, body) {
  if (!error && response.statusCode == 200) {
    fs.writeFileSync("10111.pdf", body);
  }
})

我需要在哪里指定用于此工作的编码?

Where do I need to specify the encoding used for this to work?

我尝试打开通过正常保存得到的pdf文件,SublimeText3 encodinghelper说它在Windows中,而我下载的文件在utf8中.

I tried opening the pdf that I get by normal saving and SublimeText3 encodinghelper says it's in Windows-something while the one I downloaded is in utf8.

我已经遍历了nodejs缓冲区和fs文件,它们不支持Windows-asd之类的编码,仅支持诸如'utf8'和'binary'之类的常规编码.

I've gone through the nodejs buffer and fs files and they do not supprt encodings like windows-asd, only the general ones like 'utf8' and 'binary'.

我应该使用其他方法来获取文件吗?

Should I maybe use a different method for obtaining the file?

推荐答案

我很晚才知道,但是我今天看到了您的问题,所以我正在回答,以便其他人可以从中获得帮助. 您可以在尝试写入文件时添加编码,即-:

I know its very late but i seen your question today so i am answering it so that other can get help from this. You can add the encoding when you are trying to write the file i.e-:

fs.writeFileSync("10111.pdf", body,'binary');

我在此处将编码格式设置为二进制,如果您要下载可将编码设置为null的pdf,则可以根据需要使用哪种编码格式是正确的.

As i set encoding format as binary here you can use which is right encoding format according to your requirenment if you are trying to download a pdf that you can set encoding as null.

希望这会有所帮助

这篇关于如何通过Node.js以正确的编码保存pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆