0

Convert HTML+css to PDF with Itextsharp in Asp.net C# When file is save at location all the css for the pdf file get lost.

After converting file to pdf all the css is lost for the file If there is any tool please let me know

private void savepdf(System.Web.UI.HtmlControls.HtmlControl control)
{
    string destifilepath = @"D:\test\";
    string filename = "test.pdf"; 
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);  
    control.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4); //, 10f, 10f, 10f, 0f    

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
    PdfWriter.GetInstance(pdfDoc, new FileStream(destifilepath + "\\" + filename, FileMode.Create)); 
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
}
DevChamps
  • 33
  • 1
  • 11
  • HTMLWorker htmlparser = new HTMLWorker(pdfDoc); @robinB pdfDoc is the Div id which would be pass to htmlworker.We cant append Html tags to cs code because it is too huge – DevChamps May 30 '18 at 05:49
  • 1
    `HTMLWorker` will never be able to properly convert HTML with Bootstrap CSS to PDF. I recommend upgrading to iText 7 + pdfHTML. – Amedee Van Gasse May 30 '18 at 05:51
  • The name "iTextSharp" was abandoned in favor of the name "iText for .NET"; the `HTMLWorker` class was deprecated many years ago. It's unclear why you'd choose to use `HTMLWorker`. Maybe you are looking at unofficial documentation that is outdated; please consult the *official documentation* on [itextpdf.com](https://itextpdf.com). – Bruno Lowagie May 30 '18 at 06:10
  • @BrunoLowagie do you have any example to save html to pdf at specific location using itext pdf or which function would help me to build the same in C# – DevChamps May 30 '18 at 09:16
  • Yes, go to [chapter 1 of the tutorial](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-1-hello-html-pdf) and take a look at the examples that take `src` and `dest` as parameters. Those parameters define specific locations. – Bruno Lowagie May 30 '18 at 09:47
  • Thanks for your reply @BrunoLowagie now its working ConverterProperties properties = new ConverterProperties(); properties.SetBaseUri(src); but the column and width is coming different – DevChamps May 30 '18 at 10:58
  • @AmedeeVanGasse thanks for your reply – DevChamps May 30 '18 at 10:59
  • I don't understand what you mean when you say "the column and width is coming different" so you may want to post a separate question about that. Please be aware that there is a conceptual difference between HTML and PDF. A PDF *always* has pages with a fixed size; that can have an impact on widths of columns. However, I don't have any clue which width you are talking about, nor if you're talking about columns in a `` or a `
    ` column.
    – Bruno Lowagie May 30 '18 at 11:25
  • @BrunoLowagie when a file is converted to pdf the width and font gets changed – DevChamps May 30 '18 at 11:33
  • When I convert an HTML file to PDF, the width and the font doesn't get changed. So if you're blaming iText, you are wrong. If you are blaming yourself, well... that's too bad, but if you don't show which HTML you are converting using which code, no one can help you. If I were you, I'd start by reading the full tutorial. Maybe you overlooked the chapter about fonts. – Bruno Lowagie May 30 '18 at 11:40
  • @BrunoLowagie Its good to use HiQPDF issue is resolved with in 1 hr – DevChamps Jun 02 '18 at 06:44
  • @DevChamps You are hijacking a post to ask a question that is not related to the topic of that post. *Why are you doing this?* I could answer your question, but if I did, I would reward your bad behavior. Please post a new question. Do not abuse the comment section of a closed question to ask questions to a single person; post the question so that the community can answer. – Bruno Lowagie Jul 16 '18 at 13:42
  • @BrunoLowagie Apalogies – DevChamps Jul 17 '18 at 04:55

0 Answers0