如何保存由node.js服务器发送的pdf文件?

我试图保存由node.js服务器发送的pdf文件。PDF是用pdfkit创建的,这是响应的日志:

    {"readyState":4,"responseText":"%PDF-1.3
%����
5 0 obj
<<
/Type /Page
/Parent 1 0 R
/MediaBox [0 0 612 792]
/Contents 3 0 R
/Resources 4 0 R
>>
endobj
4 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
>>
endobj
10 0 obj
<<
/Type /ExtGState
/ca 1
>>
endobj
8 0 obj
<<
/Type /Page
/Parent 1 0 R
/MediaBox [0 0 612 792]
/Contents 6 0 R
/Resources 7 0 R
>>
endobj
7 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 9 0 R
>>
/ExtGState <<
/Gs1 10 0 R
>>
>>
endobj
14 0 obj
<<
/S /URI
/URI (http://google.com/)
>>
endobj
15 0 obj
<<
/Subtype /Link
/A 14 0 R
/Type /Annot
/Rect [100 665 260 692]
/Border [0 0 0]
>>
endobj
13 0 obj
<<
/Type /Page
/Parent 1 0 R
/MediaBox [0 0 612 792]
/Contents 11 0 R
/Resources 12 0 R
/Annots [15 0 R]
>>
endobj
12 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/ExtGState <<
/Gs1 10 0 R
>>
/Font <<
/F1 9 0 R
>>
>>
endobj
16 0 obj
<<
/Producer (PDFKit)
/Creator (PDFKit)
/CreationDate (D:20160409123048Z)
>>
endobj
9 0 obj
<<
/Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/Type /Catalog
/Pages 1 0 R
>>
endobj
1 0 obj
<<
/Type /Pages
/Count 3
/Kids [5 0 R 8 0 R 13 0 R]
>>
endobj
3 0 obj
<<
/Length 23
/Filter /FlateDecode
>>
stream
x�3T0\u0000B]C ani����\u0005\u0000\u001dI\u0003\\
endstream
endobj
11 0 obj
<<
/Length 117
/Filter /FlateDecode
>>
stream
x�e�A
\u00021\u0010\u0004�����f&n&\u0006d\u000f��g%/p����\u0004|��Y�.
�\u0016b�J\u001c�D�\u0017¾}\u001e���D��[��-\bs\u0017�w�!��B�I���D��p\u0014�n7솭�\u001c5E֒7N��\u0016mz�2R}�Pq�\u0017Zg\u001ep
endstream
endobj
6 0 obj
<<
/Length 152
/Filter /FlateDecode
>>
stream
x�e�A
�@\fE�9E.`�d:�\u0005qQԮ����#�\u0015���͸p#\u001f��O�Frm؇��y�\u0017��7$`\"T�\u001a��&hO��T`�m5�h\u0014�ނ�\u0005-:��T�\u0006봘�����t���A��=�;\u001c\u0013�k�Gp$\\�$N\u000f�\u001f���}��e\u001c0��c#^p�OhǕ��B�\u000f-�,�
endstream
endobj
xref
0 17
0000000000 65535 f 
0000001091 00000 n 
0000001042 00000 n 
0000001161 00000 n 
0000000119 00000 n 
0000000015 00000 n 
0000001445 00000 n 
0000000335 00000 n 
0000000231 00000 n 
0000000945 00000 n 
0000000186 00000 n 
0000001255 00000 n 
0000000733 00000 n 
0000000609 00000 n 
0000000453 00000 n 
0000000509 00000 n 
0000000852 00000 n 
trailer
<<
/Size 17
/Root 2 0 R
/Info 16 0 R
>>
startxref
1669
%%EOF
","status":200,"statusText":"OK"}

这是我的客户端代码:

var data = {};
data.azione = "getEtichettaProdotto";

//Scarico i dati anagrafica
$.ajax({
    type: 'POST',
    data: JSON.stringify(data),
    contentType: 'application/json',
    url: 'http://46.101.209.16/endpoint',                       
    complete : function(etichettas) {
        console.log(JSON.stringify(etichettas));

        var blob = new Blob([etichettas.responseText], {type: "application/pdf"});
        saveAs(blob, "Label.pdf");
    }//SUCCESS
});

我的node.js代码 :

    PDFDocument = require('pdfkit');

function creaEtichetta(req, res){
    doc = new PDFDocument
        size: 'a4'
        bufferPages: true


    doc.pipe(res);
    doc.addPage().fontSize(25).text('Here is some vector graphics...', 100, 100);

    doc.save()
   .moveTo(100, 150)
   .lineTo(100, 250)
   .lineTo(200, 250)
   .fill("#FF3300");

   doc.addPage().fillColor("blue").text('Here is a link!', 100, 100).link(100, 100, 160, 27, 'http://google.com/')

    doc.end();
}
exports.creaEtichetta = creaEtic

请帮助我,我已经困扰一个星期了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐