带有重定向输入/输出流的HTML中的wkhtmltopdf相对路径将不起作用

时间:2014-02-14 09:33:25

标签: c# .net stdin relative-path wkhtmltopdf

我正在使用wkhtmltopdf.exe(版本0.12.0 final)从html文件生成pdf文件,我使用.NET C#

我的问题是只通过在html中指定相对路径来使javascript,样式表和图像工作。现在,如果我使用绝对路径,我可以使用它。但它不适用于相对路径,这使得整个html生成有点复杂。我把我做的事情归结为以下例子:

string CMDPATH = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe";
string HTML = string.Format(
    "<div><img src=\"{0}\" /></div><div><img src=\"{1}\" /></div><div>{2}</div>",
    "./sohlogo.png",
    "./ACLASS.jpg",
    DateTime.Now.ToString());

WriteFile(HTML, "test.html");

Process p;
ProcessStartInfo psi = new ProcessStartInfo();

psi.FileName = CMDPATH;
psi.UseShellExecute = false;
psi.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

psi.Arguments = "-q - -";

p = Process.Start(psi);

StreamWriter stdin = p.StandardInput;
stdin.AutoFlush = true;
stdin.Write(HTML);
stdin.Dispose();

MemoryStream pdfstream = new MemoryStream();
CopyStream(p.StandardOutput.BaseStream, pdfstream);
p.StandardOutput.Close();
pdfstream.Position = 0;

WriteFile(pdfstream, "test.pdf");

p.WaitForExit(10000);
int test = p.ExitCode;

p.Dispose();

我尝试过相对路径,例如:“。/ sohlogo.png”,只需“sohlogo.png”,它们都可以通过html文件在浏览器中正确显示。但它们都不适用于pdf文件。错误流中没有数据。

以下命令行的作用类似于具有相对路径的魅力:

"c:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" test.html test.pdf

在这个阶段我真的需要一些输入。所以任何帮助都非常感谢!

仅供参考,WriteFile和CopyStream方法如下所示:

public static void WriteFile(MemoryStream stream, string path)
{
    using (FileStream writer = new FileStream(path, FileMode.Create))
    {
        byte[] bytes = stream.ToArray();
        writer.Write(bytes, 0, bytes.Length);
        writer.Flush();
    }
}

public static void WriteFile(string text, string path)
{
    using (StreamWriter writer = new StreamWriter(path))
    {
        writer.WriteLine(text);
        writer.Flush();
    }
}

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32768];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, read);
    }
}

编辑:我对Neo Nguyen的解决方法。

我无法使用相对路径。所以我所做的是一种方法,它使用根路径预先建立所有路径。它解决了我的问题所以也许它会解决你的问题:

/// <summary>
/// Prepends the basedir x in src="x" or href="x" to the input html text
/// </summary>
/// <param name="html">the initial html</param>
/// <param name="basedir">the basedir to prepend</param>
/// <returns>the new html</returns>
public static string MakeRelativePathsAbsolute(string html, string basedir)
{
    string pathpattern = "(?:href=[\"']|src=[\"'])(.*?)[\"']";

    // SM20140214: tested that both chrome and wkhtmltopdf.exe understands "C:\Dir\..\image.png" and "C:\Dir\.\image.png"
    //             Path.Combine("C:/
    html = Regex.Replace(html, pathpattern, new MatchEvaluator((match) =>
        {
            string newpath = UrlEncode(Path.Combine(basedir, match.Groups[1].Value));
            if (!string.IsNullOrEmpty(match.Groups[1].Value))
            {
                string result = match.Groups[0].Value.Replace(match.Groups[1].Value, newpath);
                return result;
            }
            else
            {
                return UrlEncode(match.Groups[0].Value);
            }
        }));

    return html;
}

private static string UrlEncode(string url)
{
    url = url.Replace(" ", "%20").Replace("#", "%23");
    return url;
}

我尝试了类似System.Uri.EscapeDataString()的不同System.Uri.Escape ***方法。但他们最终对wkhtmltopdf进行了严格的url编码以理解它。由于时间不够,我只是做了上面快速而又脏的UrlEncode。

2 个答案:

答案 0 :(得分:1)

根据 official docs of the command line ,有一个名为 --cache-dir 的选项。

似乎他们的意思是工作目录。 我使用它,它适用于 v0.12.3

wkhtmltopdf /my/path/to/index.html test.pdf --cache-dir /my/path/to

答案 1 :(得分:0)

快速地看,我认为问题可能在于

psi.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;

我认为这是路径指向的地方。我假设

"c:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" test.html test.pdf

工作意味着test.htmlsrc="mlp.png"引用的图片位于c:\Program Files\wkhtmltopdf\bin\mlp.png,对吧?我认为它的工作原理是因为您的图像文件与wkhtmltopdf位于同一个文件夹中...所以请尝试将WorkingDirectory设置为该目录,看看会发生什么。