User-1949942929 posted
Question: I have WebMethod as illustrated below that should return a byte[]. The byte[] then needs to be retrieved by AJAX call and display the pdf file.
[WebMethod]
public static byte[] GeneratePDF()
{
var pdfPath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/PDFTemplates/W9.pdf"));
PdfReader pdfReader = new PdfReader(pdfPath);
MemoryStream stream = new MemoryStream();
PdfStamper stamper = new PdfStamper(pdfReader, stream);
pdfReader.Close();
stamper.Close();
stream.Flush();
stream.Close();
byte[] bytes = System.IO.File.ReadAllBytes(pdfPath);
return bytes;
}
Note: for AJAX call, I have following AngularJS code
function GeneratePDF() {
$http.post('/Default.aspx/GeneratePDF', '', { responseType: 'arraybuffer' }).then(function (response) {
var blob = new Blob([new Uint8Array(response.data)], { type: 'application/pdf' });
$scope.url = (window.URL || window.webkitURL).createObjectURL(blob);
window.open($scope.url);
});
}
And html anchor below for downloading pdf file