aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

displaying / downloading pdf in server-side blazor

Open uwefms opened this issue 8 years ago • 8 comments

Is there a way to display a pdf file in server-side blazor like I can do in client-side blazor in a controller like:

   [HttpGet]
    public IActionResult CreatePDF()
    {
        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 }
        };
		CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();			
		context.LoadUnmanagedLibrary("C:\\Dinktopdf\\libwkhtmltox.dll");			

		var file = _converter.Convert(pdf);
		return File(file, "application/pdf");
    }

At server-side I fail using

return File(file, "application/pdf");

which only seems to work in controllers which I did not get to work either.

uwefms avatar Sep 21 '18 19:09 uwefms

Found out by myself ... There is no need to send the pdf via

File(file, "application/pdf")

as it is already there where we need it to display.

< embed src="pdf/reservations.pdf" width="100%" height="2100px" />

did the trick. Even better - printing and downloading is provided also.

Closed as beeing no real blazor server-side issue.

uwefms avatar Sep 23 '18 07:09 uwefms

I need to do that please, so I want more details how to show pdf that stored in the variable 'file' in your method var file = _converter.Convert(pdf); directly after conversion not from PDF file

khalidalbasheer avatar Sep 14 '19 17:09 khalidalbasheer

@khaledalmolaa

I need to do that please, so I want more details how to show pdf that stored in the variable 'file' in your method var file = _converter.Convert(pdf); directly after conversion not from PDF file

Sorry for the delay ...

I am not sure what you mean. Do you need help for serverside or clientside blazor ? Do you want to know how I create the pdf with Dink2Pdf or how to display it ?

 regards

     Uwe

uwefms avatar Sep 17 '19 16:09 uwefms

I need help for serverside I want to display it in the browser after conversion

khalidalbasheer avatar Sep 17 '19 18:09 khalidalbasheer

@khaledalmolaa ,

If you have or created a pdf with name "Reservierungen.pdf" in a directory called /pdf in your wwwroot folder

just use:

<iframe src="pdf/Reservierungen.pdf" style="width: 100%;height: 930px;border: none;" frameborder="0" allowfullscreen></iframe>

on your razor page and it will be displayed, printed or downloaded. Just try with any pdf. It works with firefox chrome or edge.

regards

   Uwe

uwefms avatar Sep 17 '19 18:09 uwefms

I tried with iframe and object but it is not display pdf file, it just download it what the error in your opinion?

khalidalbasheer avatar Sep 17 '19 19:09 khalidalbasheer

@khaledalmolaa ,

I created a small project with only one additional line in index.razor at

https://github.com/uwefms/BlazorPdfSample.

If you run it with the newest firefox you get the following screen. With other browsers you might have to hover over the frame to make the icons appear.

ManagePdf

regards

Uwe

uwefms avatar Sep 18 '19 16:09 uwefms

@uwefms There is no changes, I think the problem is with the browser not in the blazor pic Thank you @uwefms for your time and efforts. Good luck

khalidalbasheer avatar Sep 18 '19 17:09 khalidalbasheer