简体   繁体   中英

c# read html file and convert to pdf

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.

In regards to the exception, make sure the HTML is valid XHTML. PDFizer requires valid XHTML.

If the converter has an overload for string, you can simply use:

  htmlToPdf.Run(File.ReadAllText(@"mypath/pdf.html"));

If not and accepts only StringBuilder:

  System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();
    sbHtml.Append(File.ReadAllText(@"mypath/pdf.html"));

Would this help?

System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();    
sbHtml.Append(File.ReadAllText("mypath/pdf.html"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM