Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I don't know where is the problem, I just want to make a pdf file of an html page. Please help me to fix this.

public ActionResult ApplyForPdf()
       {
           Document doc = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
           doc.SetPageSize(PageSize.A4);
           doc.SetMargins(20f, 20f, 20f, 20f);
           byte[] pdfBytes;
           var mem = new MemoryStream();
           pdfBytes = mem.ToArray();
           return File(pdfBytes, "application/pdf");
           //return View();
       }


What I have tried:

This is my first try to make a pdf file of an html page. So I don't try anything like this before.
Posted
Updated 3-Aug-22 21:23pm

1 solution

Given you are already using itextsharp, you should start from here: Chapter 1: Hello HTML to PDF[^]
With itext, it would be as simple as:
public void createPdf(string html, string dest)
{
	HtmlConverter.ConvertToPdf(html, new FileStream(dest, FileMode.Create));
}


In case you are using pdfHTML. (An iText 7 add-on for Java and C#), a simple example for converting HTML to PDF using iText in C# would be:
static void Main(string[] args)
{
    using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
    using (FileStream pdfDest = File.Open("output.pdf", FileMode.Create))
    {
        ConverterProperties converterProperties = new ConverterProperties();
        HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
    }
}
For complete details, refer: Convert HTML & CSS to rich, smart PDF documents - iText[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900