C# 使用WKHTMLTOPDF以HTML格式而非PDF格式显示图像
您好,我正在使用Wkhtmltopdf使用c#,Asp.net从Html页面创建PDF。 PDF的创建非常顺利,但当我将图像添加到HTML时,它不会显示在PDF上C# 使用WKHTMLTOPDF以HTML格式而非PDF格式显示图像,c#,asp.net,c#-4.0,wkhtmltopdf,C#,Asp.net,C# 4.0,Wkhtmltopdf,您好,我正在使用Wkhtmltopdf使用c#,Asp.net从Html页面创建PDF。 PDF的创建非常顺利,但当我将图像添加到HTML时,它不会显示在PDF上 public static string HtmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls, string[] options = null, // string pdfHtmlToPd
public static string HtmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls,
string[] options = null,
// string pdfHtmlToPdfExePath = "C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe")
string pdfHtmlToPdfExePath = "C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.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“看看这是否有帮助。如果是这样,你的相对路径就是问题所在 将pdfkit与ruby一起使用,我发现如果我将STDIN上带有图像的html文件发送到wkhtmltopdf,图像不会呈现。(ie wkhtmltopdf-toto.pdf:无图像) 如果我通过传递一个html文件(wkhtmltopfdtoto.htmltoto.html)来手动执行完全相同的命令,这是可行的
不知道为什么,但是,我只是写了一个小包装,这样称呼它。我最近看到一个jpeg文件转换失败-问题是图像是灰度图像
请注意,所有灰度图像不一定都是灰度图像-如果是这种情况,请使用irfanview之类的程序进行检查。我使用视图存储我想要的每个pdf模板的HTML标记,然后将模型传递给视图。使用Server.MapPath()将获得图像的完整路径
<img src="@Server.MapPath("~/path/to/your/image.jpg")" />
我遇到了这个问题,对我来说,“修复”是删除高度属性 而不是
试一试
我无法解释这种行为,它可能是一个bug。生成的HTML文件将按我的需要显示。但图像不以PDF格式显示。请帮忙。我会试试的,让你知道。我试过了,但是没有用。请帮我解决这个问题。我用过“file:///E:/AMS/AMS/AMS.WEB/TempLogo/chafa.jpg&“src=”“也是,但它不起作用。我还尝试将版本从0.11更改为0.9,但也没有成功。我会试着想点别的。
file:///E:/AMS/AMS/AMS.WEB/TempLogo/chafa.jpg
为我工作。使用absolutUri
,HTML只呈现absolutUri
。嘿,你能显示toto.HTML吗?我真的很想复制它,因为除了路径问题之外,我从来没有遇到过其他问题。现在我重新思考它,我认为这也可能是一个路径问题-你的HTML有相对路径吗?因为在非标准格式转换期间,工作目录可能会从输入文件中读取。这也为我修复了它。更新到0.12.5似乎已经修复了这个错误,并允许我使用宽度和高度,而不会出现任何问题。这为我“解决”了这个问题。我正在运行0.12.5,但仍然不能同时使用高度和宽度