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

时间:2018-03-28 12:07:47

标签: angular asp.net-web-api itext

我正在使用Itexsharp.dll生成带有webapi和angular 2的html到pdf,但是 我是来自webapi的返回字节数组,并且使用Blob以角度2打开字节,但是当打开pdf时它是空白的。我的代码在这里

在Web api代码中: -

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也在窗口打开但空白打开你可以帮我解决它的错误

     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 个答案:

答案 0 :(得分:0)

map运算符用于以所需格式解析您的响应,并返回一个可以订阅的observable。

修改方法

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

并在您的组件中订阅

 this.service
      .downloadPDF()
      .subscribe(data => {
        const fileUrl = URL.createObjectURL(data);
         window.open(fileUrl);
      });