c# – 使用WKHTMLTOPDF以HTML格式显示图像,但不以PDF格式显示
发布时间:2020-12-15 05:39:12 所属栏目:百科 来源:网络整理
导读:您正在使用Wkhtmltopdf使用c#,Asp.net从Html页面创建PDF. PDF创建顺利,但是当我将图像添加到HTML时,它不会显示在PDF上. public static string HtmlToPdf(string pdfOutputLocation,string outputFilenamePrefix,string[] urls,string[] options = null,// st
您正在使用Wkhtmltopdf使用c#,Asp.net从Html页面创建PDF.
PDF创建顺利,但是当我将图像添加到HTML时,它不会显示在PDF上. public static string HtmlToPdf(string pdfOutputLocation,string outputFilenamePrefix,string[] urls,string[] options = null,// string pdfHtmlToPdfExePath = "C:Program Files (x86)wkhtmltopdfwkhtmltopdf.exe") string pdfHtmlToPdfExePath = "C:Program Fileswkhtmltopdfwkhtmltopdf.exe") { string urlsSeparatedBySpaces = string.Empty; try { //Determine inputs if ((urls == null) || (urls.Length == 0)) throw new Exception("No input URLs provided for HtmlToPdf"); else urlsSeparatedBySpaces = String.Join(" ",urls); //Concatenate URLs string outputFolder = pdfOutputLocation; string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name var p = new System.Diagnostics.Process() { StartInfo = { FileName = pdfHtmlToPdfExePath,Arguments = ((options == null) ? "" : String.Join(" ",options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,UseShellExecute = false,// needs to be false in order to redirect output RedirectStandardOutput = true,RedirectStandardError = true,RedirectStandardInput = true,// redirect all 3,as it should be all 3 or none WorkingDirectory = HttpContext.Current.Server.MapPath(outputFolder),CreateNoWindow = true } }; p.Start(); // read the output here... var output = p.StandardOutput.ReadToEnd(); var errorOutput = p.StandardError.ReadToEnd(); // ...then wait n milliseconds for exit (as after exit,it can't read the output) p.WaitForExit(60000); // read the exit code,close process int returnCode = p.ExitCode; p.Close(); // if 0 or 2,it worked so return path of pdf if ((returnCode == 0) || (returnCode == 2)) return outputFolder + outputFilename; else throw new Exception(errorOutput); } catch (Exception exc) { throw new Exception("Problem generating PDF from HTML,URLs: " + urlsSeparatedBySpaces + ",outputFilename: " + outputFilenamePrefix,exc); } } 这是我显示图片的HTML代码区域.. <div id="Image" class="right" style="background:#333;height:15%;width:25%;"> <img id="LogoImage" runat="server" width="100%" height="100%" src="../TempLogo/chafa.jpg" /> 解决方法
尝试为图像src添加完整路径.这通常是由于“../TempLogo/chafa.jpg”实际上并不是wkhtmltopdf工作目录的正确相对URL.
所以,而不是“../TempLogo/chafa.jpg”尝试“C:/yourpath/TempLogo/chafa.jpg”或“file:/// C:/yourpath/TempLogo/chafa.jpg”,看看是否有帮助.如果是这样,你的相对路径就是问题所在. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |