Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将字符串响应转换为ArrayBuffer?_Javascript_Python_Django_Pdf_Pdfkit - Fatal编程技术网

Javascript 如何将字符串响应转换为ArrayBuffer?

Javascript 如何将字符串响应转换为ArrayBuffer?,javascript,python,django,pdf,pdfkit,Javascript,Python,Django,Pdf,Pdfkit,从服务器上我得到了字符串格式的pdf响应,现在在客户端,我想把它转换成ArrayBuffer,这样我们就可以在pdf中再次更改 我正在将其转换为pdf格式,如下所示,但不是说pdf已损坏。 function str2ab(str) { var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char var bufView = new Uint8Array(buf); f

从服务器上我得到了字符串格式的pdf响应,现在在客户端,我想把它转换成ArrayBuffer,这样我们就可以在pdf中再次更改

我正在将其转换为pdf格式,如下所示,但不是说pdf已损坏。

function str2ab(str) {
          var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
          var bufView = new Uint8Array(buf);
          for (var i=0, strLen=str.length; i < strLen; i++) {
            bufView[i] = str.charCodeAt(i);
          }
          return buf;
    }
使用pdf工具包将其转换为字符串格式

add_str = render_to_string(address_template_path , {"address_dict": address_dict})
pdfkit.from_string(add_str, file_location)

address_pdf = open(file_location)
response = HttpResponse(address_pdf.read(), content_type='application/pdf')  # Generates the response as pdf response.
response['Content-Disposition'] = 'inline;filename= %s' % envelope_id

找到一个可能对你有帮助的线程@ManojSureddi我尝试了这个链接,但它不起作用。@gieks“我将uint16数组更改为uint8数组”,你到底为什么这么做?你明白其中的区别吗?你对文件编码了解多少?@Thomas我尝试了这两种方法,但都不起作用,我改变了,因为pdfkit使用utf-8编码格式…@geeks,嗯,ArrayBuffer只是一个没有意义的字节集合(它们自己)。从您的代码中,我不确定您的响应字符串中是哪一个,
utf-8
-编码字节还是正确的“字符”。由于我没有处理blob和
a.download
的经验,我不知道您需要什么输出。我唯一确信的是,你必须以某种方式对这件事进行转码;不仅仅是将字符码的最后8位放入Uint8Array,让ArrayBuffer的另一半空着,找到一个可能对您有帮助的线程@ManojSureddi我尝试了这个链接,但它不起作用。@geeks“我将Uint16Array更改为Uint8Array”,您到底为什么这样做?你明白其中的区别吗?你对文件编码了解多少?@Thomas我尝试了这两种方法,但都不起作用,我改变了,因为pdfkit使用utf-8编码格式…@geeks,嗯,ArrayBuffer只是一个没有意义的字节集合(它们自己)。从您的代码中,我不确定您的响应字符串中是哪一个,
utf-8
-编码字节还是正确的“字符”。由于我没有处理blob和
a.download
的经验,我不知道您需要什么输出。我唯一确信的是,你必须以某种方式对这件事进行转码;不只是将字符码的最后8位推入UINT8数组,而将ArrayBuffer的另一半留空
add_str = render_to_string(address_template_path , {"address_dict": address_dict})
pdfkit.from_string(add_str, file_location)

address_pdf = open(file_location)
response = HttpResponse(address_pdf.read(), content_type='application/pdf')  # Generates the response as pdf response.
response['Content-Disposition'] = 'inline;filename= %s' % envelope_id