【问题标题】:wkhtmltopdf : failing when run through C# Codewkhtmltopdf:通过 C# 代码运行时失败
【发布时间】:2013-10-24 01:04:59
【问题描述】:

我正在使用 wkhtmltopdf 从给定的 HTML 创建 PDF。基本上我们有一些基于图形/图表的报告,所以我们使用 C# 代码创建 HTML,然后使用 WKHTMLTOPDF 创建 PDF。它工作正常,但昨天它停止创建 PDF。我尝试在没有任何错误或消息的情况下调试和处理作品。只是我们没有得到 PDF 作为输出。我尝试从命令行运行和使​​用 Wkhtmltopdf.exe,它确实创建了具有预期所有详细信息的 PDF。

我的代码是

public static bool HTMLtoPDF(string HTMLPath, string PDFPath, PageOrientation orientation)
{
    using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
    {
        string dir = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Vikasumit.VSCommon)).Location) + "\\wkhtmltopdf\\";

        proc.StartInfo.WorkingDirectory = dir;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.FileName = dir + "wkhtmltopdf.exe";
        if (orientation == PageOrientation.Landscape)
        {
            proc.StartInfo.Arguments = " --page-width 11in --page-height 8.5in --margin-left 0.5cm --margin-right 0 --margin-top 1.25cm --margin-bottom 0 " + HTMLPath + "  " + PDFPath;
        }
        else
        {
            proc.StartInfo.Arguments = " --page-width 8.5in --page-height 11in --margin-left 0.5cm --margin-right 0 --margin-top 1.25cm --margin-bottom 0 " + HTMLPath + "  " + PDFPath;
        }
        proc.Start();
        proc.WaitForExit();
        string result = proc.StandardOutput.ReadToEnd();
        proc.Close();
        proc.Dispose();
    }
    return false;
}

确实这很好用,此代码适用于只有一页或 2 页的 PDF,但此报告有 800 页 [大约 1.2MB HTML 文件和来自命令行的输出文件大约 2.3MB。]

知道这里可能出了什么问题吗?分配给任务的内存是有限的还是什么?谢谢。

【问题讨论】:

  • PDFpath 和 HTMLPath 得到什么值?另外,为什么你的结果总是假的?
  • PDFPath 和 HTMLPath 获取输入和输出文件的完整物理路径,返回 false 是错误的一部分,我之前尝试过 catch 并在那里留下一些东西。但是上面的代码适用于 2-10 页报告的小文件,但对于 800 页报告失败。这是我的主要问题。
  • 啊,真是个烦人的问题 :)。 StandardError 是否包含任何内容?
  • 不,伙计,如果有错误,我现在可能已经解决了:)。

标签: c# pdf wkhtmltopdf


【解决方案1】:

我使用 Rotativa nuget 组件将 wkhtmltopdf 可执行文件包装到我的 Web 应用程序中。我不保存到文件,而是用它来为客户下载 pdf...

1) 他们确实建议添加忽略加载错误开关(不确定它涵盖的内容) --load-error-handling 忽略

2) 由于您写入磁盘,而且它只发生在大文件上,是否存在磁盘空间问题?

【讨论】:

  • 不,我有 50-60GB 的磁盘可用存储空间和大约 1GB 的 RAM/内存可用,所以没有像磁盘空间错误这样的事情
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
相关资源
最近更新 更多