1

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;
    }
2
  • where are you subscribing it? Commented Mar 28, 2018 at 12:38
  • Please read the introduction of the PDF to HTML tutorial. You're using 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? Commented Mar 28, 2018 at 14:19

1 Answer 1

0

map operator is used to parse your response in the desired format and it returns an observable which shall be subscribed.

Modify your method

   downloadPDF():any{
     return this.http.get(uri, { headers: this.Header() })
                .map((response: Response) => {
                    const mediaType = 'application/pdf';
                   return new Blob([response._body], {type: mediaType});

                }).catch();
                 }

and subscribe it in your component

 this.service
      .downloadPDF()
      .subscribe(data => {
        const fileUrl = URL.createObjectURL(data);
         window.open(fileUrl);
      });
Sign up to request clarification or add additional context in comments.

1 Comment

My coding is same as per your but i am not write in my question so its not helpful thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.