Mal*_*iri 2 c# rotativa asp.net-core
我正在尝试使用Rotativa在asp.net核心中创建pdf,但它给我一个错误.我想创建html并将其转换为pdf,然后存储在服务器目录中
[HttpPost]
public IActionResult Index(Invoice invoice)
{
var webRoot = _env.WebRootPath;
var pdf = new ViewAsPdf("Index")
{
FileName = "Test.pdf",
PageSize = Rotativa.AspNetCore.Options.Size.A4,
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
PageHeight = 20,
};
var byteArray = pdf.BuildFile(ControllerContext).Result;
//var fileStream = new MemoryStream(Path.Combine(webRoot, pdf.FileName), FileMode.Create, FileAccess.Write);
var memoryStream = new MemoryStream(byteArray, 0, byteArray.Length);
}
Run Code Online (Sandbox Code Playgroud)
该错误表示您尚未配置Rotativa.
像这样配置您的应用程序:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
Rotativa.AspNetCore.RotativaConfiguration.Setup(env);
}
Run Code Online (Sandbox Code Playgroud)
也wkhtmltopdf.exe像这样添加到wwwroot:
wwwroot
Rotativa
wkhtmltopdf.exe
Run Code Online (Sandbox Code Playgroud)
完成后,以下内容将起作用.
public async Task<IActionResult> Index()
{
var pdf = new Rotativa.AspNetCore.ViewAsPdf("Index")
{
FileName = "C:\\Test.pdf",
PageSize = Rotativa.AspNetCore.Options.Size.A4,
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
PageHeight = 20,
};
var byteArray = await pdf.BuildFile(ControllerContext);
return File(byteArray, "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)
注意使用async/await而不是使用.Result.
这里的GitHub演示https://github.com/shaunluttin/asp-net-core-rotativa-pdf
在这里下载wkhtmltopdf https://wkhtmltopdf.org/
| 归档时间: |
|
| 查看次数: |
420 次 |
| 最近记录: |