-2

I have to convert this Html tempelate written in notepad into PDF using itextsharp

<Body>
    <h3>Hello</h3><img src="https://i.stack.imgur.com/nMwn1.png">
</Body>
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
UmeshDixit
  • 7
  • 1
  • 4
  • Google `NReco.PdfGenerator` it would give you what you want. – iSR5 Aug 14 '20 at 10:56
  • I have to use itextsharp – UmeshDixit Aug 14 '20 at 11:00
  • Then, read this : https://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp – iSR5 Aug 14 '20 at 11:01
  • @Glorfindel i think inlining the image here is not the appropriate correction. Instead the original HTML should have been put in a code section. – mkl Jul 10 '22 at 16:07
  • 1
    @mkl thanks for catching that. My script doesn't replace images in code, but that assumes code is formatted as code in the first place :) – Glorfindel Jul 10 '22 at 16:42

2 Answers2

1

Install a package called iTextSharp through Nuget Package.

using iTextSharp.text;  
using iTextSharp.text.html.simpleparser;  
using iTextSharp.text.pdf;  

public class PdfController : Controller
{     
    [Route("/htmlpdf")]
    public FileStreamResult DownloadPDF()
    {
        string HTMLContent = "Hello <b>World</b>";// Put your html tempelate here
        
        MemoryStream ms = new MemoryStream();
        TextReader txtReader = new StringReader(HTMLContent);

        // 1: create object of a itextsharp document class  
        Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

        // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file  
        PdfWriter PdfWriter = PdfWriter.GetInstance(doc, ms);
        PdfWriter.CloseStream = false;

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

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


        // 5: parse the html into the document  
        htmlWorker.Parse(txtReader);

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

        ms.Flush(); //Always catches me out
        ms.Position = 0; //Not sure if this is required

        return File(ms, "application/pdf", "HelloWorld.pdf");
    }
}

Test of result

enter image description here

Michael Wang
  • 3,782
  • 1
  • 5
  • 15
0
//You can also use this free "SelectPdf" library for basic use

SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
//SelectPdf.PdfDocument doc = converter.ConvertUrl("https://google.com");
SelectPdf.PdfDocument doc = converter.ConvertHtmlString(HTMLContent.ToString());
doc.Save("test.pdf");
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 04 '22 at 23:08