- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用Rotativa工具
来显示PDF。它在localhost
上工作正常,但在Azure
平台上不起作用。
下面是我的代码...
public ActionResult GeneratePDF(int id = 0)
{
ReportTransactionData reporttransactiondata = db.ReportTransactionDatas.Find(id);
var viewFileToPrint = @"~/Views/ReportTranData/PDFReport.cshtml";
//var oRotativaPDF = new Rotativa.ViewAsPdf();
var oRotativaPDF = new Rotativa.PartialViewAsPdf();
try
{
if (reporttransactiondata == null)
{
return HttpNotFound();
}
else
{
// Populate reporttransactiondata with Verfier Name...TO BE IMPLEMENTED LATER...
//reporttransactiondata.VerifierName = GetVerifierNameByID(reporttransactiondata.VerifierID);
}
// Code to call a function/action...
//return new Rotativa.ActionAsPdf("PrintRptInPDF", reporttransactiondata)
//oRotativaPDF = new Rotativa.ViewAsPdf(viewFileToPrint, reporttransactiondata)
// {
// FileName = "Technician Job Report.pdf",
// PageSize = Size.A4,
// PageOrientation = Orientation.Portrait,
// PageMargins = new Margins(0, 0, 0, 0),
// PageWidth = 230, //250 //300 // 350
// PageHeight = 360, // 380 // 400 //420 // 450
// CustomSwitches = "--disable-smart-shrinking"
// };
oRotativaPDF = new Rotativa.PartialViewAsPdf(viewFileToPrint, reporttransactiondata)
{
FileName = "Technician Job Report.pdf",
PageSize = Size.A4,
PageOrientation = Orientation.Portrait,
PageMargins = new Margins(0, 0, 0, 0),
PageWidth = 230, //250 //300 // 350
PageHeight = 360, // 380 // 400 //420 // 450
CustomSwitches = "--disable-smart-shrinking"
};
}
catch (Exception ex)
{
// TODO: Code here...
}
return oRotativaPDF;
}
请忽略注释代码。这工作得很好,但是当我部署 Web 应用程序时,客户端不会下载 PDF 文件,并且一段时间后我的 IE 浏览器会显示 500 内部服务器错误。
我进一步探讨了这个问题,发现这个错误可能是因为 wkhtmltopdf.exe 没有在 Azure 平台上自行执行。因此,我在网上搜索有关问题解决方案的帮助下得出了以下解决方案......
公共(public) ActionResult 生成 PDF(int id = 0) { ReportTransactionData reporttransactiondata = db.ReportTransactionDatas.Find(id); 字符串 viewName = @"~/Views/ReportTranData/PDFReport.cshtml"; 字符串 wkhtmltopdfPath = Server.MapPath(@"~/Rotativa/"); 字符串开关 = string.Empty; 尝试 { if (报告交易数据 == null) { 返回 HttpNotFound(); }
string fullPath = Server.MapPath(@"~/ApplicationFiles/TechnicianJobReport.pdf");
FileInfo objFileInfo = new System.IO.FileInfo(fullPath);
if (objFileInfo.Exists)
{
objFileInfo.Delete();
}
string sViewString = RenderRazorViewToString(viewName, reporttransactiondata);
var byteArray = ConvertHTMLtoPDF(wkhtmltopdfPath, switches, sViewString);
var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
fileStream.Write(byteArray, 0, byteArray.Length);
fileStream.Close();
// Download file at client side...
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(objFileInfo.Name));
Response.ContentType = "application/pdf";
Response.WriteFile(objFileInfo.FullName);
Response.End();
}
catch (Exception ex)
{
// Handle exception here and Log Error to file...
Repositories.Repository objRepository = new Repositories.Repository();
string sLogFilePath = Server.MapPath(@"~/ApplicationFiles/ErrorLogFile.txt");
objRepository.LogErrorToFile(ex, sLogFilePath, this.ControllerContext.Controller.ToString());
}
return View(reporttransactiondata);
}
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
/// <summary>
/// Converts given URL or HTML string to PDF.
/// </summary>
/// <param name="wkhtmltopdfPath">Path to wkthmltopdf.</param>
/// <param name="switches">Switches that will be passed to wkhtmltopdf binary.</param>
/// <param name="html">String containing HTML code that should be converted to PDF.</param>
/// <returns>PDF as byte array.</returns>
private static byte[] ConvertHTMLtoPDF(string wkhtmltopdfPath, string switches, string html)
{
// switches:
// "-q" - silent output, only errors - no progress messages
// " -" - switch output to stdout
// "- -" - switch input to stdin and output to stdout
switches = "-q " + switches + " -";
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
switches += " -";
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.exe"),
Arguments = switches,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
WorkingDirectory = wkhtmltopdfPath,
CreateNoWindow = true
}
};
proc.Start();
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
using (var sIn = proc.StandardInput)
{
sIn.WriteLine(html);
}
}
var ms = new MemoryStream();
using (var sOut = proc.StandardOutput.BaseStream)
{
byte[] buffer = new byte[4096];
int read;
while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
string error = proc.StandardError.ReadToEnd();
if (ms.Length == 0)
{
throw new Exception(error);
}
proc.WaitForExit();
return ms.ToArray();
}
但这在本地主机上再次工作正常,但在 Azure 服务器上却不行,并给出相同的 500 内部服务器错误,没有任何异常。请看看这里是否有人可以提供帮助。我使用这个 wkhtmltopdf exe 因为我可以根据我的(A4 页面大小的一半)纸张尺寸要求指定 pdf 的高度和宽度。如果有任何其他选项可能不会导致我最终遇到执行外部 exe 或 dll 的问题,请也建议该选项。
最佳答案
像 (#Fabrizio Accatino) 写道:Rotativa 正在运行 wkhtmltopdf.exe。它位于项目根目录下的“Rotativa”文件夹中。所以问题可能是:
我希望这能指导您解决问题。
关于pdf - 我正在使用 Rotativa 工具来显示 pdf。它在本地主机上运行良好,但在 Azure 平台上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28940062/
我在 cordova@7.1.0、cordova-ios@4.5.2 下运行。安装平台:ios 4.5.2。 我运行 npm install、bower install,然后运行 cordova
我正在使用 VSTS 构建 IOS,运行命令后出现以下错误:cordova build ios 平台“android”似乎不是有效的 cordova 平台。它缺少 API.js。不支持安卓。 Cord
您使用什么软件/Wiki 来编写和分享有关开发人员、测试人员和管理人员的规范? 你使用维基系统,如果是,你使用什么维基软件? 或者您是否使用 Sharepoint 来管理和版本规范?将 SharePo
这是一家销售完整软件套件/平台的公司的示例 www.ql2.com/technology/platform.php 我想知道这样的套件/平台是如何开发的?你必须使用J2EE吗? 我更感兴趣的是这家公司
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个连接到套接字连接的应用程序,并且该连接向我发送了很多信息..可以说每秒 300 个订单(也许更多)..我有一个类(它就像一个监听器,对某个事件(并且该事件具有顺序)接收该顺序。创建一个对象,然
我即将开始一个 Netbeans 平台的项目。有没有人推荐他们用过并觉得有用的书籍或教程? 编辑: 这是一个已经开发好的swing应用。 最佳答案 除了 NetBeans 网站上的教程外,我还喜欢这本
有没有什么好的方法可以以非特定语言的方式定义接口(interface)/类层次结构,然后以特定语言生成相应的源代码?特别是,我需要同时针对 Java 和 C# 来创建一个相当全面的 API。我记得有一
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
大家晚上好我使用 API 平台,我想在创建实体时自动将所有者添加到我的实体中。我创建了一个事件来覆盖 API 平台,它获取当前用户并添加它。但是我的事件永远不会发生,但它确实存在于 debug:eve
这是一个有点奇怪的元编程问题,但我意识到我的新项目不需要完整的 MVC 框架,作为一个 Rails 人,我不确定现在该使用什么。 为您提供必要功能的要点;该网站将显示静态页面,但用户将能够登录并“编辑
这两天我的信息有点过载。 我打算建立自己的网站,允许本地企业列出他们的打折商品,然后用户可以进来搜索“Abercrombie T 恤”,然后就会列出出售它们的商店。 这是一个非常棒的小项目,我真的很兴
我的任务是为产品的下一代版本评估“企业”平台。我们目前正在考虑两种“类型”的平台——RAD(工作流引擎、集成 UI、工作流“技术插件”的小核心、状态的自动持久化……),例如 SalesForce.co
我需要一个不依赖于特定语言或构建系统的依赖管理器。我研究了几个优秀的工具(Gradle、Bazel、Hunter、Biicode、Conan 等),但没有一个能满足我的要求(见下文)。我还使用了 Gi
我在 Symfony 4 Flex 应用程序中使用 API Platform v2.2.5,该应用程序由一个功能 API 和 JWT Authentication 组成。 ,一些资源默认Open AP
虽然隐私法通常不属于我们开发人员的管辖范围,但我确实认为这是一个重要的话题,因为我们开发人员应该有责任警告我们的雇主,如果他们想要的东西会违反一些法律......在这种情况下,隐私法......通常情
我已经下载了 VisualVM 源代码,并尝试使用 Netbeans 7.01 编译 Glassfish 插件。这样做会导致以下错误: C:\source\visualvm\trunk\plugins
尝试 gradle 同步后...失败并在消息对话框中显示 Missing Android platform(s) detected: 'android-26' Install missing plat
大家好!我最近开始使用 Cordova,当我运行 Cordova platform add android 时,出现以下错误。我已经成功放置了 Java 和 Android SDK 的环境变量。但 n
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我是一名优秀的程序员,十分优秀!