duanqujing3863 2018-08-03 15:14
浏览 296

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

The problem is that I'm POSTing some data to the PHP file, the post data are some parameters in from which the PDF will generate, I'd do it with an anchor or a normal form, but I'm posting complex data which JavaScript is helping me with.

I used console.log(data) on my success function of the JQuery .ajax method, and it prints the following.

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

I'm sending the data to the client with a response, and I want to open a new tab-pop-up window with the PDF generated, but window.open() isn't doing the trick.

  • 写回答

1条回答 默认 最新

  • dongxun8189 2018-08-08 13:22
    关注

    As @MonkeyZeus mentioned, you have to call an iFrame to do the job because most browsers are blocking data:URL because of phishing and other issues (arrgh!), I tested opening the data:URL in Chromium and Mozilla Quantum, it worked, but Chrome won't load until you refresh the page (in my case, at least), so the code in the .ajax success status would be something like this:

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

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配