Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Can't stream PDF file (.net core 2.0)

$
0
0

For a few days I have no idea how to solve my problem.

I need return to user PDF document dynamically generated based on ID. I use for this NReco.PdfGenerator.LT. When I running a project directly from VS2017 everything works fine and the file is downloaded. But if I publish a project on an IIS server and then try clicking on the button, will open a page that does not exist (HTTP ERROR 404).

Button:

<a asp-controller="Dokumenty" asp-action="PobierzPotwierdzenieDokPdf" asp-route-DokID="@item.Dok_DokID" class="btn btn-warning btn-sm"><i class="glyphicon glyphicon-print" title="Drukuj PDF"></i></a>


Controller:

public async Task<FileResult> PobierzPotwierdzenieDokPdf(int DokID)
    {

        var dokument = _context.Dokumenty.FirstOrDefault(i => i.Dok_DokID == DokID);
        var notatki = _context.DokumentyNotatki.Where(d => d.DokNot_DokID == DokID);

        viewDokumentPotwierdzeniePdf viewDokumentPotwierdzeniePdf = new viewDokumentPotwierdzeniePdf()
        {
            Dokument = dokument,
            DokumentNotatka = notatki
        };

        var htmlContent = await _viewRenderService.RenderToStringAsync("Dokumenty/DokumentPotwierdzeniePdf", viewDokumentPotwierdzeniePdf);

        var htmlToPdf = new HtmlToPdfConverter
        {
            PageHeight = 210,
            PageWidth = 148,
            Margins = new PageMargins() { Bottom = 5, Left = 5, Right = 5, Top = 5 },
            Orientation = PageOrientation.Landscape,
            PageFooterHtml = "<div style='font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: 10px; text-align: center; color: grey;'>Strona <span class='page'></span> z <span class='topage'></span> | Operator drukujący: " + HttpContext.Session.GetString("Ope_Nazwisko") + " | Data wydruku: " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + " | <b>System ERP Polimer</b></div>"
        };
        htmlToPdf.License.SetLicenseKey("PDF_Generator_Bin_Examples_Pack_0000000000", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        htmlToPdf.PdfToolPath = @"C:\Program Files\wkhtmltopdf\bin\";

        byte[] pdfBytes = htmlToPdf.GeneratePdf(htmlContent);

        FileResult fileResult = new FileContentResult(pdfBytes, "application/pdf")
        {
            FileDownloadName = dokument.Dok_NrKancelaryjny.Replace("/", "_") + ".pdf"
        };

        dokument.Dok_Wydruk = 1;
        _context.Entry(dokument).State = EntityState.Modified;
        await _context.SaveChangesAsync();

        return fileResult;
    }

IIS is trying to open the url http://domainname.int/Dokumenty/PobierzPotwierdzenieDokPdf?DokID=77 where the number at the end is the ID. But that page does not physically exist.

Please help. What am I doing wrong? I use default MVC route, maybe this is a problem? But I do not know how to change it to be good  

P.S. sorry for my English :)

 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>