简体   繁体   English

C#MVC使用NReco.PdfGenerator从View生成PDF

[英]C# MVC generate PDF from View with NReco.PdfGenerator

I'm currently working on a project where I need to create a "dashboard" which can be exported as pdf. 我目前在一个项目中,需要创建一个“仪表板”,可以将其导出为pdf。 I wanted to use Rotativa but as our application uses .NET framework 4.0 it's not possible. 我想使用Rotativa,但由于我们的应用程序使用.NET Framework 4.0,因此不可能。 So I found the NReco PdfGenerator. 因此,我找到了NReco PdfGenerator。

Now that's the code how I create the PDF result: 现在就是我创建PDF结果的代码:

var ViewAsString = RenderViewAsString("~/Views/QMetrics/StandardDashboard.cshtml", viewModel);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.PageWidth = 1600;
htmlToPdf.PageHeight = 900;
var pdfBytes = htmlToPdf.GeneratePdf(ViewAsString);
FileResult FileResult = new FileContentResult(pdfBytes, "application/pdf");
FileResult.FileDownloadName = "Dashboard-" + viewModel.ProjectName + "-" + 
DateTime.Now.ToString() + "-.pdf";
return FileResult;

It successfully creates the PDF page with all the content that comes from the backend (Project information, and so on) but the page looks very ugly. 它成功创建了PDF页面,其中包含来自后端的所有内容(项目信息等),但是该页面看起来非常难看。 On the original page I have 2 columns and on the PDF page it puts everything in one column. 在原始页面上,我有2列,而在PDF页面上,则将所有内容都放在一列中。 I tried a few different page sizes and I also changed the layout to be non-responsive but nothing has changed. 我尝试了几种不同的页面尺寸,并且我也将布局更改为无响应,但没有任何变化。

My first suggesstion was that the referenced CSS and JS files are not included when the PDF get's created, so I copied all the stuff that comes from external files (bootstrap, Chart.js) and pasted it directly in the .cshtml file. 我的第一个建议是创建PDF时不包括所引用的CSS和JS文件,因此我复制了来自外部文件(引导程序,Chart.js)的所有内容并将其直接粘贴到.cshtml文件中。 But nothing changed at all. 但是什么都没有改变。 My Chart is not rendering/loading and the missing CSS stuff is still not there. 我的图表未渲染/加载,缺少的CSS内容仍然不存在。

On the NReco PDFGenerator website they say that it supports complex CSS code and also javascript code so I don't really understand why this is not working. 他们在NReco PDFGenerator网站上说,它支持复杂的CSS代码以及javascript代码,因此我真的不明白为什么这不起作用。

Has anyone here experiences with NReco or can someone recommend something else that works for .NET 4.0? 这里有没有人对NReco有经验,或者有人可以推荐适用于.NET 4.0的其他东西?

NReco PdfGenerator internally uses wkhtmltopdf tool, so you can check it and its options. NReco PdfGenerator内部使用wkhtmltopdf工具,因此您可以检查它及其选项。

Regarding 2 columns: if you don't use flex/grid layout everything should work fine. 关于2列:如果不使用flex / grid布局,那么一切都应该正常工作。 Possibly you need to disable wkhtmltopdf smart shrinking logic (enabled by default) and define web page 'window' size explicitely (with "--viewport-size 1600" option). 可能需要禁用wkhtmltopdf智能收缩逻辑(默认情况下启用),并明确定义网页“窗口”的大小(使用“ --viewport-size 1600”选项)。

Regarding CSS and charts: you need to check that CSS files could be accessed by wkhtmltopdf, simplest way to do that is running wkhtmltopdf.exe from the command line and check console log output (or, handle PdfGenerator's "LogReceived" event in C#). 关于CSS和图表:您需要检查wkhtmltopdf是否可以访问CSS文件,最简单的方法是从命令行运行wkhtmltopdf.exe并检查控制台日志输出(或在C#中处理PdfGenerator的“ LogReceived”事件)。 For Chart.js ensure that chart container div has explicit width (not in %), and that there are no js errors (you can get them in console by specifying "--debug-javascript" option). 对于Chart.js,请确保图表容器div具有显式宽度(不在%中),并且没有js错误(您可以通过在控制台中指定“ --debug-javascript”选项来获取它们)。 If your js code uses 'bind' method you have to include polyfill as WebKit engine version used in wkhtmltopdf doesn't support 'bind'. 如果您的js代码使用'bind'方法,则必须包含polyfill,因为wkhtmltopdf中使用的WebKit引擎版本不支持'bind'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM