Wu?? 2017-01-06 08:18 采纳率: 0%
浏览 1028

.net 用wkhtmltopdf 将网页转换为pdf格式

网页是动态页面,网页带背景图和图片。现在只能将普通文本转化出来。求助!!!!
string fileNameWithOutExtention = HttpContext.Current.Server.MapPath("..\Content\pdf\" + Guid.NewGuid() + ".pdf");
string wkhtmltopdfPath = HttpContext.Current.Server.MapPath("..\App_Start\wkhtmltopdf.exe");
if (string.IsNullOrEmpty(html))
{
return "false";
}
ProcessStartInfo si;
StringBuilder paramsBuilder = new StringBuilder();
paramsBuilder.Append("--page-size A4 ");
paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", "-", fileNameWithOutExtention);

        si = new ProcessStartInfo();
        si.CreateNoWindow = true;
        si.FileName = wkhtmltopdfPath;
        si.Arguments = paramsBuilder.ToString();
        si.UseShellExecute = false;
        si.RedirectStandardError = true;
        si.RedirectStandardInput = true;

        using (var process = new Process())
        {
            process.StartInfo = si;
            process.Start();
            using (var stream = process.StandardInput)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(html);
                stream.BaseStream.Write(buffer, 0, buffer.Length);
                stream.WriteLine();
            }
            process.WaitForExit();

        }

        if (File.Exists(fileNameWithOutExtention))
        {
            //把文件读进文件流
            FileStream fs = new FileStream(fileNameWithOutExtention, FileMode.Open);
            byte[] file = new byte[fs.Length];
            fs.Read(file, 0, file.Length);
            fs.Close();


        }
        else
        {
            throw new Exception("文件不存在!");
        }
        return fileNameWithOutExtention;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料