2017-08-26 57 views
0

尝试在浏览器中显示reportlab生成的pdf。在Firefox中它显示正常。但铬/ IE没有显示PDF。在浏览器中生成非常基本的PDF和显示以便我可以通过打印机轻松打印的最简单方法是什么?在新窗口浏览器中打开Ajax响应(pdf)。在Firefox中使用Firefox bur错误(Django + Javascript)

Django的视图(生成PDF):

def print_users(request,*args,**kwargs): 
     # Create the HttpResponse object with the appropriate PDF headers. 
     response = HttpResponse(content_type='application/pdf; charset=utf-8') 
     response['Content-Disposition'] = 'attachment; filename="MyUsers.pdf";charset=utf-8' 
     buffer = BytesIO() 
     report = MyPrint(buffer, 'Letter') 
     pdf = report.print_users() 
     response.write(pdf) 
     return response** 

Ajax调用(查看PDF):从服务器

$.ajax({ 
url: "/order/print/users/", 
type: "post", 
//data: { "category": myObject } 
}).done(function (result) { 
    window.open("data:application/pdf," + encodeURIComponent(result)); 
}).fail(function (jqXHR, textStatus, errorThrown) { 
// needs to implement if it fails 
}); 

响应 - > 的console.log(结果) - >

%PDF-1.4 
%���� ReportLab Generated PDF document http://www.reportlab.com 
1 0 obj 
<< 
/F1 2 0 R 
>> 
endobj 
2 0 obj 
<< 
/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font 
>> 
endobj 
3 0 obj 
<< 
/Contents 7 0 R /MediaBox [ 0 0 612 792 ] /Parent 6 0 R /Resources << 
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] 
>> /Rotate 0 /Trans << 

>> 
    /Type /Page 
>> 
endobj 
4 0 obj 
<< 
/Outlines 8 0 R /PageMode /UseNone /Pages 6 0 R /Type /Catalog 
>> 
endobj 
5 0 obj 
<< 
/Author (\(anonymous\)) /CreationDate (D:20170826165648-06'00') /Creator (\(unspecified\)) /Keywords() /ModDate (D:20170826165648-06'00') /Producer (ReportLab PDF Library - www.reportlab.com) 
    /Subject (\(unspecified\)) /Title (\(anonymous\)) /Trapped /False 
>> 
endobj 
6 0 obj 
<< 
/Count 1 /Kids [ 3 0 R ] /Type /Pages 
>> 
endobj 
7 0 obj 
<< 
/Filter [ /ASCII85Decode /FlateDecode ] /Length 201 
>> 
stream 
Gat%YbmM<A&;9LtME.\mAj^;*W1^%.Pi`-akc`QFS%cYl."[email protected]':R;[email protected])u:fGgoQ"B$oW-sB%n)@%K.K&4U[K1%1!ZR:CWQ%EfD+`Xu>;.Q>uOYJq?Y0!e9mk[3#\3$>/@2bNPa?R#F2WCj/^6.,Qf0ujK$W?H[[;rbMmg.h3OkihD]n_K3:m-t`5/\*[email protected]~>endstream 
endobj 
8 0 obj 
<< 
/Count 0 /Type /Outlines 
>> 
endobj 
xref 
0 9 
0000000000 65535 f 
0000000073 00000 n 
0000000104 00000 n 
0000000211 00000 n 
0000000404 00000 n 
0000000488 00000 n 
0000000771 00000 n 
0000000830 00000 n 
0000001121 00000 n 
trailer 
<< 
/ID 
[<fb3a83ec0486d40485c0b5b76feed691><fb3a83ec0486d40485c0b5b76feed691>] 
% ReportLab generated PDF document -- digest (http://www.reportlab.com) 

/Info 5 0 R 
/Root 4 0 R 
/Size 9 
>> 
startxref 
1167 
%%EOF 

回答

0

试试看:

window.open("data:application/IMG," + escape(result)); 
相关问题