【发布时间】:2012-03-16 14:51:22
【问题描述】:
我像这样将小的 html 字符串转换为 pdf:
// set a path to where you want to write the PDF to.
string sPathToWritePdfTo = @"path\new_pdf.pdf";
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
sbHtml.Append("<html>");
sbHtml.Append("<html>");
sbHtml.Append("<body>");
sbHtml.Append("<font size='14'> my first pdf</font>");
sbHtml.Append("<br />");
sbHtml.Append("this is my pdf!!!!");
sbHtml.Append("</body>");
sbHtml.Append("</html>");
// create file stream to PDF file to write to
using (System.IO.Stream stream = new System.IO.FileStream
(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
{
// create new instance of Pdfizer
Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
// open stream to write Pdf to to
htmlToPdf.Open(stream);
// write the HTML to the component
htmlToPdf.Run(sbHtml);
// close the write operation and complete the PDF file
htmlToPdf.Close();
我想知道我可以在不使用 append 方法的情况下对大 html 字符串进行上述转换。我尝试了这一行:
string sbHtml=File.ReadAllText("mypath/pdf.html");
代替这一行:
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
但它不起作用:我有一个异常:
htmlToPdf.Run(sbHtml);
"xmlexception 未被用户代码处理
我还必须提到,我读取 html 文件的路径来自我的电脑! 它不是来自服务器或其他任何东西。我想获得两条路径的答案。
【问题讨论】:
-
你的意思是
it didn't work? -
它是如何失败的?你有例外吗?如果有,有什么例外?尝试读取后您的字符串是否为空?你的 HTML 有效吗?
-
我有一个例外。你可以查看我编辑的帖子。谢谢!
-
您从文件中读取的
sbHtml是否包含您期望的 html 文本?