Complete html file in to pdf file by using asp.net

Viewed 49

I want to convert complete html file into pdf file by using asp.net 4.0. I used iTextSharp module to convert.

Here with you the source I used.

 string lstrhtml = "<html xmlns=\"http://www.w3.org/1999/xhtml\" >" +
            "<head><title>Custom Mail</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>" +
            "<style type=\"text/css\">DIV.main { width: 775px; }" +
            "BODY { font-size: 11px; margin: 0px; font-family: Arial, Helvetica, sans-serif; background-color: #ffffff }" +
            "td { font-size: 11px; font-family: Arial, Helvetica, sans-serif }" +
            "P { font-size: 11px; font-family: Arial, Helvetica, sans-serif }" +
            "A:link { font-size: 11px; color: #0000ff; font-family: Arial, Helvetica, sans-serif }" +
            "A:visited { font-size: 11px; color: #0000ff; font-family: Arial, Helvetica, sans-serif }" +
            "</style>" +
            "</head>" +
            "<body><h1>detail goes here</h1></body>" +
            "</html>";



string filePath = lstrpdfpath + pstrPNR + ".pdf";
                        FileStream pdfStream = new FileStream(filePath, FileMode.Create);

                        TextReader reader = new StringReader(lstrhtml);

                        // step 1: creation of a document-object
                        Document document = new Document(PageSize.A4, 30, 30, 30, 30);

                        // step 2:
                        // we create a writer that listens to the document
                        // and directs a XML-stream to a file
                        PdfWriter writer = PdfWriter.GetInstance(document, pdfStream);

                        // step 3: we create a worker parse the document
                        HTMLWorker worker = new HTMLWorker(document);

                        // step 4: we open document and start the worker on the document
                        document.Open();
                        worker.StartDocument();

                        // step 5: parse the html into the document
                        worker.Parse(reader);

                        // step 6: close the document and the worker
                        worker.EndDocument();
                        worker.Close();
                        document.Close();


                        pdfStream.Close();
                        pdfStream.Dispose();

Problems:-

  1. All the styles are printed in the pdf file
  2. If I use full complete html file, then I will receive exceptional error(Object Reference not set to an instance of an object) after the worker.Parse(reader); step.

It would be great if you can let me know how to sort out above issues.

Thanks in advance

1 Answers

This is how I solved this issue.

  1. Create a .net core api with DinkToPdf library and it is available in nuget.
  2. Call api through my asp.net Project.

The Problem is DinkToPdf library working under .net core environment only. That is the reason I have to build a .net core api project.

Related