且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Razor HTML转换为可下载的PDF

更新时间:2023-02-16 22:46:39

我问了另一个

I asked another question to try to solve the not downloading file issue, and I got it.

在进行任何操作之前,我都是通过ajax进行请求的,因为我认为这样做是一种很好的方法,但事实证明,有一种更简单的方法:不使用ajax.

Before anything, I was doing the request through ajax because I thought to be a good way to do so, but as it turns out, there's a much simpler way: not using ajax.

因此,我删除了按钮的脚本和按钮本身,因此现在看起来像这样:

So, I removed the script for the button and the button itself, so now looks like this:

<a href="DescargarPDF/?itemId=@item.Id" target="_blank" class="btn btn-secondary btn-info">PDF</a>

它的外观与以前相同,但实际上是到我的控制器方法的链接,现在看起来像这样:

It has the same appearance than before, but it's actually a link to my controller's method, which right now looks like this:

public FileResult DescargarPDF (int itemId) {
        var presupuesto = ReglasNegocio.Fachada.Consultas.ObtenerPresupuesto(itemId);             
        var archivo = new Rotativa.PartialViewAsPdf("_PresupuestoFinal", presupuesto) { FileName = "Presupuesto_" + itemId + ".pdf", PageSize = Rotativa.Options.Size.A4 };
        var binario = archivo.BuildFile(this.ControllerContext);        

        return File(binario, "application/pdf", archivo.FileName);           
    }

我知道在大多数情况下这不是一个有效的解决方案,因为我只是将ajax抛在了后面,但是还有很多其他问题,答案对他们有用,他们仍然使用ajax来管理请求.

I know in most cases this wouldn't be a valid solution since I just left ajax behind, but there are many other questions where the answer worked for them and they still use ajax to manage the request.

不过,我希望这会有所帮助.谢谢大家.编码愉快.

Still, I hope this helps. Thanks to everyone. Happy coding.

更新

我刚刚发现为什么将我的PDF文件放入控制台,请检查

I just found out why my PDF file was dropped into console, check the other question, I left a little explanation for that particular issue. Thanks everyone.