2 qq 33035717 qq_33035717 于 2017.01.06 16:18 提问

.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;
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!