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条回答

    报告相同问题?

    悬赏问题

    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
    • ¥15 onvif+openssl,vs2022编译openssl64
    • ¥15 iOS 自定义输入法-第三方输入法
    • ¥15 很想要一个很好的答案或提示
    • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
    • ¥15 怀疑手机被监控,请问怎么解决和防止
    • ¥15 Qt下使用tcp获取数据的详细操作
    • ¥15 idea右下角设置编码是灰色的
    • ¥15 全志H618ROM新增分区