我如何在浏览器中打开pdf而不是在webapi中下载

时间:2018-08-13 05:22:32

标签: pdf asp.net-web-api selectpdf

我正在使用selectPdf在我的webapt中生成动态pdf。选择PDF下载此pdf作为附件,我希望此PDF应该显示在浏览器选项卡中而不是下载。以下是我的代码。

numpy.kron(w,w) 

1 个答案:

答案 0 :(得分:0)

    [HttpGet]
    [Route("myapplication")]
    [AllowAnonymous]
    private System.Net.Http.HttpResponseMessage GeneratePDF(string guid, bool isAr, out string fileName)
        {
            string htmlString = ApplicationService.GeneratePDFHTMLString(guid, isAr, out fileName);
            string pdf_page_size = "A4";
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                pdf_page_size, true);
    
    
            string pdf_orientation = "Portrait";
            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                pdf_orientation, true);
    
            int webPageWidth = 748;
    
    
            int webPageHeight = 0;
    
    
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
    
    
            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.MarginTop = 20;
    
            converter.Options.WebPageHeight = webPageHeight;
            converter.Options.ExternalLinksEnabled = true;
    
            converter.Options.EmbedFonts = true;
            converter.Options.KeepTextsTogether = true;
    
            try
            {
                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertHtmlString(htmlString);
                doc.CompressionLevel = PdfCompressionLevel.NoCompression;
    
                //var contents = doc.Save();
    
                //doc.Close();
                //return contents;
    
                MemoryStream pdfStream = new MemoryStream();
    
                // save pdf document into a MemoryStream
                doc.Save(pdfStream);
    
                // reset stream position
                pdfStream.Position = 0;
    
                System.Net.Http.HttpResponseMessage response = new System.Net.Http.HttpResponseMessage();
                response.StatusCode = System.Net.HttpStatusCode.OK;
                //response.Content = new System.Net.Http.StreamContent(pdfStream);
                response.Content = new System.Net.Http.ByteArrayContent(pdfStream.ToArray());
                 //response.Content.Headers.ContentDisposition = new 
                 // System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                 // {
                 //      FileName = "foo.pdf"
                 //  };
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
                doc.Close();
                return response;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

尝试一下