topdf解析表格html字符串,c# – 如何使用wkhtmltopdf作为字符串传递html?

在这个例子中,STDIn和STDOut被重定向了,所以你根本不需要文件。

public static class Printer

{

public const string HtmlToPdfExePath = "wkhtmltopdf.exe";

public static bool GeneratePdf(string commandLocation, StreamReader html, Stream pdf, Size pageSize)

{

Process p;

StreamWriter stdin;

ProcessStartInfo psi = new ProcessStartInfo();

psi.FileName = Path.Combine(commandLocation, HtmlToPdfExePath);

psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName);

// run the conversion utility

psi.UseShellExecute = false;

psi.CreateNoWindow = true;

psi.RedirectStandardInput = true;

psi.RedirectStandardOutput = true;

psi.RedirectStandardError = true;

// note: that we tell wkhtmltopdf to be quiet and not run scripts

psi.Arguments = "-q -n --disable-smart-shrinking " + (pageSize.IsEmpty? "" : "--page-width " + pageSize.Width + "mm --page-height " + pageSize.Height + "mm") + " - -";

p = Process.Start(psi);

try {

stdin = p.StandardInput;

stdin.AutoFlush = true;

stdin.Write(html.ReadToEnd());

stdin.Dispose();

CopyStream(p.StandardOutput.BaseStream, pdf);

p.StandardOutput.Close();

pdf.Position = 0;

p.WaitForExit(10000);

return true;

} catch {

return false;

} finally {

p.Dispose();

}

}

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);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值