2013-09-25 28 views
2

我使用wkhtmltopdf从给定的HTML创建PDF。基本上我们有一些基于图表/图表的报表,因此我们使用C#代码创建HTML,然后使用WKHTMLTOPDF创建PDF。它工作正常,但昨天停止创建PDF。我尝试调试和处理没有任何错误或消息的作品。只是我们没有得到PDF作为输出。我尝试从命令行运行并使用Wkhtmltopdf.exe,它确实创建了所有详细的PDF,如预期的那样。wkhtmltopdf:通过C#运行时失败代码

我的代码是

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文件和输出文件从cmmand行云大约2.3MB。]

任何想法可能会在这里出错?内存分配给任务有限还是什么?谢谢。

+0

PDFpath和HTMLPath获得什么值?另外,为什么总是会导致错误? – Nenotlep

+0

PDFPath和HTMLPath获取完整的输入和输出文件的物理路径,返回false是错误的一部分,我曾尝试过,并留下一些东西。但上述代码适用于具有2-10页报表的小文件,但对800页报表无效。这是我的主要问题。 –

+0

阿格什么恼人的问题:)。 StandardError是否包含任何内容? – Nenotlep

回答

1

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

1)他们确实建议添加忽略加载错误开关(不知道它覆盖了什么) - 加载错误处理忽略

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

+0

不,我有50-60GB的存储空间可用磁盘和大约1GB的RAM /内存可用,所以没有像磁盘空间错误 –