question

SAHAArunava-4839 avatar image
0 Votes"
SAHAArunava-4839 asked JasonPan-MSFT commented

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();
dotnet-aspnet-core-mvc
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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 0 ·

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 0 ·

1 Answer

JasonPan-MSFT avatar image
0 Votes"
JasonPan-MSFT answered JasonPan-MSFT commented

Hi @SAHAArunava-4839


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




image.png (60.7 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Kindly let me know how to use my stream variables in pdf content.
And I did some internet research, to render images in pdf we have to use 3rd party libraries (itextSharp)
Is it true?

0 Votes 0 ·

In this code, all my data is in Stream Variable S

0 Votes 0 ·

Hi @SAHAArunava-4839

If you need further help, please provide more example code, how do you get the stream, read from the file or? Provide more code blocks, I can better help you.

0 Votes 0 ·
Show more comments