I returned byte[] from WebAPI controller and download PDF successfully.
I'm using iTextSharp (LGPL) 4.1.6 free PDF converter.
To install iTextSharp (LGPL / MPL), run the following command in the Package Manager Console.
Install-Package iTextSharp-LGPL -Version 4.1.6
Server side code
[Route("Export/ExportToPdf")]
public byte[] ExportToPdf(string html)
{
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);
Document document = new Document(new Rectangle(842, 595));
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
document.Open();
document.HtmlStyleClass = @"<style>*{ font-size: 8pt; font-family:arial;}</style>";
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(html), null);
foreach (var htmlElement in parsedHtmlElements)
{
document.Add(htmlElement as IElement);
}
document.Close();
return msOutput.ToArray();
}
Client Side Code.
//Call this function inside of AJAX success.
function ExportToPDF(data) {
//base64 To ArrayBuffer
var binaryString = window.atob(data);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
//-------
var link = document.createElement('a');
link.href = window.URL.createObjectURL(new Blob([bytes], { type: 'application/pdf' }));
link.download = "Report.pdf";
link.click();
}