使用ItextSharp空白文件生成HTML到Pdf

使用ItextSharp空白文件生成HTML到Pdf

本文介绍了使用ItextSharp空白文件生成HTML到Pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Itexsharp.dll通过webapi和angular 2生成html到pdf,但是我从webapi返回字节数组,并使用Blob在angular 2中打开字节,但是在打开pdf时为空白.我的代码在这里

I am using Itexsharp.dll for generate html to pdf with webapi and angular 2, butI 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代码:-

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

在角度2中:-在有角的一面,响应和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;
    }

推荐答案

map运算符用于以所需格式解析您的响应,并返回一个可观察的变量,该变量将被订阅.

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

修改您的方法

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

这篇关于使用ItextSharp空白文件生成HTML到Pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:07