I convert small html strings to pdf like this:
// 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();
I wonder i can make the above conversion for big html strings,without using the append method.I tried this line:
string sbHtml=File.ReadAllText("mypath/pdf.html");
Instead of this line:
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
but it didn’t work:I had an exception in line:
htmlToPdf.Run(sbHtml);
“xmlexception was unhandled bu user code
I also have to mention that the path i read the html file is from my pc!!
It’s not from a server or anything else.I would like to get asnwers for both paths.
If the converter has an overload for string, you can simply use:
If not and accepts only StringBuilder: