Generate Html to Pdf using ItextSharp blank file generate

China☆狼群 提交于 2020-01-05 09:14:00

问题


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;
    }

回答1:


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);
      });


来源:https://stackoverflow.com/questions/49534153/generate-html-to-pdf-using-itextsharp-blank-file-generate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!