I have a servlet that write a pdf file as a ByteArrayOutputStream to the servlet's output stream. If I open the servlet URL the browser opens the file. But if occur an error on the servlet, the browser opens an empty pdf with an error message. Sending an error through the ServletResponse the browser opens the default error page.
I want to send an error message without redirecting to an error page or opening an invalid pdf file.
I tried:
new Ajax.Request('/pdfservlet', {
onSuccess: function(response) {
docWindow = window.open('','title');
docWindow.document.open('application/pdf');
docWindow.document.write(response);
docWindow.document.close();
},
onFailure: function(response) {
alert(response);
}
});
But, onSuccess opens a page with [object object]
How can I open a PDF file using JavaScript?