Need to convert stream file to PDF.

SAHA Arunava 1 Reputation point
2021-08-19T12:36:05.217+00:00

S is my stream variable.
I have successfully converted it into doc format. Now I am trying for pdf one.
Content Type: "application/pdf" doesn't work.
Please give me a solution.

            s = ac.ExportApplicationResultDocument(templatePath, spiderDiagram, bestDiagram, worstDiagram,
                        this._positionsNormalCaseResults, this._positionsWorstCaseResults,
                        this._normalCaseRequiredTrainings, this._worstCaseRequiredTrainings,
                        this._assessedLevel, this._justification, this._normalCaseResultingDecision,
                        this._worstCaseResultingDecision,
                        (InternalStatus)application.IdCurrentApplicationStatus.Value,
                        applicant.FirstName,
                        applicant.LastName,
                        string.Format(Resources.TextResources.ResultPageGlobalResultEADSCorpId, applicant.EADSPersonalID), worstCaseTableImage, bestCaseTableImage, this._assessedDate);
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=Result.docx");

                    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    s.CopyTo(Response.OutputStream);
                    Response.End();
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,182 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 4,366 Reputation points Microsoft Vendor
    2021-08-20T03:02:42.99+00:00

    Hi @SAHA Arunava

    Response.AddHeader("Content-Disposition", "attachment; filename=Result.docx");

    Does Response represent HttpResponse ?

    After search the usage if your code, I am sure your code is ok in .net framework. And in .net core , we can open pdf by return File() , if you want to download it, you can use return new FileContentResult.

    1. Open PDF:

    public IActionResult OpenPDF()  
    {  
            var globalSettings = new GlobalSettings  
            {  
                ColorMode = ColorMode.Color,  
                Orientation = Orientation.Portrait,  
                PaperSize = PaperKind.A4,  
                Margins = new MarginSettings { Top = 10 },  
                DocumentTitle = "PDF Report"  
            };  
            var objectSettings = new ObjectSettings  
            {  
                PagesCount = true,  
                HtmlContent = TemplateGenerator.GetHTMLString(),  
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },  
                HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },  
                FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }  
            };  
            var pdf = new HtmlToPdfDocument()  
            {  
                GlobalSettings = globalSettings,  
                Objects = { objectSettings }  
            };  
            var file = _converter.Convert(pdf);  
      
            return File(file, "application/pdf");  
    }  
    

    2. Download PDF:

    public IActionResult DownloadPDF()  
    {  
            var globalSettings = new GlobalSettings  
            {  
                ColorMode = ColorMode.Color,  
                Orientation = Orientation.Portrait,  
                PaperSize = PaperKind.A4,  
                Margins = new MarginSettings { Top = 10 },  
                DocumentTitle = "PDF Report"  
            };  
            var objectSettings = new ObjectSettings  
            {  
                PagesCount = true,  
                HtmlContent = TemplateGenerator.GetHTMLString(),  
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },  
                HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },  
                FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }  
            };  
            var pdf = new HtmlToPdfDocument()  
            {  
                GlobalSettings = globalSettings,  
                Objects = { objectSettings }  
            };  
            var file = _converter.Convert(pdf);  
      
            return new FileContentResult(file, "application/pdf")  
            {  
                FileDownloadName = Guid.NewGuid() + ".pdf"  
            };  
    }  
    

    My test result:

    124915-image.png


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Jason