I am using Itexsharp.dll for generate html to pdf with webapi and angular 2, but I am return bytes array from webapi and open bytes in angular 2 with Blob but when open pdf it was blank. My code is here
IN Web api code: -
HttpResponseMessage response = new HttpResponseMessage();
MemoryStream workStream = new MemoryStream();
StringReader sr = new StringReader(htmlPdfContent.ToString());
using (MemoryStream memoryStream = new MemoryStream())
{
using (Document pdfDoc = new Document(PageSize.A4))
{
PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
dynamic parsedHtmlElements;
try
{
parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(htmlPdfContent), null);
}
catch (Exception ex)
{
throw ex;
}
foreach (var htmlElement in parsedHtmlElements)
{
try
{
pdfDoc.Add(htmlElement);
}
catch (Exception e)
{
throw e;
}
}
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
response.Content = new ByteArrayContent(bytes);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");
response.Content.Headers.ContentDisposition.FileName = "Employee.pdf";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
}
}
In angular 2 :- In angular side getting response and pdf also open in window but blank open can you help me what is wrong in it
return this.http.get(uri, { headers: this.Header() })
.map((response: Response) => {
const mediaType = 'application/pdf';
const blob = new Blob([response._body], {type: mediaType});
const filename = 'test.pdf';
// saveAs(blob, filename);
// window.open(resp, '_blank');
const fileURL = URL.createObjectURL(blob);
window.open(fileURL);
}).catch();
Header() {
const headers = new Headers();
headers.append('Accept', 'application/json');
return headers;
}
HTMLWorker, a class that was abandoned a long time ago because it frustrated so many developers. If you don't like being frustrated, why don't you upgrade to iText 7 and the pdfHTML add-on? Why do you talk about iTextSharp, while the name was changed to iText for .NET in 2016. Why are you using iText 5 instead of iText 7?