displaying / downloading pdf in server-side blazor
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.
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.
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
@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
I need help for serverside I want to display it in the browser after conversion
@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
I tried with iframe and object but it is not display pdf file, it just download it what the error in your opinion?
@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.

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