簡體   English   中英

如何在瀏覽器中打開 pdf 而不是在 webapi 中下載

[英]how can i open pdf in browser instead of download in webapi

我正在使用 selectPdf 在我的 webapt 中生成動態 pdf。 選擇 PDF 下載此 pdf 作為附件,我希望此 PDF 應顯示在瀏覽器選項卡中,而不是下載。 以下是我的代碼。

   [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/octet-stream");
            doc.Close();
            return response;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    [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;
            }
        }

嘗試這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM