Show a pdf stream in a new window

后端 未结 3 533
感动是毒
感动是毒 2020-12-09 18:19

I\'m generating in a server a PDF document that I want to show then in the client. The server side looks like following:

ByteArrayOutputStream baos = generat         


        
相关标签:
3条回答
  • 2020-12-09 18:53

    I couldn't do this async, but this js returns the attachment ok for me:

    $('<iframe src="url"></iframe>').appendTo('body').hide();

    The browser then fires a save/view popup, which is fine for my requirements; no error handling though.

    I think with your server side, you might want to return it as inline e.g. response.setHeader("Content-Disposition", "inline; filename=file.pdf");

    You're setting the content length OK, it could be the success code will be firing twice, the first time at the beginning of the stream and the second time at the end.

    Do let us know if you got this working.

    0 讨论(0)
  • 2020-12-09 18:59

    You can get base64 string of your pdf stream and pass it to response.

    And your method change

    $.ajax({
        type: "POST",
        url: url,
        data: {"data": JSON.stringify(myData)},
        success: function(data, textStatus, jqXHR) {
            var pdfWin= window.open("data:application/pdf;base64, " + data, '', 'height=650,width=840');
            // some actions with this win, example print...
        },
        error: function(jqXHR) {
            showError("...");
        }
    });
    
    0 讨论(0)
  • 2020-12-09 19:06

    Try using:

    dataType: "application/pdf",
    success: function(data, textStatus, jqXHR) {
        window.open(escape(data), "Title", "");
    },
    
    0 讨论(0)
提交回复
热议问题