根级别的数据无效。第1行,第1位。 [英] Data at the root level is invalid. Line 1, position 1.

查看:111
本文介绍了根级别的数据无效。第1行,第1位。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将html保存为PDF格式。我收到此错误:根级别的数据无效。第1行,第1位,html包含文本框,标签,图像

我的代码

I want to save a html as PDF. I am getting this error:Data at the root level is invalid. Line 1, position 1, html contains textboxes,lables,images
my code

string confirmValue = Request.Form["confirm_value"];
           if (confirmValue == "Yes")
           {

               string sPathToWritePdfTo = @"C:\new_pdf_name.pdf";


               System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
               sbHtml.Append("Contractor1.aspx");




               using (System.IO.Stream stream = new System.IO.FileStream(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
               {

                   Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();

                   htmlToPdf.Open(stream);

                   htmlToPdf.Run(sbHtml.ToString());

                   htmlToPdf.AddChapter("Mobily");

                   htmlToPdf.Close();
               }


               HttpContext.Current.Response.Clear();

               HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "mobily.pdf"));

               HttpContext.Current.Response.ContentType = "application/pdf";



               HttpContext.Current.Response.WriteFile(sPathToWritePdfTo);

               HttpContext.Current.Response.End();

           }

推荐答案

我建​​议使用http://wkhtmltopdf.org/ [ ^ ]。



参考 - C# - 使用Pechkin(WkHtmlToPdf)将HTML转换为PDF [ ^ ],它使用.NET包装器类 wkhtmltopdf
I would suggest to use http://wkhtmltopdf.org/[^].

Refer - C# – Convert HTML to PDF using Pechkin (WkHtmlToPdf)[^], which uses the .NET Wrapper Class for wkhtmltopdf.


这篇关于根级别的数据无效。第1行,第1位。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆