我转换小HTML字符串PDF这样的:C#读取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();
我不知道我可以为大HTML字符串上面的转换,而无需使用附加method.I试过这种行:
string sbHtml=File.ReadAllText("mypath/pdf.html");
而是这行:
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
,但它没有工作,我只好排队异常:
htmlToPdf.Run(sbHtml);
“xmlexception了未处理BU用户代码
我还不得不提到的路径我阅读HTML文件是从我的电脑! 这不是来自服务器或其他任何东西。我想为这两个路径获取asnwers。
而你的意思是“它没有工作”? – 2012-03-16 14:55:24
它是如何失败?你有例外吗?如果是这样,什么是例外?尝试完成读取后,字符串是否为空?你的HTML有效吗? – JamieSee 2012-03-16 14:58:13
我有一个exception.you可以看看我编辑后post.Thanks! – Dchris 2012-03-16 15:10:55