在Ajax上获取PDF的输出流,并在另一个文档上打开它



问题是,我正在将一些数据发布到PHP文件中,发布数据是PDF将从中生成的一些参数,我会使用锚点或普通表单来完成,但我发布的是JavaScript正在帮助我处理的复杂数据。

我在JQuery.ajax方法的成功函数中使用了console.log(data),它打印了以下内容。

HTTP/1.0 200 OK
Cache-Control:       no-cache, private
Content-Disposition: inline; filename="document.pdf"
Content-Type:        application/pdf
Date:                Fri, 03 Aug 2018 14:48:08 GMT
%PDF-1.3
1 0 obj
<< /Type /Catalog
/Outlines 2 0 R
/Pages 3 0 R >>
endobj
2 0 obj
<< /Type /Outlines /Count 0 >>
endobj
3 0 obj
<< /Type /Pages
/Kids [6 0 R
]
/Count 1
/Resources <<
/ProcSet 4 0 R
/Font << 
/F1 8 0 R
/F2 9 0 R
>>
/ExtGState << 
/GS1 10 0 R
/GS2 11 0 R
/GS3 12 0 R
/GS4 13 0 R
>>
>>
/MediaBox [0.000 0.000 595.280 841.890]
>>
endobj
4 0 obj
[/PDF /Text ]
endobj
5 0 obj
<<
/Producer (�� d o m p d f)
/CreationDate (D:20180803104808-04'00')
/ModDate (D:20180803104808-04'00')
>>
endobj
6 0 obj
<< /Type /Page
/MediaBox [0.000 0.000 595.280 841.890]
/Parent 3 0 R
/Contents 7 0 R
>>
endobj
7 0 obj
<< /Filter /FlateDecode
/Length 673 >>
stream
...lots of stream chars goes here...
endstream
endobj
8 0 obj
<< /Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>
endobj
9 0 obj
<< /Type /Font
/Subtype /Type1
/Name /F2
/BaseFont /Helvetica-Bold
/Encoding /WinAnsiEncoding
>>
endobj
10 0 obj
<< /Type /ExtGState
/BM /Normal
/ca 0.67
>>
endobj
11 0 obj
<<
/Type /ExtGState
/BM /Normal
/CA 0.67
>>
endobj
12 0 obj
<< /Type /ExtGState
/BM /Normal
/ca 1
>>
endobj
13 0 
obj
<< /Type /ExtGState
/BM /Normal
/CA 1
>>
endobj
xref
0 14
0000000000 65535 f 
0000000009 00000 n 
0000000074 00000 n 
0000000120 00000 n 
0000000350 00000 n 
0000000379 00000 n 
0000000538 00000 n 
0000000641 00000 n 
0000001386 00000 n 
0000001493 00000 n 
0000001605 00000 n 
0000001665 00000 n 
0000001725 00000 n 
0000001782 00000 n 
trailer
<<
/Size 14
/Root 1 0 R
/Info 5 0 R
/ID[<f9dc912e3da657434ee0495c45e20e3a><f9dc912e3da657434ee0495c45e20e3a>]
>>
startxref
1839
%%EOF

我正在将数据发送到客户端并做出响应,我想打开一个新的选项卡弹出窗口,其中包含生成的PDF,但window.open((没有起到作用。

正如@MonkeyZeus所提到的,你必须调用iFrame来完成这项工作,因为大多数浏览器都在屏蔽数据:URL是因为网络钓鱼和其他问题(arrgh!(,我在Chromium和Mozilla Quantum中测试了打开数据:URL,它起作用了,但Chrome在你刷新页面之前不会加载(至少在我的情况下(,所以.ajax成功状态中的代码应该是这样的:

...
success:function(res){
var pdf= window.open("")
pdf.document.write("<iframe width='100%' height='100%'"+
" src='data:application/pdf;base64, " + encodeURI(res)+"'></iframe>")
}
...

最新更新