Need to convert stream file to PDF.

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 A set of technologies in the .NET Framework for building web applications and XML web services. 4,504 questions Sign in to follow 3 comments Hide comments for this question Report a concern I have the same question I have the same question 0 votes

AgaveJoe 28,031 Reputation points 2021-08-19T13:02:44.557+00:00

You did not share the code that exports to PDF. You can't only change the content-type if that's what you're doing. You need a PDF library which are plentiful if you do an internet search.

0 votes Report a concern SAHA Arunava 1 Reputation point 2021-08-19T13:08:46.767+00:00

Ok, i thought this MIME works for word, similarly it will go for PDF if I change that content-type. Ok let me check libraries. THANK YOU

0 votes Report a concern

This comment has been deleted due to a violation of our Code of Conduct. The comment was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

Add comment

1 answer

Sort by: Most helpful Most helpful Newest Oldest

JasonPan - MSFT 5,451 Reputation points • Microsoft Vendor 2021-08-20T03:02:42.99+00:00
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" >; > 

124915-image.png

My test result: 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